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

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


当前回答

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

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的消息“file Not Found”。

其他回答

我很难让jop的答案与绝对路径一起工作,直到我发现这个参考:https://ss64.com/nt/for_r.html

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

For /R C:\absoulte\path\ %%G IN (*.*) do (
  Echo %%G
)

我会使用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。

这将递归地列出当前目录及其子目录中的所有文件(仅是文件):

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

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

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

(感谢@agnul)

下面是代码中的注释。

我只是在温习婊子技能,所以请原谅任何明显的错误。

我试着写一个all in one解决方案,尽我所能,在用户需要的地方做了一些修改。

一些重要注意事项:如果只希望处理根目录、文件和文件夹,只需将变量递归更改为FALSE。否则,它会检查所有文件夹和文件。

最欢迎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

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

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的消息“file Not Found”。