In a windows batch file, is there a way to traverse a folder/subfolders hierarchy doing some action on each file?
5 Answers
Yes, you can do this using the for
command with the /r
switch, e.g.:
for /r %%f in (*) do echo %%f
See also this question for an example.
-
This will traverse every file and directory. Question was only for directories. Use /d to get only directories of 1st level. Use like /r /d to do it recursively. – Peter Jan 25 '21 at 18:20
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.

- 38,619
- 8
- 86
- 96
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. :)

- 740
- 7
- 10
if you want to query folders in some location and traverse them, then you should first write the result of the query to a text file and then traverse lines of file content. Following example shows how to find first level subfolders whose name contain 'dev' and traverse them:
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
dir /b /s /ad *.* | sort
That should give the same results regardless of the path depth

- 50,140
- 28
- 121
- 140

- 1