D'Arcy Rail-Ip
D'Arcy Rail-Ip

Reputation: 11975

How to recursively loop through a directory on a specific filetype?

I can't find a specific mention of my issue, though it seems like it would be a common problem.

I'm trying to loop through all batch files in a directory, regardless of how deep.

Here is what I have:

for /f "tokens=* delims=" %%a in ('dir %DIR% /s /b *.bat') do (
    if not exist %%a\* echo %%a
)

Where DIR is set beforehand. I'm echoing only files.

Clearly this is wrong as it outputs firstly all files, THEN all batch files.

Seems I need to somehow merge the *.bat specifier and the %DIR% variable but I'm not sure how to do this.

Upvotes: 1

Views: 149

Answers (2)

jftuga
jftuga

Reputation: 1973

You question is somewhat vague to me, but I think this is what you want:

@echo off
setlocal

set DIR=h:\scripts

for /f "usebackq delims=;" %%q in (`dir %DIR%\*.bat /s/b/a-d`) do (
    echo %%q
)

endlocal

This puts all .bat files into %%q.

Upvotes: 1

ths
ths

Reputation: 2942

for /r %DIR% %%a in (*.bat) do ( ...

Upvotes: 1

Related Questions