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

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


当前回答

下面是代码中的注释。

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

我试着写一个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

其他回答

我使用xcopy命令和/L选项来获取文件名。所以如果你想获取一个目录或子目录下的所有文件,你可以这样做:

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

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

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

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

若要只遍历所有文件夹而不遍历文件,则可以使用

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

其中/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

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

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

"delims="用于显示包含空格的长文件名....

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

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

使用“/a”会列出所有内容,包括隐藏属性和系统属性。 “/ad”只显示子目录,包括隐藏目录和系统目录。 "/a-d"参数消除带有'D'irectory属性的内容。 "/a-d-h-s"将显示所有内容,但包含'D'irectory, 'H'idden 'S'ystem属性的条目除外。

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

希望这能有所帮助。

遍历…

...当前目录下的文件:for %f in (.\*) do @echo %f ...subdirs in current dir: for /D %s in (.\*) do @echo %s .../R %f在(.\*)做@echo %f .../R /D %s in (.\*) do @echo %s

不幸的是,我没有找到同时遍历文件和子dirs的任何方法。

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

除此之外:你有没有注意到,MS Windows的内置帮助是描述cmd命令行的语法的一个很好的资源?

也可以在这里看看:http://technet.microsoft.com/en-us/library/bb490890.aspx

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

dir /b /a-d > AllFilesInCurrentDirectorylist.txt