If you are creating batch (.bat or .cmd) files you will run into an issue where you want to be able to determine the relative location of that script and then affect those other files.
A batch file uses %0 as the parameter for the batch file name. Using %~ we can expand parameters. Thus, when we combine a few parameters together we can create a way to read the current directory that the script is stored in.
%~dp0
d=drive letter
p=path
Thus, if the file c:\folder1\folder2\script.cmd exists and has the following contents:
echo %~dp0
We will get the following output:
c:\folder1\folder2\
Pay attention to the fact that the output contains the trailing backslash "\". This is important because if you are going to reference another file in that directory you'll need to not use an additional "\".
If script.cmd is used to call script2.cmd in the same directory and also call script3.cmd in a subdirectory it would look something like this.
Example:
set scriptlocation=%~dp0
call %scriptlocation%script2.cmd
call %scriptlocation%folder3\script3.cmd
For an in-depth explanation:
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true
No comments:
Post a Comment