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

后端 未结 6 1135
闹比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:29

    You can use the modifiers ability of the FOR variables.

    for %%Q in ("%~dp0\.") DO set "BuildDir=%%~fQ"
    

    The %%~fQ results to the required path without trailing backslash (nor \.)

    And this creates still a valid absolute path for the case, when the batch file is in a root directory on any drive.

    This solution returns D:\, instead of D: only by simply removing the trailing character.

提交回复
热议问题