CMD: failure of %~d0 when CALL quotes the name of the batch file

后端 未结 4 2178
遥遥无期
遥遥无期 2021-01-03 02:43

Why the following failure of %~d0 to return the batch file\'s drive letter S: when CALL quotes the name of the batch file?

S:\\!DJ DAP>type test.b         


        
4条回答
  •  轮回少年
    2021-01-03 03:11

    Like dbenham: Fascinating!

    I suppose it's a feature of cmd.exe.

    Quotes aren't stripped from %0 in the main batch context.
    But they are all stripped by a call to a subroutine. This can be realized when more than two quotes are used, only one quote from each side will be removed when the %0 is expanded.

    ParamTest.bat

    @echo off
    cls
    setlocal
    d:
    echo Main %%0: %~0, %~f0
    echo Main %%1: %~1, %~f1
    call :func %1
    exit /b
    
    :func
    echo Func %%0: %~0, %~f0
    echo Func %%1: %~1, %~f1
    exit /b
    

    Output for: """"PARAM"test.BAT" ""paramTEST.bAt""

    Main %0: """PARAM"test.BAT, D:\"""PARAM"test.BAT
    Main %1: "paramTEST.bAt", D:\"paramTEST.bAt"
    Func %0: :func, C:\temp\ParamTest.bat
    Func %1: "paramTEST.bAt", D:\"paramTEST.bAt"
    

    And %0 seems to save the related directory, as you get different results for %~f0 and %~f1 even when the content seems to be equal.
    But perhaps the path is just prefixed to %0.

    Output for: PARAMtest.BAT paramTEST.bAt

    Main %0: PARAMtest.BAT, C:\temp\ParamTest.bat
    Main %1: paramTEST.bAt, D:\paramTEST.bAt
    Func %0: :func, C:\temp\ParamTest.bat
    Func %1: paramTEST.bAt, D:\paramTEST.bAt
    

提交回复
热议问题