windowsbatch-filefor-loopcmd

Iterate all files in a directory using a 'for' loop


How can I iterate over each file in a directory using a for loop?

And how could I tell if a certain entry is a directory or if it's just a file?


Solution

  • This lists all the files (and only the files) in the current directory and its subdirectories recursively:

    for /r %i in (*) do echo %i
    

    Also if you run that command in a batch file you need to double the % signs.

    for /r %%i in (*) do echo %%i
    

    (thanks @agnul)