How to get the path of a batch script without the trailing backslash in a single command?

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\.

For example, robocopy accepts only directories without trailing spaces.

This errors out:

robocopy "C:\myDir" %~dp0 

This is successful:

robocopy "C:\myDir" %~dp0.

Leave a Comment