yakk0.org a blog on technology in the real world

22Apr/100

Path variables in batch files

This is another post documenting something I used to fix a problem at work so I don’t forget about it. We were pushing out Firefox to all our systems yesterday via SCCM. It’s pushed out using a batch file to configure various things, and I noticed that although the batch file was being called from the dozen or so distribution points we have, the script was calling back to the main server. I’ve seen this before but never looked into a solution. I started searching for a variable or a way to get the path the script is run from and found this article posted in 2008 on myITforum.com. It is addressing the exact situation I was encountering.

In short I needed to use the variable %~dp0 before the items on the distribution point share. For example I changed “\\SCCM\packages\MozillaFirefox\Firefox Setup 3.6.3.exe” in the script to “%~dp0\Firefox Setup 3.6.3.exe.” I tested it with a test script that echoed out the commands instead of running them, and the paths were correct.

The myITforum article goes into further detail about the other uses of this variable. It’s actually a modification of the %0, %1, %2 convention of dos batch files for referencing command-line arguments. %0 references the script itself, including the full drive and path, %1 is the first argument, and %2 is the second, and so on. The “~d” modifier before the number 0 resolves the variable to the drive letter only and “~p” would resolve it to the path only. Combining the two into  “~dp” resolves it into drive and the path, or in this case the full UNC path. More information can be found by typing “FOR /?” from a command prompt.

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

(required)

No trackbacks yet.