The PUSHD/POPD Implementation in Pure Windows Batch


PUSHD/POPD is a great pair of tools that exist in modern operating system e.g. Windows, Linux or MAC. It allows you to jump forward and backwards between directories. The pushd takes a directory, before jumping, it will push the current directory to the stack, which we can popd later (restoring to previous directory). For example:

1
2
3
4
5
6
? pwd
/root
? pushd /abc
/abc
? popd
/root
? pwd
/root
? pushd /abc
/abc
? popd
/root

We are now implementing the PUSHD/POPD using Windows Batch Programming. You could save the following windows scripts using .cmd or .bat extensions. Even it has been provided on Modern windows command shell, it is still a good idiom/exercise to do so.

PUSHD.bat (Windows Batch Script)

The idea is to store the stack using the environment variable. We can store a stack index pointer for example, when PUSHD_COUNT environment variable is set to 1, the top of the stack (directory string) is stored in PUSHD0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@echo off
if [%1] == [] (
    echo pushd: no other directory
    goto :eof
)
set TMP=%cd%
if [%TMP%] == [] (
    for /f %%f in ('cd') do (
        set TMP=%%f
    )
)
cd /d "%1" 2> nul
if [%errorlevel%] == [1] (
    echo pushd: %1: No such file or directory
    goto :eof
)
if [%PUSHD_COUNT%] == [] (
    set PUSHD_COUNT=0
)
set PUSHD%PUSHD_COUNT%=%TMP%
set /a PUSHD_COUNT=%PUSHD_COUNT+1
@echo off
if [%1] == [] (
    echo pushd: no other directory
    goto :eof
)
set TMP=%cd%
if [%TMP%] == [] (
    for /f %%f in ('cd') do (
        set TMP=%%f
    )
)
cd /d "%1" 2> nul
if [%errorlevel%] == [1] (
    echo pushd: %1: No such file or directory
    goto :eof
)
if [%PUSHD_COUNT%] == [] (
    set PUSHD_COUNT=0
)
set PUSHD%PUSHD_COUNT%=%TMP%
set /a PUSHD_COUNT=%PUSHD_COUNT+1

When we push a new directory using PUSHD, we first store the current directory string in environment variable PUSHD%PUSHD_COUNTER% then we increment the counter %PUSHD_COUNTER%.

To get the current directory in the Windows command shell, we can read the content from environment variable %CD%. In the ancient Windows System, when this is not set, you can use the following to parse the output of the CD command (into %CD% environment variable), which prints the current directory when no argument is given.

1
2
3
4
5
if [%CD%] == [] (
    for /f %%f in ('cd') do (
        set CD=%%f
    )
)
if [%CD%] == [] (
    for /f %%f in ('cd') do (
        set CD=%%f
    )
)

POPD.bat (Windows Batch Script)

Popd pops the directory string from the stack, and change to it by CD or CHDIR.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@echo off
if [%PUSHD_COUNT%] == [] (
    echo popd: directory stack empty
    goto :eof
)
set /a PUSHD_COUNT=%PUSHD_COUNT-1
set TMP=PUSHD%PUSHD_COUNT%
 
set TMPFILE=%RANDOM%.tmp
 
setlocal enabledelayedexpansion
if [!%TMP%!] == [] (
    echo popd: directory stack empty
    goto :eof
)
set TMPDIR=!%TMP%!
rem endlocal will erase the chdir and therefore 
rem saving the file in a temp file
echo %TMPDIR% > %TMPFILE%
endlocal
 
rem reading the directory from a file
set /p TMP=<%TMPFILE%
del /f %TMPFILE%
chdir /d "%TMP%"
 
set PUSHD%PUSHD_COUNT%=
@echo off
if [%PUSHD_COUNT%] == [] (
    echo popd: directory stack empty
    goto :eof
)
set /a PUSHD_COUNT=%PUSHD_COUNT-1
set TMP=PUSHD%PUSHD_COUNT%

set TMPFILE=%RANDOM%.tmp

setlocal enabledelayedexpansion
if [!%TMP%!] == [] (
    echo popd: directory stack empty
    goto :eof
)
set TMPDIR=!%TMP%!
rem endlocal will erase the chdir and therefore 
rem saving the file in a temp file
echo %TMPDIR% > %TMPFILE%
endlocal

rem reading the directory from a file
set /p TMP=<%TMPFILE%
del /f %TMPFILE%
chdir /d "%TMP%"

set PUSHD%PUSHD_COUNT%=

We first decrement the %PUSHD_COUNTER% stack pointer. If the counter variable is not found, we exit the script with message popd: directory stack empty. As we are evaluating a environment variable and the name is formed by another environment name, we need to use the !%NAME%! within the local scope of setlocal enabledelayedexpansion to do so.

One side-effect of setlocal is that it will erase/restore any states/changes after endlocal thus changing the directory within the local scope isn’t working. And the only way to pass the directory string outside the scope is to write it to the file, e.g. the %RANDOM% temporary file.

We can use the following batch syntax to read a file content and save it to an environment variable using the set /p syntax.

1
set /p NAME=<file.txt
set /p NAME=<file.txt

Remember to delete the temp file and clear the content of PUSHD%PUSHD_COUNT%. Also, we pass the /D parameter to allow the CD command to jump to a directory with a different drive letter e.g. from C: to D:

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
701 words
Last Post: Three ways to Reverse a List/Array/Tuple in Python
Next Post: Algorithm to Find the Winner on a Tic Tac Toe Game

The Permanent URL is: The PUSHD/POPD Implementation in Pure Windows Batch

Leave a Reply