Can a Windows batch file determine its own file name?

后端 未结 6 550
死守一世寂寞
死守一世寂寞 2020-12-07 11:16

Can a Windows batch file determine its own file name?

For example, if I run the batch file C:\\Temp\\myScript.bat, is there a command within myScript.bat that can de

6条回答
  •  攒了一身酷
    2020-12-07 11:45

    Using the following script, based on SLaks answer, I determined that the correct answer is:

    echo The name of this file is: %~n0%~x0
    echo The name of this file is: %~nx0
    

    And here is my test script:

    @echo off
    echo %0
    echo %~0
    echo %n0
    echo %x0
    echo %~n0
    echo %dp0
    echo %~dp0
    pause
    

    What I find interesting is that %nx0 won't work, given that we know the '~' char usually is used to strip/trim quotes off of a variable.

提交回复
热议问题