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

后端 未结 6 1244
闹比i
闹比i 2021-02-02 07:49

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          


        
6条回答
  •  天命终不由人
    2021-02-02 08:12

    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.
    

提交回复
热议问题