This works when the file is called from the same directory where it it is placed:
@ECHO OFF
set _MyCurrentPath=%CD%
pushd ..
set _Level1UpDir=%CD%
ECHO _Level1UpDir: %_Level1UpDir%
pushd ..
set _Level2UpDir=%CD%
ECHO _Level2UpDir: %_Level2UpDir%
pushd ..
set _Level3UpDir=%CD%
ECHO _Level3UpDir: %_Level3UpDir%
PAUSE
Yet how-to get the n-th level up just from the %0 … it would requre some fancy call with for /f %%in
My strategy was to count the number of backslashes and the a second for loop as follows:
SET count=1
FOR /f “tokens=1-4 delims=.” %%G IN (‘echo %0’) DO (call :subroutine “%%G”)
GOTO :eof
:subroutine
echo %count%:%1
set /a count+=1
pause
GOTO :eof
What’s wrong with the path
.\..\..\..(repeat..as much as you like, in a loop) ?Here is batch code to repeat a string N times
If you need the fully expanded path, you could try tilde-expansion. Eg:
%~dp0will be%0expanded to a full path.I often put
CD %~dp0near the top of my batch files if they contain paths relative to the batch file itself.Thus you could use
%~dp0\..\..\..to get the full path and then 3 up from there.