Wednesday 16 July 2008

Batch programming tip#09: Using FOR /D

This is a basic variation of the for loop. Add the /D modifier to loop through directories.

The following script shows all the subdirectories of the current directory (the directory the batch is running from).

@echo off

:action
for /D %%l in (*) do (
echo Directory: %%l
)
goto eof

:eof
echo Press any key to close window...
pause > nul
goto blackhole

:blackhole


Let's simply list all directories and all files in those directories:

@echo off

:action
for /D %%l in (*) do (
echo Directory: %%l
cd %~dp0\%%l
for %%f in (*.*) do (
echo File: %%f
)

)
goto eof

:eof
echo Press any key to close window...
pause > nul
goto blackhole

:blackhole

Well, that's it for now.

Thoughts?

No comments:

Online Marketing
Add blog to our blog directory blog search directory Blog Directory Blogarama - The Blog Directory