Below is a script example that can be used in batch files to determine where the file you are running lives. This is useful when writing scripts for moving or installing files where you need to know the current working drive and or directory as well.
Here is a simple batch/Script that will show the above data:
echo Current location of this file and its status:
echo Current dir = %CD%
Echo Script dir = %~dp0
echo Batch File Path (where it lives and its name) = %0
echo Current Drive = %~d0
echo Path to batch file = %~p0
If you run the above batch file with the name "Dos Commands that display drive and directory where a file lives.cmd" as we did you will get this output:
Current location of this file and its status:
Current dir = C:\download\OS\Win Tips
Script dir = C:\download\OS\Win Tips\
Batch File Path (where it lives and its name) = "C:\download\OS\Win Tips\Dos commands that display drive and directory where a file lives.cmd"
Current Drive = C:
Path to batch file = \download\OS\Win Tips\
Press any key to continue . . .
Now that you see how these variables work in a batch it will be easy to put them to use.
Thanks for visiting mswintips.com.
Return to Index Page
Copyright ©2018 CSN All Rights Reserved