JSI Tip 8025. How many levels have I Pushd into the stack?

The Pushd command stores the name of the current directory for use by the Popd command, before changing the current directory to the specified directory.

I have scripted PushdLevel.bat to return the current number of levels you have pushed into the stack.

The syntax for using PushdLevel.bat is:

call pushdlevel level

Where level is a call directed numeric environment variable that will contain the Pushd level, from 0 (none) through N.

NOTE: See 'CMD does not support UNC paths as current directories'?

PushdLevel.bat contains:

@echo off
If \{%1\}==\{\} @echo Syntax PushdLevel Level&exit /b 1
set /a %1=0
if exist "%TEMP%\%~n0.TMP" del /q "%TEMP%\%~n0.TMP"
pushd >"%TEMP%\%~n0.TMP"
for /f "Tokens=1 Delims=:" %%L in ('type "%TEMP%\%~n0.TMP"^|findstr /v /n /c:"\[$#$\]"') do (
     set /a %1=%%L
del /q "%TEMP%\%~n0.TMP"
exit /b 0

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.