Computing.Net > Forums > Programming > Batch script with for loop in it

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch script with for loop in it

Reply to Message Icon

Name: pball
Date: August 23, 2008 at 23:31:22 Pacific
OS: XP Pro
CPU/Ram: 2.8 ghz / 2 gig
Product: home made
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Valerie (by Garibaldi)
Date: August 24, 2008 at 00:44:00 Pacific
Reply:

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


0

Response Number 2
Name: Judago
Date: August 24, 2008 at 01:48:26 Pacific
Reply:

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.


0

Response Number 3
Name: Razor2.3
Date: August 24, 2008 at 05:03:20 Pacific
Reply:

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 /?


0

Response Number 4
Name: Mechanix2Go
Date: August 24, 2008 at 06:43:12 Pacific
Reply:

"I prefer to follow the KISS standard."

Why am I not surprised.


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

M2


0

Response Number 5
Name: pball
Date: August 24, 2008 at 07:27:50 Pacific
Reply:

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?


0

Related Posts

See More



Response Number 6
Name: Razor2.3
Date: August 24, 2008 at 14:28:55 Pacific
Reply:

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.


0

Response Number 7
Name: Mechanix2Go
Date: August 25, 2008 at 05:27:24 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch script with for loop in it

FOR Loop in Win2K Script www.computing.net/answers/programming/for-loop-in-win2k-script/9942.html

Removing Special Characters FOR Loop www.computing.net/answers/programming/removing-special-characters-for-loop/18895.html

XP Pro Batch Script -FOR loop issue www.computing.net/answers/programming/xp-pro-batch-script-for-loop-issue/16073.html