Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directory list for FOR loop in msdos .bat file

I am looking how to get list of all directories to be used in FOR loop.

So far I have work around:

set folderList = (folder1 folder2 folder3 folder4)
FOR %%i in %folderList% do zip %%i D:\...my_path...\%%i\*.*

is it possible that folderList would be generated dynamically ?

like image 615
bensiu Avatar asked Feb 18 '23 14:02

bensiu


2 Answers

assuming you want to list subdirectories of c:\temp

for /f %%i in ('dir c:\temp /ad /b') do echo %%i

this will list foldernames of c:\temp, if you want get it recursively just add /s to dir command:

for /f %%i in ('dir c:\temp /ad /b /s') do echo %%i

as for @dbenham comment (thank you) to correctly handle dirs with space just add tokens=* to for :

 for /f "tokens=*" %%i in ('dir c:\temp /ad /b') do echo %%i
like image 102
Loïc MICHEL Avatar answered Feb 20 '23 03:02

Loïc MICHEL


Please try below code:

for /d %%F in ("d:\...my_path...\*") do zip "%%~nxF" "%%F\*.*"
like image 43
dbenham Avatar answered Feb 20 '23 05:02

dbenham