- 1 year ago
How do I get a relative path out of a windows batch file using echo?
Comment
1 / 1
To get a relative path out of a Windows batch file using the echo
command, you can use the %~dp0
variable. This variable represents the drive letter and the folder path of the currently running batch file.
Here's an example of how you can use it:
batch@echo off
setlocal
rem Get the relative path of the batch file
set "batchPath=%~dp0"
echo The relative path of the batch file is: %batchPath%
endlocal
When you run this batch file, it will display the relative path of the batch file itself.
Note that the %~dp0
variable includes a trailing backslash. If you want to remove the trailing backslash, you can use the %batchPath:~0,-1%
syntax:
batchecho The relative path of the batch file is: %batchPath:~0,-1%
This will remove the trailing backslash from the relative path.
By using the %~dp0
variable, you can easily get the relative path of the batch file and use it in your script as needed.