A simpler approach:
@echo off
setlocal EnableDelayedExpansion
set aPath=C:\just\a\long\path\to\a\file\in\the\file\system\filenot\file\folder
set aDir=file
echo %aPath%
rem This FOR is just for testing; remove it...
for /L %%n in (1,1,3) do (
set "n=%%n"
set "result="
for %%a in ("!aPath:\%aDir%\=" "!") do if !n! gtr 0 set "result=!result!%%~a\%aDir%\" & set /A n-=1
echo %%n: !result:~0,-1!
)
Output example:
C:\just\a\long\path\to\a\file\in\the\file\system\filenot\file\folder
1: C:\just\a\long\path\to\a\file
2: C:\just\a\long\path\to\a\file\in\the\file
3: C:\just\a\long\path\to\a\file\in\the\file\system\filenot\file
EDIT: An even simpler method with the aid of some magic:
@echo off
setlocal EnableDelayedExpansion
set aPath=C:\just\a\long\path\to\a\file\in\the\file\system\filenot\file\folder
set aDir=file
echo %aPath%
set n=%1
set "result="
set "p=%aPath:\=" & (if !n! gtr 0 set "result=!result!\!p!") & (if "!p!" equ "!aDir!" set /A n-=1) & set "p=%"
echo %1: %result:~1%
Output example:
C:\Tests> test 1
C:\just\a\long\path\to\a\file\in\the\file\system\filenot\file\folder
1: C:\just\a\long\path\to\a\file
C:\Tests> test 2
C:\just\a\long\path\to\a\file\in\the\file\system\filenot\file\folder
2: C:\just\a\long\path\to\a\file\in\the\file