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?
17 Answers 17
This lists all the files (and only the files) in the current directory:
Also if you run that command in a batch file you need to double the % signs.
nxi ) . This thread can be really useful too: stackoverflow.com/questions/112055/…. – Sk8erPeter Dec 21 ’11 at 21:25
- . 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?
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).
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:
From a batch file:
This for-loop will list all files in a directory.
«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 «%».
Rename all files in a directory with a Windows batch script
How would I write a batch or cmd file that will rename all files in a directory? I am using Windows.
2 Answers 2
A FOR statement to loop through the names (type FOR /? for help), and string search and replace (type SET /? for help).
UPDATE — 2012-11-07
I’ve investigated how the RENAME command deals with wildcards: How does the Windows RENAME command interpret wildcards?
It turns out that this particular problem can be very easily solved using the RENAME command without any need for a batch script.
The number of characters after the _ does not matter. The rename would still work properly if 120×90 became x or xxxxxxxxxx . The important aspect of this problem is that the entire text between the last _ and the . is replaced.
As of Windows 7 you can do this in one line of PowerShell.
Explanation
powershell -C «. » launches a PowerShell session to run the quoted command. It returns to the outer shell when the command completes. -C is short for -Command .
gci returns all the files in the current directory. It is an alias for Get-ChildItem .
| % <. >makes a pipeline to process each file. % is an alias for Foreach-Object .
$_.Name is the name of the current file in the pipeline.
($_.Name -replace ‘120×90′, ’67×100’) uses the -replace operator to create the new file name. Each occurrence of the first substring is replaced with the second substring.
rni changes the name of each file. The first parameter (called -Path ) identifies the file. The second parameter (called -NewName ) specifies the new name. rni is an alias for Rename-Item.
Command to list all files in a folder as well as sub-folders in windows
I tried searching for a command that could list all the file in a directory as well as subfolders using a command prompt command. I have read the help for «dir» command but coudn’t find what I was looking for. Please help me what command could get this.
6 Answers 6
The below post gives the solution for your scenario.
/S Displays files in specified directory and all subdirectories.
/B Uses bare format (no heading information or summary).
/O List by files in sorted order.
If you want to list folders and files like graphical directory tree, you should use tree command.
There are various options for display format or ordering.
Check example output.
Answering late. Hope it help someone.
An addition to the answer: when you do not want to list the folders, only the files in the subfolders, use /A-D switch like this:
An alternative to the above commands that is a little more bulletproof.
It can list all files irrespective of permissions or path length.
I have a slight issue with the use of C:\NULL which I have written about in my blog
But nevertheless it’s the most robust command I know.
If you simply need to get the basic snapshot of the files + folders. Follow these baby steps:
- Press Windows + R
- Press Enter
- Type cmd
- Press Enter
- Type dir -s
- Press Enter
Following commands we can use for Linux or Mac. For Windows we can use below on git bash.
List all files, first level folders, and their contents
List all first-level subdirectories and files