代码之家  ›  专栏  ›  技术社区  ›  Vhaerun

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

  •  289
  • Vhaerun  · 技术社区  · 16 年前

    如何使用 for

    15 回复  |  直到 8 年前
        1
  •  548
  •   jop    16 年前

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

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

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

    (谢谢@agnul)

        2
  •  232
  •   CharlesB Craig McQueen    10 年前

    反复浏览。。。

    • …当前目录中的文件: 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的buildin帮助是描述cmd命令行语法的一个很好的资源?

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

        3
  •  101
  •   Aaron Votre    9 年前

    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将只获取文件的第一行。

        4
  •  65
  •   aphoria    12 年前

    跑步和跑步之间有着微妙的区别 FOR 从命令行和批处理文件。在批处理文件中,需要放置两个 % 每个变量引用前面的字符。

    从命令行:

    FOR %i IN (*) DO ECHO %i
    

    FOR %%i IN (*) DO ECHO %%i
    
        5
  •  44
  •   Jay    15 年前

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

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

    “delims=”用于显示包含空格的长文件名。。。。

    “/b”仅显示名称,不显示大小日期等。。

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

    • “/ad”将仅显示子目录,包括隐藏目录和系统目录。
    • “/a-d”参数将删除具有“d”目录属性的内容。
    • “/a-d-h-s”将显示所有内容,但带有'd'directory',h'idden's'system属性的条目除外。

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

        6
  •  12
  •   Sam Meldrum    16 年前

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

    @echo off
    for %%i in (*.*) do echo %%i
    
        7
  •  7
  •   datchung    5 年前

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

    For /R C:\absoulte\path\ %%G IN (*.*) do (
      Echo %%G
    )
    
        8
  •  6
  •   jafarbtech    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
    

    哪里 /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
    
        9
  •  5
  •   Axeman    16 年前
    for %1 in (*.*) do echo %1
    

    请尝试cmd中的“帮助”,以获得完整的指南

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

        10
  •  3
  •   Ankur    14 年前

    “AllFilesIncirentDirectoryList.txt” 在当前目录中,其中包含当前目录中所有文件(仅文件)的列表。过来看

    dir /b /a-d > AllFilesInCurrentDirectorylist.txt
    
        11
  •  3
  •   Peter Mortensen icecrime    8 年前

    它还可以使用 forfiles 命令:

    forfiles /s 
    

    并检查它是否是一个目录

    forfiles /p c:\ /s /m *.* /c "cmd /c if @isdir==true echo @file is a directory"
    
        12
  •  3
  •   Ste    5 年前

    我只是在复习比亚奇的技能,所以请原谅任何明显的错误。

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

    recursive 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
    
        13
  •  2
  •   Biri    16 年前

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

    检查 FileSystemObject on MSDN .

        14
  •  2
  •   competent_tech    12 年前

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

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

    我只是使用c:\作为目的地,因为它总是存在于windows系统上,并且它不是复制的,所以这并不重要。如果您也需要子目录,只需在末尾使用/s选项。如果出于其他原因需要,也可以使用xcopy的其他开关。

        15
  •  2
  •   Peter Mortensen icecrime    8 年前

    尝试此操作以测试文件是否为目录:

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

        16
  •  2
  •   Littlepony    8 年前

    试试这个:

    ::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..)

        17
  •  1
  •   Sam B    6 年前

    在我的情况下,我必须删除临时文件夹下的所有文件和文件夹。所以我就是这样做的。我必须运行两个循环,一个用于文件,一个用于文件夹。如果文件或文件夹的名称中有空格,则必须使用“”

    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"
    )