If you put a test.bat file in your desktop of your Windows 10 machine, then you can call it by simply writing test in the address bar of Windows directory explorer.
This behavior is used by cmd command to start a command line in any custom directory as described here.
Now that my test.bat is working like cmd I want to know where my batch file is called from. I mean the directory where the test command is invoked in it's address bar.
1 Answer
Batch scripts have a few variables they can use to get special information about the script.
%1 to %9 are the parameters that were used after calling the batch script, but %0 is the full path including the filename itself of the batch file.
So if the batchfile is stored on the desktop, %0 would be:
c:\users\user\Desktop\test.batGiven that %0 gives us this information, we can use special operators to extract the path.
The variable you are looking for is %~dp0.
In order to store this in a variable and work with it later, you can use the following command:
set scriptpath=%~dp0
echo %scriptpath% :: will output c:\users\user\Desktop 0