Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In batch files, as in standard C programs, argument 0 contains the path to the currently executing script. You can use <code>%~dp0</code> to get only the path portion of the 0th argument (which is the current script) - this path is always a fully qualified path.</p> <p>You can also get the fully qualified path of your first argument by using <code>%~f1</code>, but this gives a path according to the current working directory, which is obviously not what you want. </p> <p>Personally, I often use the <code>%~dp0%~1</code> 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.</p> <p>If you need to support both relative <em>and</em> absolute paths, you can make use of <a href="https://stackoverflow.com/questions/1645843/batch-file-resolve-absolute-path-from-relative-path-and-or-file-name/4488734#4488734">Frédéric Ménez's solution</a>: temporarily change the current working directory. </p> <p>Here's an example that'll demonstrate each of these techniques:</p> <pre><code>@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 </code></pre> <p>If you save this as c:\temp\example.bat and the run it from c:\Users\Public as</p> <p>c:\Users\Public>\temp\example.bat ..\windows</p> <p>...you'll observe the following output:</p> <pre><code>%~dp0 is "C:\temp\" %0 is "\temp\example.bat" %~dpnx0 is "C:\temp\example.bat" %~f1 is "C:\Users\windows" %~dp0%~1 is "C:\temp\..\windows" batch-relative %~f1 is "C:\Windows" </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload