Tuesday, April 23, 2013

Save and restore current directory in .BAT files

Current directory can be saved and restored in batch files with the help of %CD% pseudo-variable. At run-time %CD% expands to the current working directory.

@echo off

rem Save current directory
set curdir=%cd%
echo %cd%

rem Change current directory
cd ..\..
echo %cd%

rem Restore current directory
cd %curdir%
echo %cd%
Code above produces the following output:
C:\Users\vurdalakov\github\code examples
C:\Users\vurdalakov
C:\Users\vurdalakov\github\code examples

No comments:

Post a Comment