Can a Windows batch file determine its own file name?

后端 未结 6 552
死守一世寂寞
死守一世寂寞 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:33

    You can get the file name, but you can also get the full path, depending what you place between the '%~' and the '0'. Take your pick from

    d -- drive
    p -- path
    n -- file name
    x -- extension
    f -- full path
    

    E.g., from inside c:\tmp\foo.bat, %~nx0 gives you "foo.bat", whilst %~dpnx0 gives "c:\tmp\foo.bat". Note the pieces are always assembled in canonical order, so if you get cute and try %~xnpd0, you still get "c:\tmp\foo.bat"

提交回复
热议问题