loop a script for multiple files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
cactusman252
Posts: 10
Joined: 08 Mar 2011 08:54

loop a script for multiple files

#1 Post by cactusman252 » 31 Mar 2011 10:24

Is there a way to make this script run for every file in a directory. File names are random. I tried *.txt but no luck.


=================================================================
for /f "tokens=2*" %%a in ('findstr /c:"File(s)" "THEFILE.txt"') do (
for /f "tokens=2 delims=\" %%c in ("%%b") do -

Echo %%b
) >> THEFILE.txt

=================================================================

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: loop a script for multiple files

#2 Post by aGerman » 03 Apr 2011 17:21

You try writing to the same file where you're currently reading it's contents? Not a good idea.

BTW: You should enclose code in Code-Tags.

Post an example of the files content (also enclosed in Code-Tags) and explain what you're trying to do.

Regards
aGerman

cactusman252
Posts: 10
Joined: 08 Mar 2011 08:54

Re: loop a script for multiple files

#3 Post by cactusman252 » 04 Apr 2011 09:53

A sample file would look like this:

Code: Select all

Directory of Y:\Tom\Program Integration

15/09/2010  06:44 AM         2,523,556 GF (1).avi
15/09/2010  06:45 AM        58,323,282 GF (10).avi
15/09/2010  06:45 AM        34,318,466 GF (11).avi
15/09/2010  06:45 AM           993,988 GF (12).avi
15/09/2010  06:45 AM        17,037,976 GF (13).avi
15/09/2010  06:45 AM           668,494 GF (14).avi
15/09/2010  06:45 AM        33,422,756 GF (15).avi
15/09/2010  06:45 AM        21,128,992 GF (16).avi
15/09/2010  06:45 AM        23,537,194 GF (17).avi
15/09/2010  06:45 AM        20,266,966 GF (18).avi
15/09/2010  06:46 AM        28,177,226 GF (19).avi
15/09/2010  06:44 AM        19,919,346 GF (2).avi
15/09/2010  06:44 AM        18,056,900 GF (3).avi
15/09/2010  06:44 AM         2,690,740 GF (4).avi
15/09/2010  06:44 AM        39,466,326 GF (5).avi
15/09/2010  06:44 AM        47,864,220 GF (6).avi
15/09/2010  06:45 AM        32,951,816 GF (7).avi
15/09/2010  06:45 AM        18,330,400 GF (8).avi
15/09/2010  06:45 AM        39,266,124 GF (9).avi
              19 File(s)    458,944,768 bytes

 Directory of Y:\Tom\Program Integration\KNOWLEDGE MGMT\
 
11/08/2010  09:02 AM        28,177,226  (FR).avi
11/08/2010  09:02 AM        20,185,404 Labels (FR) .avi
09/08/2010  12:18 PM        39,266,124 Labels .avi
              3 File(s)    28,198,284 bytes


so i need this to be at the bottom

Code: Select all

28,198,284 bytes
458,944,768 bytes



the script that selects the values i need and appends it to the end of the file looks like this:

Code: Select all

 for /f "tokens=2*" %%a in ('findstr /c:"File(s)" "THEFILE.txt"') do (
for /f "tokens=2 delims=\" %%c in ("%%b") do -

Echo %%b
) >> THEFILE.txt



I just need to loop it so it does this to all my files in the directory. In the end all text files will have similar results at the bottom.

Any ideas?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: loop a script for multiple files

#4 Post by aGerman » 04 Apr 2011 10:59

Try it this way:

Code: Select all

@echo off &setlocal
(
  for /f "tokens=2*" %%a in ('findstr /c:"File(s)" "THEFILE.txt"') do echo %%b
)>"%temp%\tmpfile.txt"
move "%temp%\tmpfile.txt" "THEFILE.txt"

Regards
aGerman

cactusman252
Posts: 10
Joined: 08 Mar 2011 08:54

Re: loop a script for multiple files

#5 Post by cactusman252 » 05 Apr 2011 07:18

Not quite. I have multiple files in the same directory and need it so the script runs for each file separately. The trick is to read the file, grab the desired string or strings, append it to the bottom of that same file, then move on to the next txt file. So a loop would be required.

This does what i need but as you can see i have to specify the file name every time.

Code: Select all

for /f "tokens=2*" %%a in ('findstr /c:"File(s)" "THEFILE.txt"') do (
for /f "tokens=2 delims=\" %%c in ("%%b") do -

Echo %%b
) >> THEFILE.txt

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: loop a script for multiple files

#6 Post by aGerman » 05 Apr 2011 10:53

Hmm, this way?

Code: Select all

@echo off &setlocal
for %%a in (*.txt) do call :proc "%%~a"
del "%temp%\tmpfile.txt"
goto :eof

:proc
(
  for /f "tokens=2*" %%a in ('findstr /c:"File(s)" "%~1"') do echo %%b
)>"%temp%\tmpfile.txt"
type "%temp%\tmpfile.txt">>"%~1"


Regards
aGerman

cactusman252
Posts: 10
Joined: 08 Mar 2011 08:54

Re: loop a script for multiple files

#7 Post by cactusman252 » 05 Apr 2011 12:49

that was simpler than i thought it would be :shock:

Thanks a lot aGerman! You're the best

Post Reply