Quicktip: Windows 7 Batch File – Current Working Directory

Windows Current (Working) Directory
When you are running a script in Windows command line (old DOS prompt), you have 2 paths to deal with:
Current (Working) directory and
Path of the script you are running.

Most of the time, they may be the same. Sometimes, you would change working directory to a different path, so you can pick up files/scripts from there. If you ever set up a shortcut to a batch file, you will see a Start in Folder – this corresponds to the current working directory. This can be different from actual path of the script.

To change directory in Windows command line, you can use the CD command. This command affects the current working directory. Windows stores the current working directory in a special variable called %CD%. This variable can be used both on command line and in batch files.

To save the Current working directory, before running a step in a batch file (like calling another script), you can use PUSHD. This simply saves the current path in a stack. Upon return, you can simply do a POPD operation to restore the current working directory.

Windows 7 Default for Current (Working) Directory
Windows 7 sometimes defaults current working directory to C:\Windows\System32 (%Windir%\System32) in command prompt (This incidentally is the path of the cmd.exe). So, if you have some old Batch File scripts that ran fine earlier and suddenly fail to find programs/scripts, chances are, your script is suffering from the above defaulting behavior.

To overcome this, I add the below lines to the top of all my scripts, to force the current

SET SCRIPT_DIR=%~dp0
REM Windows 7 may default to C:\Windows\System32, so force back to the script directory
CD /d %SCRIPT_DIR%

The above snippet forces the current working directory back to the directory where the script is run from. I could have simply said cd /d %~dp0, but I save it in a variable that I can use elsewhere in the script.

Script Arguments and Modifiers

The weird syntax with tilde (~), in the above code snippet, is a special modifier to dissect paths in a Windows Batch File.

If you want to get the full path of a script, you can use the variable %0. For e.g., if you are running a script, c:\dev\test.bat, then

echo %0

prints c:\dev\test.bat

If you want to get parts of this path string, then you would use modifiers. %~p0 refers to the path without the drive letter of the %0.

echo %~p0

prints \dev\test.bat

To get the Drive letter alone, you can use %~d0. This will print C:

You can combine the 2 using %~dp0. This gives the drive letter and path (minus the file name).
(To get fully quality path name use f instead of p).

You can also get just the file name, by using the modifier n and x to get the extension. So to print only name of the script (minus the directory it is in), use %nx0.

%0 refers to the script being run. %1 – %9 are the command line arguments (if any) passed into this scripts. You can use the modifiers on these arguments the same way as well. For e.g., if you have a command like,

process_data.bat c:\data\datafile.dat

%0 refers to process_data.bat
%1 refers to c:\data\datafile.dat

%dp1 would then give us c:\data, the directory in which data file is located and %nx1 will give you datafile.dat.

Unlike the CD command, getting the parts of the path of a script/file can only be done inside the script. These cannot be run on command line as such.

There is a lot more to parameters and modifiers. See here , here , here for details.

Advertisements

Comments, please?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s