For loop: operation on a stack of files [newbie]

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Karen1017
Posts: 1
Joined: 20 Jan 2010 02:20

For loop: operation on a stack of files [newbie]

#1 Post by Karen1017 » 20 Jan 2010 02:28

Hi everyone!

I've never worked with batch files before, but just found out I need it for the project I'm working on...

I've got a stack of files: temp0000.000, temp0000.001,... temp0000.364. Now, I want to perform the same operation on all of these files. Is there an easy way to do this? (e.g. with the for function)?
I know what the operation syntax should be btw, this is just about the way to do it for ALL of the files.

- Of course I tried reading some of the stuff online but I couldn't really figure it out :?

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

#2 Post by !k » 20 Jan 2010 09:13

Which operation you need? Some operations may use the masks ? and * for manipulate with multiple files.

To manipulate files one by one, you can use command FOR

Code: Select all

for %%a in (temp0000.*) do echo Do something with "%%a"


...or M$ util FORFILES.EXE

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

#3 Post by alan_b » 20 Jan 2010 11:38

If you should want to do more than one DOS command for each file then something like :-

Code: Select all

@echo off
for %%a in (C:\Your_Path\*.*) do (
  dir %%a | find "/"
  attrib %%a
)
pause


Within the () brackets you can do extra tests upon each %%a item,
and do things such as copy all *.exe to a flash drive, type all *.txt files, and delete all *.tmp files.

Suggestion - for any dangerous operation such as DEL, use DIR %%a in your first attempt, and only replace DIR with DEL when you are satisfied that it will not accidentally select and damage files you need.

Alan

Post Reply