Batch script with for loop in it

home made
August 23, 2008 at 23:31:22
Specs: XP Pro, 2.8 ghz / 2 gig

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


See More: Batch script with for loop in it

Report •


#1
August 24, 2008 at 00:44:00

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..

http://www.computing.net/answers/do...


Report •

#2
August 24, 2008 at 01:48:26

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.


Report •

#3
August 24, 2008 at 05:03:20

I prefer to follow the KISS standard.
FOR %%a IN (*.ext) DO @ECHO %%~DPNa

EDIT: I don't know much about using the for loop.
Time to learn!

FOR /?


Report •

Related Solutions

#4
August 24, 2008 at 06:43:12

"I prefer to follow the KISS standard."

Why am I not surprised.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#5
August 24, 2008 at 07:27:50

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?


Report •

#6
August 24, 2008 at 14:28:55

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.


Report •

#7
August 25, 2008 at 05:27:24

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


Report •


Ask Question