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?
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.
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/
%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
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)
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.
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
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
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.
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