405

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

我怎么知道某个条目是一个目录还是只是一个文件?

4

17 回答 17

560

这列出了当前目录中的所有文件(并且只有文件):

for /r %i in (*) do echo %i

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

for /r %%i in (*) do echo %%i

(感谢@agnul)

于 2008-09-26T09:56:07.017 回答
237

遍历...

  • ...当前目录中的文件: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

于 2008-09-26T10:05:56.290 回答
109

要遍历每个文件,一个 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!"
)

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

于 2016-05-10T13:54:52.587 回答
66

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

从命令行:

FOR %i IN (*) DO ECHO %i

从批处理文件:

FOR %%i IN (*) DO ECHO %%i
于 2008-09-26T11:52:44.870 回答
44

这个 for 循环将列出目录中的所有文件。

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

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

'/b" 仅显示名称,不显示大小日期等。

关于 dir 的 /a 参数需要了解的一些事情。

  • 任何使用“/a”都会列出所有内容,包括隐藏属性和系统属性。
  • "/ad" 只会显示子目录,包括隐藏目录和系统目录。
  • “/ad”参数消除了具有“目录”属性的内容。
  • “/adhs”将显示所有内容,但具有“目录”、“隐藏”“系统”属性的条目。

如果您在命令行上使用它,请删除“%”。

希望这可以帮助。

于 2009-07-07T13:02:22.050 回答
14

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

试试这个:

@echo off
for %%i in (*.*) do echo %%i
于 2008-09-26T09:52:34.217 回答
9

在找到此参考之前,我很难让 jop 的答案使用绝对路径:https ://ss64.com/nt/for_r.html

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

For /R C:\absoulte\path\ %%G IN (*.*) do (
  Echo %%G
)
于 2019-06-25T20:35:49.920 回答
6

遍历您可以使用的所有文件和文件夹

for /F "delims=" %%a in ('dir /b /s') do echo %%a

遍历所有文件夹而不是文件,那么您可以使用

for /F "delims=" %%a in ('dir /a:d /b /s') do echo %%a

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

在迭代中实现搜索

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

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

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

for /F "delims=" %%a in ('dir "folder name" /b /AD /s') do echo %%a
于 2019-02-28T12:09:14.590 回答
5
for %1 in (*.*) do echo %1

在 cmd 中尝试“帮助”以获得完整指南

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

于 2008-09-26T09:48:12.433 回答
4

这是我在代码中的注释。

我只是在刷怪技能,所以请原谅任何明目张胆的错误。

我试图在用户需要的地方稍作修改,尽可能地编写一个多合一的解决方案。

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

C&C 最欢迎...

@echo off
title %~nx0
chcp 65001 >NUL
set "dir=c:\users\%username%\desktop"
::
:: Recursive Loop routine - First Written by Ste on - 2020.01.24 - Rev 1
::
setlocal EnableDelayedExpansion
rem THIS IS A RECURSIVE SOLUTION [ALBEIT IF YOU CHANGE THE RECURSIVE TO FALSE, NO]
rem By removing the /s switch from the first loop if you want to loop through
rem the base folder only.
set recursive=TRUE
if %recursive% equ TRUE ( set recursive=/s ) else ( set recursive= )
endlocal & set recursive=%recursive%
cd /d %dir%
echo Directory %cd%
for %%F in ("*") do (echo    → %%F)                                 %= Loop through the current directory. =%
for /f "delims==" %%D in ('dir "%dir%" /ad /b %recursive%') do (    %= Loop through the sub-directories only if the recursive variable is TRUE. =%
  echo Directory %%D
  echo %recursive% | find "/s" >NUL 2>NUL && (
    pushd %%D
    cd /d %%D
    for /f "delims==" %%F in ('dir "*" /b') do (                      %= Then loop through each pushd' folder and work on the files and folders =%
      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. =%
      rem You can do stuff to your files here.
      rem Below are some examples of the info you can get by expanding the %%F variable.
      rem Uncomment one at a time to see the results.
      echo    → %%~F           &rem expands %%F removing any surrounding quotes (")
      rem echo    → %%~dF          &rem expands %%F to a drive letter only
      rem echo    → %%~fF          &rem expands %%F to a fully qualified path name
      rem echo    → %%~pF          &rem expands %%A to a path only
      rem echo    → %%~nF          &rem expands %%F to a file name only
      rem echo    → %%~xF          &rem expands %%F to a file extension only
      rem echo    → %%~sF          &rem expanded path contains short names only
      rem echo    → %%~aF          &rem expands %%F to file attributes of file
      rem echo    → %%~tF          &rem expands %%F to date/time of file
      rem echo    → %%~zF          &rem expands %%F to size of file
      rem echo    → %%~dpF         &rem expands %%F to a drive letter and path only
      rem echo    → %%~nxF         &rem expands %%F to a file name and extension only
      rem echo    → %%~fsF         &rem expands %%F to a full path name with short names only
      rem echo    → %%~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
      rem echo    → %%~ftzaF       &rem expands %%F to a DIR like output line
      )
      )
    popd
    )
  )
