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

700,393

Solution 1

This lists all the files (and only the files) in the current directory:

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)

Solution 2

Iterate through...

  • ...files in current dir: for %f in (.\*) do @echo %f
  • ...subdirs in current dir: for /D %s in (.\*) do @echo %s
  • ...files in current and all subdirs: for /R %f in (.\*) do @echo %f
  • ...subdirs in current and all subdirs: for /R /D %s in (.\*) do @echo %s

Unfortunately I did not find any way to iterate over files and subdirs at the same time.

Just use cygwin with its bash for much more functionality.

Apart from this: Did you notice, that the buildin help of MS Windows is a great resource for descriptions of cmd's command line syntax?

Also have a look here: http://technet.microsoft.com/en-us/library/bb490890.aspx

Solution 3

To iterate over each file a for loop will work:

for %%f in (directory\path\*) do ( something_here )

In my case I also wanted the file content, name, etc.

This lead to a few issues and I thought my use case might help. Here is a loop that reads info from each '.txt' file in a directory and allows you do do something with it (setx for instance).

@ECHO OFF
setlocal enabledelayedexpansion
for %%f in (directory\path\*.txt) do (
  set /p val=<%%f
  echo "fullname: %%f"
  echo "name: %%~nf"
  echo "contents: !val!"
)

*Limitation: val<=%%f will only get the first line of the file.

Solution 4

There is a subtle difference between running FOR from the command line and from a batch file. In a batch file, you need to put two % characters in front of each variable reference.

From a command line:

FOR %i IN (*) DO ECHO %i

From a batch file:

FOR %%i IN (*) DO ECHO %%i

Solution 5

This for-loop will list all files in a directory.

pushd somedir
for /f "delims=" %%f in ('dir /b /a-d-h-s') do echo %%f
popd

"delims=" is useful to show long filenames with spaces in it....

'/b" show only names, not size dates etc..

Some things to know about dir's /a argument.

  • Any use of "/a" would list everything, including hidden and system attributes.
  • "/ad" would only show subdirectories, including hidden and system ones.
  • "/a-d" argument eliminates content with 'D'irectory attribute.
  • "/a-d-h-s" will show everything, but entries with 'D'irectory, 'H'idden 'S'ystem attribute.

If you use this on the commandline, remove a "%".

Hope this helps.

Share:
700,393
Vhaerun
Author by

Vhaerun

Free time ? Perl/Ruby scripter : Java developer

Updated on July 14, 2022

Comments

  • Vhaerun
    Vhaerun almost 2 years

    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?

  • Penfold
    Penfold over 15 years
    You're right. I've tried in immediate mode to check the FOR syntax and pasted the line straight into the answer forgetting about parameters :-)
  • Vhaerun
    Vhaerun over 15 years
    I would have used perl to do this. Unfortunately , it's not up to me.
  • Biri
    Biri over 15 years
    Some old application? Sad things.
  • RickL
    RickL over 15 years
    It might depend on what OS you are using, i.e. XP/Vista/2000 might support different command line arguments.
  • Vhaerun
    Vhaerun over 15 years
    An idiot developer saw batch files and thought that they were the cure for all of our problems .
  • jocull
    jocull over 13 years
    If you do not want to use this recursively, make sure you take out the /r
  • Sk8erPeter
    Sk8erPeter over 12 years
    If you would like to echo only the filenames (not the full path) with their extensions in the current directory (recursively), you can do it like this: for /r %i in (*) do ( echo %~nxi ). This thread can be really useful too: stackoverflow.com/questions/112055/….
  • Parampreet Dhatt
    Parampreet Dhatt over 11 years
    @Vhaerun One advantage of Windows Script Host (WSH) over Perl would be that WSH comes pre-installed with all versions of Windows, whereas Perl would need to be installed separately, which may or may not be a feasible option in all cases.
  • Felix Dombek
    Felix Dombek about 11 years
    %file and %subdir can only be one character long, i.e. %f, %s.
  • Ali Mst
    Ali Mst over 10 years
    What if you want to do more than one thing? Can you list several things to do?
  • Jay
    Jay about 10 years
    @Vaccano yes, after the Do, use parenthesis. I.e. do (echo %i&del %i). You can also use "enter" instead of "&" for multiple commands.
  • Soundararajan
    Soundararajan over 9 years
    If you are using commands like copy/move rather than echo, make sure that you quote the path properly. for /r %%i in (*) do echo "%%i"
  • psyklopz
    psyklopz about 9 years
    @isdir==true needs to be @isdir==TRUE
  • Server Overflow
    Server Overflow about 8 years
    the 'subdirs in current dir' is not working. I get an error: s was unexpected at this time
  • Nyerguds
    Nyerguds almost 8 years
    Ah, a multi-line example. Thanks for that!
  • nijogeorgep
    nijogeorgep over 7 years
    @Aaron Votre How to end a loop?
  • Aaron Votre
    Aaron Votre over 7 years
    @nijogeorgep You don't have to "end" the loop. In my example, everything inside of the parentheses, ( echo, etc ), will be run once for each '*.txt' file in the directory. I think the answer you are looking for might be better explained here: stackoverflow.com/questions/1355791/…
  • nijogeorgep
    nijogeorgep over 7 years
    @Aaron Votre I was trying to run some maven commands using script, but it was running as infinite loop , So I asked for it. Buy now I added an exit to my loop and fixed my issue
  • Aaron Votre
    Aaron Votre over 7 years
    @nijogeorgep Glad you figured it out!
  • BenKoshy
    BenKoshy over 7 years
    @jocull hi there - thx for your advice. does the for loop require an "end" keyword anywhere here?
  • Álvaro González
    Álvaro González over 6 years
    You apparently need to use single letter variable names.