1

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

@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
4

1 回答 1

1

首先文件按扩展名排序,然后按文件名和文件夹分组在一起(这是多余的)与/O:GEN. /B排除完整路径并排除文件/A-D夹。FOR /F %%J IN ('command') DO (..)每行命令输出都设置为%%J 变量。with%%~nJ只取文件名,不带扩展名。这里有一些链接:

  1. 目录
  2. 为 /F
  3. 文件属性

编辑:示例:

@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

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

于 2012-12-02T09:33:31.293 回答