• 批处理参考


    echo %~dp0 当前路径

    echo %~n0 当前文件名

    %~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
    The modifiers can be combined to get compound results:
    %~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

    for example: 利用for循环查找e盘下Floder文件夹里面的子文件夹(转)

    1     @echo off
    2     SETLOCAL ENABLEDELAYEDEXPANSION
    3     for /r "E:Folder" /d %%i in (*) do (
    4     set FolderPath=%%i
    5     @echo !FolderPath!
    6     set folderName=%%~ni
    7     @echo !folderName!


        其中参数 %%i 是获取子文件全路径
        其中参数 %%~ni 是获取子文件名称
        
        还有表示方法
        其中参数 %%~pnxi

        p代表路径,n代表文件名,x代表后缀,组合起来就是去掉了驱动器号

        你如果用%%~i 也表示全路径

    关于%~dp0的批处理命令的详细解释
        对此命令并不清楚,以下内容都来自百度搜索结果:
        %~dp0 “d”为Drive的缩写,即为驱动器,磁盘、“p”为Path缩写,即为路径,目录
        cd是转到这个目录,不过我觉得cd /d %~dp0 还好些
        选项语法:
        ~0 - 删除任何引号("),扩充 %0
        %~f0 - 将 %0 扩充到一个完全合格的路径名(“f”是file,即文件)
        %~d0 - 仅将 %0 扩充到一个驱动器号
        %~p0 - 仅将 %0 扩充到一个路径
        %~n0 - 仅将 %0 扩充到一个文件名(“n”是name 文件名)
        %~x0 - 仅将 %0 扩充到一个文件扩展名
        %~s0 - 扩充的路径只含有短名(“s”为Short,短的)
        %~a0 - 将 %0 扩充到文件的文件属性(“a”为attribute,即属性)
        %~t0 - 将 %0 扩充到文件的日期/时间(“t”time)
        %~z0 - 将 %0 扩充到文件的大小(Size 大小)

        %~$PATH:0 - 查找列在路径环境变量的目录,并将 %0 扩充到找到的第一个完全合格的名称。如果环境变量名未被定义,或者没有找到文件,此组合键会扩充到空字符串,可以组合修饰符来得到多重结果:

        %~dp0 - 仅将 %0 扩充到一个驱动器号和路径
        %~nx0 - 仅将 %0 扩充到一个文件名和扩展名
        %~fs0 - 仅将 %0 扩充到一个带有短名的完整路径名
        %~dp$PATH:0 - 查找列在路径环境变量的目录,并将 %I 扩充到找到的第一个驱动器号和路径。
        %~ftza0 - 将 %0 扩充到类似输出线路的 DIR
        %0为当前批处理文件,如果0换成1为第一个文件,2为第2个
        *********************************************************************************************
        %0             代指批处理文件自身
        %~d0        是指批处理所在的盘符
        %~dp0      是盘符加路径
        
        cd %~dp0 就是进入批处理所在目录了

    --------------------------------
    测试路径:D:gongju ginxstop.bat

    -------------------------------

    批处理文件中获取当前所在路径的几种方法

    @echo off
    setlocal EnableDelayedExpansion
    echo 当前正在运行的批处理文件所在路径:!cd!
    pause
    
    @echo off
    
    echo 当前目录是:%cd%
    
    pause
    
    @echo off
    
    :: set "abc=%cd%"
    echo 当前正在运行的批处理文件所在路径:%~dp0
    
    pause
    
    @echo off
    echo 当前的盘符及路径:%~dp0
    echo 当前的盘符及路径的短文件名格式:%~sdp0
    pause
    
    @echo 取当前文件的上级目录的名称
    set a=%cd%
    for %%a in ("%a%") do (
            set ok=%%~dpa
            for /f "delims=" %%b in ("!ok:~0,-1!") do (
                    echo %%~nb
            )
    )
    pause

    --------------------------------
    测试路径:D:gongju ginxstop.bat

    %0,批处理文件自身,D:gongju ginxstop.bat             

    %~0,全路径,D:gongju ginxstop.bat          

    %~f0,(file)全路径,D:gongju ginxstop.bat             

    %~d0,(dir)盘符,D:              

    %~p0,(path)路径-无盘符,gongju ginx           

    %~n0,(name)文件名称,stop              

    %~x0,(exe)后缀名,.bat              

    %~s0,(Short)全路径,D:gongju ginxstop.bat                 

    %~a0,(attribute)文件属性,--a------            

    %~t0,(time)文件修改日期,2017-03-30 15:10          

    %~z0,(Size)文件大小,696         

    %~$PATH:0,全路径,D:gongju ginxstop.bat                

    %~dp0,所在文件夹,D:gongju ginx            

    %~nx0,文件全称,stop.bat               

    %~fs0,全路径,D:gongju ginxstop.bat                

    %~dp$PATH:0,文件夹,D:gongju ginx                  

    %~ftza0,文件所有信息,--a------ 2017-03-30 15:10 696 D:gongju ginxstop.bat                

    %1,第一个附加参数,             

    %2,第二个附加参数,   

    cd /d [path],可直接跳转到其他盘符

     --------------------------------

    example: 磁盘中的视频(from https://blog.csdn.net/wangdonghao137/article/details/44015669)

     1 @echo off
     2 title 视频搜索
     3 echo 正在搜索你磁盘中的视频.......
     4 REM  '>' 清空原txt文档中的数据下写入,而 '>>'则是在保留原来数据的前提下写入
     5 echo star>"本机视频.txt"
     6 for /r "C:" %%i in (*.3gp,*.avi,*.mp4,*.wmv,*.mov,*.mpeg,*.mpg,*.qt,*.ram,*.asf) do (
     7     echo %%i
     8     echo %%i>>"本机视频.txt"
     9 )
    10 for /r "D:" %%i in (*.3gp,*.avi,*.mp4,*.wmv,*.mov,*.mpeg,*.mpg,*.qt,*.ram,*.asf) do (
    11     echo %%i
    12     echo %%i>>"本机视频.txt"
    13 )
    14 for /r "E:" %%i in (*.3gp,*.avi,*.mp4,*.wmv,*.mov,*.mpeg,*.mpg,*.qt,*.ram,*.asf) do (
    15     echo %%i
    16     echo %%i>>"本机视频.txt"
    17 )
    18 for /r "F:" %%i in (*.3gp,*.avi,*.mp4,*.wmv,*.mov,*.mpeg,*.mpg,*.qt,*.ram,*.asf) do (
    19     echo %%i
    20     echo %%i>>"本机视频.txt"
    21 )
    22 for /r "G:" %%i in (*.3gp,*.avi,*.mp4,*.wmv,*.mov,*.mpeg,*.mpg,*.qt,*.ram,*.asf) do (
    23     echo %%i
    24     echo %%i>>"本机视频.txt"
    25 )
    26 echo end>>"本机视频.txt"
    27 echo 搜索完成
    28 pause

    DOS批处理--根据当前日期和时间来创建文件夹和文件名(https://blog.csdn.net/champwang/article/details/47857477)

     1 @echo off
     2  
     3 echo . 
     4 echo ...initing
     5  
     6 set str_time_first_bit="%time:~0,1%"
     7 if %str_time_first_bit%==" " (
     8     set str_date_time=%date:~0,4%%date:~5,2%%date:~8,2%_0%time:~1,1%%time:~3,2%%time:~6,2%
     9 )else ( 
    10     set str_date_time=%date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2%%time:~6,2%
    11 )
    12 set logname=%str_date_time%.log
    13 echo %logname%
    14  
    15 set foldername=%str_date_time%_log_folder
    16 echo %foldername%
    17  
    18 pause
    19 exit
    20  

    日期时间生成文件名

     1 @echo off
     2  
     3 echo . 
     4 echo ...initing
     5  
     6 set str_time_first_bit="%time:~0,1%"
     7 if %str_time_first_bit%==" " (
     8     set str_date_time=%date:~0,4%%date:~5,2%%date:~8,2%_0%time:~1,1%%time:~3,2%%time:~6,2%
     9 )else ( 
    10     set str_date_time=%date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2%%time:~6,2%
    11 )
    12 set logname=%str_date_time%.log
    13 echo %logname%
    14  
    15 set foldername=%str_date_time%_log_folder
    16 echo %foldername%
    17  
    18 pause
    19 exit
    20  

    continue

  • 相关阅读:
    创建DataFrame https://www.cnblogs.com/andrew-address/p/13040035.html
    vim
    python 上下文管理协议
    AutoLisp 函数总结
    Visual Lisp 专题
    python基础之字符编码
    9月24号面试总结(康拓普1面)
    9月20号面试总结(zhongluan)
    9月20号面试总结(shangyun)
    9.19面试总结(ximei)
  • 原文地址:https://www.cnblogs.com/littlestone08/p/10394190.html
Copyright © 2020-2023  润新知