Can a Windows batch file determine its own file name?

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

    Try to run below example in order to feel how the magical variables work.

    @echo off
    
    SETLOCAL EnableDelayedExpansion
    
    echo Full path and filename: %~f0
    echo Drive: %~d0
    echo Path: %~p0
    echo Drive and path: %~dp0
    echo Filename without extension: %~n0
    echo Filename with    extension: %~nx0
    echo Extension: %~x0
    
    echo date time : %~t0
    echo file size: %~z0
    
    ENDLOCAL
    

    The related rules are following.

    %~I         - expands %I removing any surrounding quotes ("")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string
    

提交回复
热议问题