FOR /F on multiple files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
miskox
Posts: 555
Joined: 28 Jun 2010 03:46

FOR /F on multiple files

#1 Post by miskox » 10 Apr 2014 02:09

According to HELP FOR /F should be able to handle multiple files at once:

Code: Select all

for /F ["ParsingKeywords"] {%% | %}variable in (filenameset) do command [CommandLineOptions]

The filenameset argument specifies one or more file names. Each file is opened, read and processed before going on to the next file in filenameset. To override the default parsing behavior, specify "ParsingKeywords". This is a quoted string that contains one or more keywords to specify different parsing options.


So this should allow me to display all records in all .txt files in _some_dir folder:

Code: Select all

for /f "tokens=1" %%f in (_some_dir\*.txt) do echo %%f


This returns file not found error.

Jeb's solution on SO (http://stackoverflow.com/questions/9645 ... tch-script) :

Code: Select all

for %%f in (*.txt) do (
    FOR /F %%L IN (%%f) DO (
      echo %%L
  )
)


Jeb's solution works. Is Microsoft's version possible?

Saso

ShadowThief
Expert
Posts: 1163
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: FOR /F on multiple files

#2 Post by ShadowThief » 10 Apr 2014 06:19

you can say

Code: Select all

for /F %%f in ('dir \b somedir\*.txt') do echo %%f

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: FOR /F on multiple files

#3 Post by foxidrive » 10 Apr 2014 06:26

This is one way - it can be enhanced further to remove the filenames - if your records do not start with a : character

Code: Select all

findstr "^" *.txt

miskox
Posts: 555
Joined: 28 Jun 2010 03:46

Re: FOR /F on multiple files

#4 Post by miskox » 10 Apr 2014 06:54

@ShadowThief: your solution returns filenames only. I need each record from each file (total number of records without empty lines).

@foxi: your solution partially works: it shows empty lines, too.

I decided to use Jeb's solution for now.

Saso

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: FOR /F on multiple files

#5 Post by foxidrive » 10 Apr 2014 07:16

miskox wrote:@foxi: your solution partially works: it shows empty lines, too.


Did you say that you want to exclude blank lines?

If it's like the other thread and you have numerals in every record then replace "^" with "[0-9]"

miskox
Posts: 555
Joined: 28 Jun 2010 03:46

Re: FOR /F on multiple files

#6 Post by miskox » 10 Apr 2014 10:58

@foxi: my mistake. You are right. The main question in this thread is about FOR /F command not working as it should (according to the Operating System's help). My another thread is about blank lines.

Your solution is very good.

Sorry again.

Saso

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: FOR /F on multiple files

#7 Post by foxidrive » 10 Apr 2014 11:10

miskox, this works at the command prompt:

Code: Select all

for /f %f in (file1.txt file2.txt) do @echo %f

miskox
Posts: 555
Joined: 28 Jun 2010 03:46

Re: FOR /F on multiple files

#8 Post by miskox » 10 Apr 2014 11:16

I don't know what are the filenames so I tried with

Code: Select all

for /f %%f in (_some_folder\*.txt) set /a cnt=cnt+1


but it returns an error (though MS says it should work).

Looks like Jeb's solution is the one I will use (I already implemented it).

Thank you all. Foxi, sorry again for the misunderstanding.

Saso

penpen
Expert
Posts: 1996
Joined: 23 Jun 2013 06:15
Location: Germany

Re: FOR /F on multiple files

#9 Post by penpen » 10 Apr 2014 11:43

Where did Micrsoft say, that you may use wildcards, when using "for /F"?
Wildcards are only allowed on the 'pure' and '/D' "for" cmd, regarding "help for".

penpen

miskox
Posts: 555
Joined: 28 Jun 2010 03:46

Re: FOR /F on multiple files

#10 Post by miskox » 10 Apr 2014 13:14

penpen wrote:Where did Micrsoft say, that you may use wildcards, when using "for /F"?
Wildcards are only allowed on the 'pure' and '/D' "for" cmd, regarding "help for".

penpen


I re-read the help and you are right: I mistakenly thought that 'set' and 'filenameset' are the same (=same rules apply - obviously not) (set can have wildcards, filenameset is just a list of files).

Saso

Post Reply