S h o r t S t o r i e s

// Tales from software development


leave a comment »

Batch file enhancements

Batch files. Can’t live with them, can’t live without them.

Despite all the options available today for ad hoc programming under Windows sometimes a quick and dirty batch file really is the best option.

There are some interesting enhancements that Microsoft has implemented over the years that many people who only occasionally use batch files, like me, are probably unaware of. A couple that I think are particularly useful are the %0 parameter and the syntax that can be used to modify the way that a value is substituted for a batch parameter.

The %0 parameter

You probably remember that any command line parameters passed to a batch file are accessed using %1, %2, %3, etc. What you might not know is that %0 is also implemented and, like arg[0] in C, represents the executable itself. Or, in this case, the batch file.

Having access to the name of the file that’s executing can sometimes be useful but it’s the way that batch parameter substitution can be modified that makes it really useful.

Batch parameter substitution

If a batch parameter is a file path then there is a syntax that allows you to extract exactly the parts of the path that you want:

If %0 is the path (including filename) of the batch file that’s executing then %~d0 is the drive specifier from the path, %~p0, is the path component, %~n0 is the filename, etc.

These modifiers can be combined so that, for example, %~dp0 is the drive and path of the batch file that is currently executing. Now this is useful! For example, it used to be a problem if you wanted your batch file to write to a log file in the same folder as the batch file. If you used a relative path for the log file then it would only work correctly when the current working directory was the directory where the batch file and log file were located. The only way around this was to hard code the full path to the log file. But then if you moved the batch file, the log file would still be written to in the original location if you forgot to update the path to the log file. But you can use %~dp0 in the log file path:

ECHO Starting backup >>%~dp0Backup.log

This syntax and the modifiers are documented on MSDN but you can also get information by typing Help Call or Call /? at a command window prompt. There is some other goodness too:

%* represents all batch parameters

%~0 is the path with quotes removed

%~$PATH:0 searches your PATH for the named file and returns the path of the matching file. For example, if you called a batch file with notepad.exe as a parameter, then %~$PATH:1 will usually return C:\Windows\System32\notepad.exe. The modifier syntax can also be used, so %~dp$PATH:1 will return C:\Windows\System32\.


Written by Sea Monkey

November 19, 2008 at 8:30 pm

Leave a Reply

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: