views:

8435

answers:

9

How could I iterate over each file in a directory using for? And how could I tell if a certain entry is a directory or if it's just a file?

+2  A: 

In bash, you might do something like this:

for fn in *; do
    if [ -d $fn ]; then
        echo "$fn is a directory"
    fi
    if [ -f $fn ]; then
        echo "$fn is a file"
    fi
done

I just noticed that you asked about batch, which I misread as bash. This answer may therefore be not appropriate to your question.

Greg Hewgill
Bwahahaha! :-) +1
Chris Jester-Young
+2  A: 
for %1 in (*.*) do echo %1

Try "HELP FOR" in cmd for a full guide

This is the guide for XP commands. http://www.ss64.com/nt/

Axeman
@Axeman: +1, add a link to this site for a definitive online reference: http://www.ss64.com/nt/.
sixlettervariables
+4  A: 

%1 refers to the first argument passed in and can't be used in an iterator.

Try this:

@echo off
for %%i in (*.*) do echo %%i
Sam Meldrum
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 :-)
Axeman
+11  A: 

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)

jop
I tried your command , but it's not working .
Vhaerun
I get a syntax error on that also.
Sam Meldrum
It might depend on what OS you are using, i.e. XP/Vista/2000 might support different command line arguments.
RickL
Also if you run that command in a batch file you need to double the % signs. See the help for "for".
agnul
+2  A: 

I would use vbscript (Windows Scripting Host), because in batch I'm sure you cannot tell that a name is a file or a directory.

In vbs, it can be something like this:

Dim fileSystemObject
Set fileSystemObject = CreateObject("Scripting.FileSystemObject")

Dim mainFolder
Set mainFolder = fileSystemObject.GetFolder(myFolder)

Dim files
Set files = mainFolder.Files

For Each file in files
...
Next

Dim subFolders
Set subFolders = mainFolder.SubFolders

For Each folder in subFolders
...
Next

Check FileSystemObject on MSDN.

Biri
I would have used perl to do this. Unfortunately , it's not up to me.
Vhaerun
Some old application? Sad things.
Biri
An idiot developer saw batch files and thought that they were the cure for all of our problems .
Vhaerun
+9  A: 

Iterate through...

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

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

Marco
+3  A: 

There is a subtle difference between running the FOR from 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
aphoria
+2  A: 

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.

Jay
A: 

The following code creates a file Named "AllFilesInCurrentDirectorylist.txt" in the current Directory, which contains the list of all files (Only Files) in the current Directory. Check it out

dir /b /a-d > AllFilesInCurrentDirectorylist.txt
Ankur Pandya