echo/ & pause & cls
于 2020-01-24T22:29:52.287 回答
3

以下代码在当前目录中创建一个名为“AllFilesInCurrentDirectorylist.txt”的文件,其中包含当前目录中所有文件(Only Files)的列表。看看这个

dir /b /a-d > AllFilesInCurrentDirectorylist.txt
于 2010-09-28T09:21:02.290 回答
3

它也可以使用forfiles命令:

forfiles /s 

并检查它是否是一个目录

forfiles /p c:\ /s /m *.* /c "cmd /c if @isdir==true echo @file is a directory"
于 2014-09-11T14:41:35.343 回答
2

我会使用 vbscript(Windows 脚本主机),因为在批处理中我确定您无法分辨名称是文件还是目录。

在 vbs 中,它可以是这样的:

Dim fileSystemObject
Set fileSystemObject = CreateObject("Scripting.FileSystemObject")

Dim mainFolder
Set mainFolder = fileSystemObject.GetFolder(myFolder)

Dim files
Set files = mainFolder.Files

For Each file in files
...
Next

Dim subFolders
Set subFolders = mainFolder.SubFolders

For Each folder in subFolders
...
Next

检查MSDN 上的 FileSystemObject

于 2008-09-26T09:56:26.670 回答
2

试试这个来测试一个文件是否是一个目录:

FOR /F "delims=" %I IN ('DIR /B /AD "filename" 2^>^&1 ^>NUL') DO IF "%I" == "File Not Found" ECHO Not a directory

这只会告诉您文件是否不是目录,如果文件不存在,这也是正确的,因此如果需要,请务必先检查。插入符号 (^) 用于转义重定向符号,文件列表输出被重定向到 NUL 以防止显示,而 DIR 列表的错误输出被重定向到输出,因此您可以针对 DIR 的消息“找不到文件”进行测试”。

于 2011-01-04T17:55:15.570 回答
2

我使用带有 /L 选项的 xcopy 命令来获取文件名。因此,如果您想获取一个目录或子目录中的所有文件,您可以执行以下操作:

for /f "delims=" %%a IN ('xcopy "D:\*.pdf" c:\ /l') do echo %%a

我只是使用 c:\ 作为目标,因为它始终存在于 Windows 系统上并且它不会复制,所以没关系。如果您也想要子目录,请在最后使用 /s 选项。如果您出于其他原因需要它们,也可以使用 xcopy 的其他开关。

于 2012-12-29T19:27:55.680 回答
2

试试这个:

::Example directory
set SetupDir=C:\Users

::Loop in the folder with "/r" to search in recursive folders, %%f being a loop ::variable 
for /r "%SetupDir%" %%f in (*.msi *.exe) do set /a counter+=1

echo there are %counter% files in your folder

它计算您的目录(和子目录)中的 .msi 和 .exe 文件。因此,它也使文件夹和文件作为可执行文件有所不同。

如果您需要过滤循环中的其他文件,只需添加扩展名 (.pptx .docx ..)

于 2016-12-02T10:38:32.220 回答
1

就我而言,我必须删除临时文件夹下的所有文件和文件夹。所以这就是我最终这样做的方式。我必须运行两个循环,一个用于文件,一个用于文件夹。如果文件或文件夹的名称中有空格,那么您必须使用“”

cd %USERPROFILE%\AppData\Local\Temp\
rem files only
for /r %%a in (*) do (
echo deleting file "%%a" ...
if exist "%%a" del /s /q "%%a"
)
rem folders only
for /D %%a in (*) do (
echo deleting folder "%%a" ...
if exist "%%a" rmdir /s /q "%%a"
)
于 2018-09-26T17:54:47.073 回答