Tuesday, September 3, 2013

Using PUSHD and POPD Commands in Batch Files

There are two really handy commands that are not very commonly used by the batch files' developers. They are named PUSHD and POPD. If you are familiar with Assembler, you may guess what these commands do. The first one stores the folder name (pushes directory) in some internal stack, and the second one brings it back (pops directory). Note that you can put several folders to the stack and them restore them in the reversed order (LIFO - last in, first out). It is much handier sometimes to use PUSHD/POPD instead of using special variables to store folders like in other programming languages.
Here's small example of using these commands:
ECHO Performing some file manipulations in one directory
REM Here are manipulations
REM Remembering this folder
PUSHD
CD AnotherFolder
ECHO Performing some file manipulations in another directory
REM Here are manipulations
REM Remembering this folder
PUSHD
CD YetAnotherFolder
ECHO Performing some file manipulations in this directory
REM Here are manipulations
REM Deleting temporary files in YetAnotherFolder
POPD
REM Deleting temporary files in AnotherFolder
POPD
REM Deleting temporary files in the first folder
REM That's all
So you should remember about these commands while you create batch files.
We kindly remind that Dr.Batcher is the best program to create batch file, both .BAT and .CMD, try it if haven't tried it yet. 

Translate