In batch files, as in standard C programs, argument 0 contains the path to the currently executing script. You can use %~dp0
to get only the path portion of the 0th argument (which is the current script) - this path is always a fully qualified path.
You can also get the fully qualified path of your first argument by using %~f1
, but this gives a path according to the current working directory, which is obviously not what you want.
Personally, I often use the %~dp0%~1
idiom in my batch file, which interpret the first argument relative to the path of the executing batch. It does have a shortcoming though: it miserably fails if the first argument is fully-qualified.
If you need to support both relative and absolute paths, you can make use of Frédéric Ménez's solution: temporarily change the current working directory.
Here's an example that'll demonstrate each of these techniques:
@echo off
echo %%~dp0 is "%~dp0"
echo %%0 is "%0"
echo %%~dpnx0 is "%~dpnx0"
echo %%~f1 is "%~f1"
echo %%~dp0%%~1 is "%~dp0%~1"
rem Temporarily change the current working directory, to retrieve a full path
rem to the first parameter
pushd .
cd %~dp0
echo batch-relative %%~f1 is "%~f1"
popd
If you save this as c:empexample.bat and the run it from c:UsersPublic as
c:UsersPublic>empexample.bat ..windows
...you'll observe the following output:
%~dp0 is "C:emp"
%0 is "empexample.bat"
%~dpnx0 is "C:empexample.bat"
%~f1 is "C:Userswindows"
%~dp0%~1 is "C:emp..windows"
batch-relative %~f1 is "C:Windows"
the documentation for the set of modifiers allowed on a batch argument can be found here:
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/call
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…