The %~dp0 piece was mentioned already, but there is actually more to it: the character(s) after the ~ define the information that is extracted.
No letter result in the return of the patch file name
d - returns the drive letter
p - returns the path
s - returns the short path
x - returns the file extension
So if you execute the script test.bat below from the c:\Temp\long dir name\ folder,
@echo off echo %0 echo %~d0 echo %~p0 echo %~dp0 echo %~x0 echo %~s0 echo %~sp0
you get the following output
test c: \Temp\long dir name\ c:\Temp\long dir name\ .bat c:\Temp\LONGDI~1\test.bat \Temp\LONGDI~1\
And if a parameter is passed into your script as in
test c:\temp\mysrc\test.cpp
the same manipulations can be done with the %1 variable.
But the result of the expansion of %0 depends on the location!
At the "top level" of the batch it expands to the current batch filename.
In a function (call), it expands to the function name.
@echo off echo %0 call :test goto :eof :test echo %0 echo %~0 echo %~n0
The output is (the batchfile is started with myBatch.bat )
myBatch.bat :test :test myBatch