Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Name: pball
I want to be able to perform something to each file of certain type inside a folder. I have this script from something else I've done.
for /f "tokens=*" %%x in ('dir /b *.ext') do ( something )
That lets me do something to each .ext file in the folder the batch file is in. But it's not working with what I want.
I'm wondering if there is a better or more general way of doing this, since I don't know much about using the for loop.
I want to be able to get it to find all files of .ext for instance, and assign it a variable. Then in the command I would use that variable with a new trick I learned %var:~0,-3% to complete the task.
thanks

That's pretty vague, you don't say whether the 'something' is to be 'done' to the filenames or to the content of the files. Take a look at the code I posted to rename files, it might be of some use..

Assuming you want each file name set inside a variable. The following batch should do that.
setlocal enabledelayedexpansion
set count=0
for /f "tokens=*" %%x in ('dir /b /a:-d *.ext') do (
set /a count+=1
set var!count!=%%x
)
setlocal disabledelayedexpansion
Each variable should be set to %var1%, %var2%,%var3% ect. Just remember that if a filename contains spaces it must be enclosed in "double quotes" for it to do you much good.I have also added the /a:-d attribute switch to your dir command to avoid the possibility of picking up directories. For example dir /b *.ext would pick up a directory named directory.ext.

I prefer to follow the KISS standard.
FOR %%a IN (*.ext) DO @ECHO %%~DPNaEDIT: I don't know much about using the for loop.
Time to learn!FOR /?

"I prefer to follow the KISS standard."
Why am I not surprised.
=====================================
If at first you don't succeed, you're about average.M2

Thanks Judago
your script helped the most even though you misunderstood what I wanted.I don't know why the stuff I copied from another batch file I have which is exactly as you have except for the /a:-d part.
To clarify what I want the script to do. I want to take each file in the folder matching a certain file type, perform an operation to it, then move on to the next file.
also is there an easy way to get just the file name and drop the location? Cause the variable returned from this would be c:\folder\file.ext
is there something similar to %var:~0,-3% except it would return just the file name always without having to know the charaters to remove?

pball: is there an easy way to get just the file name
Again:FOR /?Really, everyone here will just refer to that for an answer. You really should just cut out the middle men.But to answer the question, use %%~Na.
Mechanix2Go: Why am I not surprised.
Because if it takes more than three lines, I look for a different way?Also, if we wrote up a How To article on dates and batch files, do you think we could get Justin to post it? Because, really, half of the questions here seem to be dedicated to the subject.

If you use /s with DIR you get the drive and directory. If you don't, you don't.
=====================================
If at first you don't succeed, you're about average.M2

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |