使用'for'循环迭代目录中的所有文件

Vha*_*run 324 windows for-loop cmd batch-file

如何使用for循环遍历目录中的每个文件?

如何判断某个条目是某个目录还是仅仅是一个文件?

jop*_*jop 456

这将列出当前目录中的所有文件(以及仅文件):

for /r %i in (*) do echo %i
Run Code Online (Sandbox Code Playgroud)

此外,如果您在批处理文件中运行该命令,则需要将%符号加倍.

for /r %%i in (*) do echo %%i
Run Code Online (Sandbox Code Playgroud)

(谢谢@agnul)

  • 如果你不想以递归方式使用它,请确保取出/ r (69认同)
  • 如果你想只回显当前目录中的扩展名(不是完整路径)(递归),你可以这样做:`for/r%i in(*)do(echo%~nxi) `.这个线程也非常有用:http://stackoverflow.com/questions/112055/what-does-d0-mean-in-a-windows-batch-file. (26认同)
  • @Vaccano是的,在执行之后,请使用括号。即(回显%i&del%i)。您也可以对多个命令使用“ enter”而不是“&”。 (2认同)
  • 如果您使用的是复制/移动而不是回声等命令,请确保正确引用路径.for/r %% i in(*)do echo"%% i" (2认同)
  • 您显然需要使用单字母变量名称。 (2认同)

小智 204

迭代...

  • ...当前目录中的文件: for %f in (.\*) do @echo %f
  • ...当前目录中的子目录: for /D %s in (.\*) do @echo %s
  • ...当前和所有子目录中的文件: for /R %f in (.\*) do @echo %f
  • ...当前和所有子目录中的子目录: for /R /D %s in (.\*) do @echo %s

不幸的是,我没有找到任何方法同时迭代文件和子目录.

只需使用cygwin及其bash即可获得更多功能.

除此之外:您是否注意到,MS Windows的构建帮助是描述cmd命令行语法的重要资源?

另请看这里:http://technet.microsoft.com/en-us/library/bb490890.aspx

  • `%file`和`%subdir`只能是一个字符长,即`%f`,`%s`. (21认同)

aph*_*ria 55

FOR从命令行运行和批处理文件之间存在细微差别.在批处理文件中,您需要%在每个变量引用前放置两个字符.

从命令行:

FOR %i IN (*) DO ECHO %i
Run Code Online (Sandbox Code Playgroud)

从批处理文件:

FOR %%i IN (*) DO ECHO %%i
Run Code Online (Sandbox Code Playgroud)


小智 55

要迭代每个文件,for循环将起作用:

for %%f in (directory\path\*) do ( something_here )

在我的情况下,我也想要文件内容,名称等.

这导致一些问题,我认为我的用例可能会有所帮助.这是一个循环,它从目录中的每个'.txt'文件中读取信息,并允许您对它执行某些操作(例如setx).

@ECHO OFF
setlocal enabledelayedexpansion
for %%f in (directory\path\*.txt) do (
  set /p val=<%%f
  echo "fullname: %%f"
  echo "name: %%~nf"
  echo "contents: !val!"
)
Run Code Online (Sandbox Code Playgroud)

*限制:val <= %% f只会获取文件的第一行.

  • 啊,一个多线的例子.感谢那! (12认同)

Jay*_*Jay 40

此for循环将列出目录中的所有文件.

pushd somedir
for /f "delims=" %%f in ('dir /b /a-d-h-s') do echo %%f
popd
Run Code Online (Sandbox Code Playgroud)

"delims ="对于显示带有空格的长文件名非常有用....

'/ b"只显示名称,而不是大小日期等.

关于dir/a argument的一些事情.

  • 任何使用"/ a"都会列出所有内容,包括隐藏和系统属性.
  • "/ ad"只会显示子目录,包括隐藏目录和系统目录.
  • "/ ad"参数使用"D"irectory属性消除内容.
  • "/ adhs"会显示所有内容,但是带有'D'irectory,'H'idden'S'ystem属性的条目.

如果在命令行上使用此选项,请删除"%".

希望这可以帮助.


Sam*_*rum 10

%1引用传入的第一个参数,不能在迭代器中使用.

试试这个:

@echo off
for %%i in (*.*) do echo %%i
Run Code Online (Sandbox Code Playgroud)


Ste*_*Ste 9

这是我在代码中的注释。

\n\n

我只是在温习混蛋技巧,所以请原谅任何明显的错误。

\n\n

我尝试尽我所能编写一个一体化解决方案,并根据用户需要进行一些修改。

\n\n

一些重要的注意事项:如果您只想处理根目录文件和文件夹,只需将变量更改recursive为即可。FALSE否则,它将遍历所有文件夹和文件。

\n\n

欢迎《C&C》...

