The PUSHD/POPD Implementation in Pure Windows Batch

  • 时间:2020-09-16 12:48:17
  • 分类:网络文摘
  • 阅读:72 次

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) —

推荐阅读:
wordpress技巧:为标签链接添加rel=”nofollow”属性  wordpress 4.0 新增自定义图标插件安装  WordPress在线文件管理插件:FileBrowser  设置wordpress文章标题高亮的代码  免插件实现WordPress文章置顶的方法  让新浪微博变身外链图床的wordpress插件:微博图床  三款自动翻译文章标题为英文的wordpress插件介绍与比较  WodrPress实现远程图片本地化插件-Auto_Save_Image  碧沙岗公园作文  世界的奇迹作文700字 
评论列表
添加评论