Search code examples
batch-filefor-loopfile-type

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.


Solution

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