Suppose I wish to get the absolute path of a batch script from within the batch script itself, but without a trailing backslash. Normally, I do it this way:
SET BuildDir=%~dp0 SET BuildDir=%BuildDir:~0,-1%
The first statement gets the path with the trailing backslash and the second line removes the last character, i.e. the backslash. Is there a way to combine these two statements into a single line of code?
So %%k refers to the value of the 3rd token, which is what is returned.
Instead of removing the trailing backslash, adding a trailing dot is semantically equivalent for many software.
C:\Windows is equivalent to C:\Windows\.
echo %dp0 >C:\Windows\ echo %dp0. >C:\Windows\.
This errors out:
robocopy "C:\myDir" %~dp0
This is successful:
robocopy "C:\myDir" %~dp0.
Only with delayed expansion when you write both statements into the same line:
set BuildDir=%~dp0&&set BuildDir=!BuildDir:~0,-1!
But that kinda defies the purpose.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With