这个特定的批处理脚本如何工作

时间:2012-12-02 03:48:03

标签: batch-file

下面的代码从文件中删除文件名exension。但我无法理解它是如何做到的。它只是以裸格式列出当前目录中的所有文件。但在那之后我有点迷茫。有人可以帮帮我吗?

@echo off
cd %1
if exist filelisting.txt del filelisting.txt
for /F "delims=" %%j in ('dir /A-D /B /O:GEN') do echo %%~nj >> filelisting.txt

1 个答案:

答案 0 :(得分:1)

第一个文件按扩展名排序,然后按文件名排序,文件夹与/O:GEN组合在一起(冗余)。排除/B完整路径并排除/A-D个文件夹。使用FOR /F %%J IN ('command') DO (..)时,每行命令输出都设置为%%J变量。而%%~nJ仅使用没有扩展名的文件名。这里有一些链接:

  1. DIR
  2. FOR /F
  3. FILE ATTRIBUTES
  4. 编辑:示例:

    @echo off
    echo %~0    -   expands %i removing any surrounding quotes (")
    echo %~f0   -   expands %i to a fully qualified path name
    echo %~d0   -   expands %i to a drive letter only
    echo %~p0   -   expands %i to a path only
    echo %~n0   -   expands %i to a file name only
    echo %~x0   -   expands %i to a file extension only
    echo %~s0   -   expanded path contains short names only
    echo %~a0   -   expands %i to file attributes of file
    echo %~t0   -   expands %i to date/time of file
    echo %~z0   -   expands %i to size of file
    echo %~$PATH:0  -   searches the directories listed in the PATH environment variable and expands %i to the fully qualified name of the first one found.
    echo %~dp0  -   expands %i to a drive letter and path only
    echo %~nx0  -   expands %i to a file name and extension only
    echo %~fs0  -   expands %i to a full path name with short names only
    echo %~dp$PATH:0    -   searches the directories listed in the PATH environment variable for %i and expands to the drive letter and path of the first one found.
    echo %~ftza0    -   expands %i to a DIR like output line
    pause
    

    保存这个.bat文件。%0是被调用的.bat文件的路径。这将列出所有%~0个操作。 %~仅适用于一侧封闭变量 - 批处理文件参数%0,%1 ..或FOR变量。