• Batch


    %~dp0 简易解释

    The variable %0 in a batch script is set to the name of the executing batch file. The ~dp special syntax between the % and the 0 basically says to expand the variable %0 to show the drive letter and path, which gives you the current directory containing the batch file!

    %~dp0 只表示将要“运行的”bat命令的folder,不包含bat名称自己。注意,不是“运行处”的folder (该功能用%cd%实现)。详细的区别,查看Batch - %~dp0 vs %cd%

    %~dp0 详细解释

    The %~dp0 (that’s a zero) variable when referenced within a Windows batch file will expand to the drive letter and path of that batch file.

    The variables %0-%9 refer to the command line parameters of the batch file. %1-%9 refer to command line arguments after the batch file name. %0 refers to the batch file itself.

    %0-%9代表的是batch文件的参数。%1-%9 是batch名称之后的命令行参数,%0代表batch文件自己。

    If you follow the percent character (%) with a tilde character (~), you can insert a modifier(s) before the parameter number to alter the way the variable is expanded. The d modifier expands to the drive letter and the p modifier expands to the path of the parameter.

    Example: Let’s say you have a directory on C: called bat_files, and in that directory is a file called example.bat. In this case, %~dp0 (combining the d and p modifiers) will expand to C:at_files

    例子

    a BAT file at the innermost level:

    C:
      dir
        my files
          run.bat
    

    The BAT file contains the following lines:

    @pushd %~dp0
    @echo %~dp0
    @popd
    

    If I run the BAT file from a command prompt whose current directory is C:dirmy files, then I get a very reasonable result:

    C:dirmy files>run.bat
    C:dirmy files
    

      

    Modifiers 语法

    In addition, substitution of FOR variable references has been enhanced. You can now use the following optional syntax:

    目前,对FOR 命令做了增强,可以加入单个/多个modifiers, 对%0~%9的参数进行增强。

    下面是单个modifiers语法,大写的I代表第几个参数:

    %~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 

    modifiers也可以组合使用:

    %~dpI       - expands %I to a drive letter and path only
    %~nxI       - expands %I to a file name and extension only
    %~fsI       - expands %I to a full path name with short names only
    %~dp$PATH:I - searches the directories listed in the PATH
                   environment variable for %I and expands to the
                   drive letter and path of the first one found.
    %~ftzaI     - expands %I to a DIR like output line
    

    In the above examples %I and PATH can be replaced by other valid values. The %~ syntax is terminated by a valid FOR variable name. Picking upper case variable names like %I makes it more readable and avoids confusion with the modifiers, which are not case sensitive.

    There are different letters you can use like f for "full path name"d for drive letterp for path, and they can be combined. %~ is the beginning for each of those sequences and a number I denotes it works on the parameter %I (where %0 is the complete name of the batch file, just like you assumed).  

  • 相关阅读:
    tbusers
    Dism解决win 10访问服务器共享问题,共享需要过时的SMB1协议,安装时错误代码:0x800736B3
    &【03】Python 变量与数据类型概览
    & 【01】 Spring源码编译
    &【02】Python 基础语法
    linux出现 -bash: fork: Cannot allocate memory 问题
    并发编程-15 ThreadPoolExecutor线程池
    Spring事务嵌套问题记录
    多线程笔试题记录
    & IDEA Debug启动失败或启动慢
  • 原文地址:https://www.cnblogs.com/frankcui/p/11655342.html
Copyright © 2020-2023  润新知