如何遍历 Windows 批处理文件中的文件夹树/子树?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1307935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to traverse folder tree/subtrees in a windows batch file?
提问by
In a windows batch file, is there a way to traverse a folder/subfolders hierarchy doing some action on each file?
在 Windows 批处理文件中,有没有办法遍历文件夹/子文件夹层次结构对每个文件执行某些操作?
回答by Helen
Yes, you can do this using the for
command with the /r
switch, e.g.:
是的,您可以使用for
带有/r
开关的命令来执行此操作,例如:
for /r %%f in (*) do echo %%f
See also this questionfor an example.
另请参阅此问题以获取示例。
回答by akf
You can use the FOR
command with the /r
switch, which will walk the directory tree executing whatever you specify in the DO
statement on each directory. There you can nest another FOR
statement, using a dir /b *.*
in the SET
block.
您可以将FOR
命令与/r
开关一起使用,它将遍历目录树,执行您DO
在每个目录的语句中指定的任何内容。在那里你可以嵌套另一个FOR
语句,dir /b *.*
在SET
块中使用 a 。
回答by Rhak Kahr
Fortunately I have quite similar purpose regarding this thread. I believe INSTRUCTION
幸运的是,我对这个线程有非常相似的目的。我相信指令
dir /b /s /ad *.* [enter]
will produce DIRECTORY TREE as result
结果将产生目录树
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
But I want result as below
但我想要如下结果
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
So, this SCRIPT is BORN :)
所以,这个脚本诞生了:)
@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
Brighter ideas are welcome. :)
欢迎更聪明的想法。:)
回答by wolfgang
dir /b /s /ad *.* | sort
That should give the same results regardless of the path depth
无论路径深度如何,这都应该给出相同的结果