Creating batch files that are relocateable and can be run from anywhere.

Use %~dp0 to get the path a batch file is located in. This is useful if you want to access resources that are stored relative to the batch file. This allows you to get around the fact that the current directory for a batch file is the directory you ran it from, not where the batch file is located.

An example would be,

@echo off
call %~dp0program_locations.bat

This would call another script in the same directory as the batch file called program_locations.bat. There is no slash between the two bits because the path returned by %~dp0 has a trailing slash already.

There are a whole bunch of other things you can do with %~ but that’s the one I tend to use the most often. The d stands for drive and the p for path. The overall command is working on %0, the program being run.

Advertisements

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 )

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