- 1 year ago
How do I get the path of a batch-file
To retrieve the path of a batch file within the batch script itself, you can use the %~dp0
parameter expansion. Here's an example:
batch@echo off
REM Get the path of the batch file
set "batchFilePath=%~dp0"
echo The batch file is located at: %batchFilePath%
pause
In this example, %~dp0
expands to the drive letter and the directory path of the batch file, including a trailing backslash. By assigning %~dp0
to the batchFilePath
variable, you can reference it later in the script or display it using %batchFilePath%
.
For instance, if the batch file is located in C:\Scripts\MyBatchFile.bat
, running the above script would output:
csharpThe batch file is located at: C:\Scripts\
Using %~dp0
ensures that the batch file's path is correctly resolved, even if the batch file is run from a different directory or if the path contains spaces.
Note that %~dp0
refers to the path of the batch file itself, not the current working directory. If you need to retrieve the current working directory within the batch script, you can use the %CD%
environment variable.