【问题标题】:How to traverse folder tree/subtrees in a windows batch file?如何遍历 Windows 批处理文件中的文件夹树/子树?
【发布时间】:2009-08-20 18:16:03
【问题描述】:

在 Windows 批处理文件中,有没有办法遍历文件夹/子文件夹层次结构,对每个文件执行一些操作?

【问题讨论】:

    标签: windows batch-file


    【解决方案1】:

    是的,您可以使用带有/r 开关的for 命令来执行此操作,例如:

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

    有关示例,另请参阅 this question

    【讨论】:

    • 这将遍历每个文件和目录。问题仅针对目录。使用 /d 仅获取第一级目录。使用 like /r /d 递归执行。
    【解决方案2】:

    您可以使用带有/r 开关的FOR 命令,这将遍历目录树,执行您在每个目录上的DO 语句中指定的任何内容。在那里你可以嵌套另一个FOR 语句,在SET 块中使用dir /b *.*

    【讨论】:

      【解决方案3】:

      幸运的是,我对此线程的目的非常相似。我相信指令

      dir /b /s /ad *.* [enter]
      

      将生成 DIRECTORY TREE 作为结果

      complete_path\dir_01_lev_01
      complete_path\dir_02_lev_01
      complete_path\dir_03_lev_01
      complete_path\dir_01_lev_01\dir_11_lev_02
      complete_path\dir_01_lev_01\dir_12_lev_02
      complete_path\dir_02_lev_01\dir_13_lev_02
      complete_path\dir_02_lev_01\dir_14_lev_02
      complete_path\dir_02_lev_01\dir_15_lev_02
      complete_path\dir_03_lev_01\dir_16_lev_02
      

      但我想要的结果如下

      complete_path\dir_01_lev_01
      complete_path\dir_01_lev_01\dir_11_lev_02
      complete_path\dir_01_lev_01\dir_12_lev_02
      complete_path\dir_02_lev_01
      complete_path\dir_02_lev_01\dir_13_lev_02
      complete_path\dir_02_lev_01\dir_14_lev_02
      complete_path\dir_02_lev_01\dir_15_lev_02
      complete_path\dir_03_lev_01
      complete_path\dir_03_lev_01\dir_16_lev_02
      

      所以,这个脚本诞生了 :)

      @echo off
      rem
      rem ::: My name is Tree-Folder-8-Level.cmd
      rem
      setlocal
      rem ::: Put started PATH here
      set i01=complete_path
      for /f "delims=" %%a in ('dir "%i01%" /ad /on /b') do call :p001 "%%a"
      endlocal
      goto :eof
      
      :p001
      rem ::: Display 1st LEVEL of started PATH
      echo %~1
      for /f "delims=" %%b in ('dir "%i01%\%~1" /ad /on /b') do call :p002 "%~1\%%b"
      goto :eof
      
      :p002
      rem ::: Display 2nd LEVEL of started PATH
      echo %~1
      for /f "delims=" %%c in ('dir "%i01%\%~1" /ad /on /b') do call :p003 "%~1\%%c"
      goto :eof
      
      :p003
      rem ::: Display 3rd LEVEL of started PATH
      echo %~1
      for /f "delims=" %%d in ('dir "%i01%\%~1" /ad /on /b') do call :p004 "%~1\%%d"
      goto :eof
      
      :p004
      rem ::: Display 4th LEVEL of started PATH
      echo %~1
      for /f "delims=" %%e in ('dir "%i01%\%~1" /ad /on /b') do call :p005 "%~1\%%e"
      goto :eof
      
      :p005
      rem ::: Display 5th LEVEL of started PATH
      echo %~1
      for /f "delims=" %%f in ('dir "%i01%\%~1" /ad /on /b') do call :p006 "%~1\%%f"
      goto :eof
      
      :p006
      rem ::: Display 6th LEVEL of started PATH
      echo %~1
      for /f "delims=" %%g in ('dir "%i01%\%~1" /ad /on /b') do call :p007 "%~1\%%g"
      goto :eof
      
      :p007
      rem ::: Display 7th LEVEL of started PATH
      rem :::     and 8th LEVEL of started PATH
      echo %~1
      for /f "delims=" %%h in ('dir "%i01%\%~1" /ad /on /b') do echo %~1\%%h
      goto :eof
      

      欢迎提出更聪明的想法。 :)

      【讨论】:

        【解决方案4】:

        如果要查询某个位置的文件夹并遍历它们,则应先将查询结果写入文本文件,然后遍历文件内容的行。 以下示例显示如何查找名称包含“dev”的第一级子文件夹 并遍历它们:

        if exist FolderList.txt (
         del FolderList.txt
        )
        set query=dev
        set /a count=0
        pushd %yourLocation%
        dir /ad /o-d /b *%query%* >> FolderList.txt
        for /f "tokens=*" %%a in ('type FolderList.txt') do (
         set /a count+=1
         echo %count%."%%a"
        
        )
        popd 
        
        Some sample results could be:
        1.Adev
        2.bDevc
        3.devaa
        

        【讨论】:

          【解决方案5】:
          dir /b /s /ad *.* | sort
          

          无论路径深度如何,都应该给出相同的结果

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-02-08
            • 1970-01-01
            • 2010-11-25
            • 2018-07-02
            • 1970-01-01
            • 2011-03-25
            • 2010-10-09
            相关资源
            最近更新 更多