\n\n
@echo off\ntitle %~nx0\nchcp 65001 >NUL\nset "dir=c:\\users\\%username%\\desktop"\n::\n:: Recursive Loop routine - First Written by Ste on - 2020.01.24 - Rev 1\n::\nsetlocal EnableDelayedExpansion\nrem THIS IS A RECURSIVE SOLUTION [ALBEIT IF YOU CHANGE THE RECURSIVE TO FALSE, NO]\nrem By removing the /s switch from the first loop if you want to loop through\nrem the base folder only.\nset recursive=TRUE\nif %recursive% equ TRUE ( set recursive=/s ) else ( set recursive= )\nendlocal & set recursive=%recursive%\ncd /d %dir%\necho Directory %cd%\nfor %%F in ("*") do (echo    \xe2\x86\x92 %%F)                                 %= Loop through the current directory. =%\nfor /f "delims==" %%D in (\'dir "%dir%" /ad /b %recursive%\') do (    %= Loop through the sub-directories only if the recursive variable is TRUE. =%\n  echo Directory %%D\n  echo %recursive% | find "/s" >NUL 2>NUL && (\n    pushd %%D\n    cd /d %%D\n    for /f "delims==" %%F in (\'dir "*" /b\') do (                      %= Then loop through each pushd\' folder and work on the files and folders =%\n      echo %%~aF | find /v "d" >NUL 2>NUL && (                        %= This will weed out the directories by checking their attributes for the lack of \'d\' with the /v switch therefore you can now work on the files only. =%\n      rem You can do stuff to your files here.\n      rem Below are some examples of the info you can get by expanding the %%F variable.\n      rem Uncomment one at a time to see the results.\n      echo    \xe2\x86\x92 %%~F           &rem expands %%F removing any surrounding quotes (")\n      rem echo    \xe2\x86\x92 %%~dF          &rem expands %%F to a drive letter only\n      rem echo    \xe2\x86\x92 %%~fF          &rem expands %%F to a fully qualified path name\n      rem echo    \xe2\x86\x92 %%~pF          &rem expands %%A to a path only\n      rem echo    \xe2\x86\x92 %%~nF          &rem expands %%F to a file name only\n      rem echo    \xe2\x86\x92 %%~xF          &rem expands %%F to a file extension only\n      rem echo    \xe2\x86\x92 %%~sF          &rem expanded path contains short names only\n      rem echo    \xe2\x86\x92 %%~aF          &rem expands %%F to file attributes of file\n      rem echo    \xe2\x86\x92 %%~tF          &rem expands %%F to date/time of file\n      rem echo    \xe2\x86\x92 %%~zF          &rem expands %%F to size of file\n      rem echo    \xe2\x86\x92 %%~dpF         &rem expands %%F to a drive letter and path only\n      rem echo    \xe2\x86\x92 %%~nxF         &rem expands %%F to a file name and extension only\n      rem echo    \xe2\x86\x92 %%~fsF         &rem expands %%F to a full path name with short names only\n      rem echo    \xe2\x86\x92 %%~dp$dir:F    &rem searches the directories listed in the \'dir\' environment variable and expands %%F 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\n      rem echo    \xe2\x86\x92 %%~ftzaF       &rem expands %%F to a DIR like output line\n      )\n      )\n    popd\n    )\n  )\necho/ & pause & cls\n
Run Code Online (Sandbox Code Playgroud)\n


dat*_*ung 7

在找到此参考文献之前,我无法获得使用绝对路径的 jop 答案:https ://ss64.com/nt/for_r.html

以下示例循环遍历由绝对路径指定的目录中的所有文件。

For /R C:\absoulte\path\ %%G IN (*.*) do (
  Echo %%G
)
Run Code Online (Sandbox Code Playgroud)


Axe*_*man 5

for %1 in (*.*) do echo %1
Run Code Online (Sandbox Code Playgroud)

在cmd中尝试"帮助"获取完整指南

这是XP命令的指南.http://www.ss64.com/nt/


jaf*_*ech 5

通过所有文件和文件夹重复,您可以使用

for /F "delims=" %%a in ('dir /b /s') do echo %%a
Run Code Online (Sandbox Code Playgroud)

仅遍历所有文件夹而不遍历文件,则可以使用

for /F "delims=" %%a in ('dir /a:d /b /s') do echo %%a
Run Code Online (Sandbox Code Playgroud)

哪里/s将给出整个目录树中无限深度的所有结果。/s如果要遍历该文件夹的内容而不是其子文件夹,则可以跳过

在迭代中实现搜索

遍历特定命名的文件和文件夹,您可以搜索名称并使用 for 循环进行迭代

for /F "delims=" %%a in ('dir "file or folder name" /b /s') do echo %%a
Run Code Online (Sandbox Code Playgroud)

遍历特定命名的文件夹/目录而不是文件,然后/AD在同一命令中使用

for /F "delims=" %%a in ('dir "folder name" /b /AD /s') do echo %%a
Run Code Online (Sandbox Code Playgroud)