Redirecting output on DOS batch files?

There’s a command line program I use that does operations on files. However it does it one file at a time. For example, if I have 3 files named 1.txt, 2.txt and 3.txt I have to type:

C:>xyz 1.txt
C:>xyz 2.txt
C:>xyz 3.txt

Imagine now that the files are 50+ and their names are random, ie do not have a predefined naming scheme.

I want to make a simple bat file that sends the output of dir /b command to xyz so I do not have to type each filename by hand.

Is this possible?

dir /b > list.txt

from memory

Not sure I see what you’re after but this will give you 50 files with the directory listing.

I know this works under the Win7 dos shell, but no idea about other versions.

for /l %i in (1 1 50) do dir /b>xyz%i

‘/l’ indicates that the set in parenthesis are start, increment and stop commands.

I think that will actually overwrite the file xyz, if it can find it.

You need single quotes to get FOR to use results of a command, like this:

for /f %a in (‘dir /b’) do xyz %a

Might need more work if your file names have spaces in them.

[edit] Oh yeah, use “delims=” :

for /f “delims=” %a in (‘dir /b’) do xyz %a

for %a in (*.txt) do xyz %a

Yeah, or that :smack:

Actually, assuming you have filenames with spaces, you’ll want


for %a in (*.txt) do xyz "%a"

Leaving out quotes is a much too common mistake.

Are you sure it’s > and isn’t | (pipe)

Pipe redirects output as input to next command, “>” and “>>” redirects output to a file only

Right and xyz is a command file so it should be pipe

xyz is a text file and therefore it is a >

pipe is to direct out put to another program that will act on it - you could further tht to a txt file but often its for narrowing down the screen display
netstat -an | find “listen”

vs

netsat -an > output.txt

I think another way to explain it is that the ‘>’ is a command that is specifically there to say ‘write this stuff to a file’

> == dont append - write new
>> append

So - its shorthand for "open texteditor, create new file called xyz.txt, write stuff to file and save’.

No it is not

xyz is a command line program, not a text file.

you have a .txt file extension - how is that a command line program?

I can write you an app that can do this for you.

Do all the .txt files reside in a single folder and nothing else resides there? If so, easy to do.

PM me if you want me to do it for you.

I reread your example now -and see the space

c:>xyz > 1.txt

will send the output of xyz to a new text file called 1.txt

c:>xyz >> 1.txt will append the output of xys to the 1.txt file - if it doesnt exist, it will be created.

If one of the things that xyz takes is a parm called 1.txt (a file to parse) -

c:>xyz 1.txt > output1.txt will do the same thing.

All this assumes, of course - that the ‘xyz’ puts data back out to the cmd window that you would see when you run it - if it does not, there is simply nothing to ‘redirect’ .

From what I’m understanding in the OP, he’s not wanting to redirect the output of xyz. He’s wanting to redirect the output of the dir command to call xyz with each filename it encounters.

Assuming this, it would be something like:

dir [param] would result in:

1.txt
2.txt
3.txt

He would then want xyz to be called with each filename as a parameter.

As I stated, as long as the .txt files are all in the same folder, I can write an app to let you do this.

You’d just need to setup the path to xyz and the path to the files you want as parameters when first running the app.

Also, any further files added to the data folder would automatically be added for you, too.

I would simply rethink the problem and a complicated program should not be needed -
new bat file -
dir > directory.txt
then read in directory.txt and loop thru each line and call xyz.

Right. So it would be
dir | xyz
but that is assuming that xyz can

  1. parse the dir to get the file names. there’s a lot more information than file names in the dir output
  2. handle a list of filenames and does not import the filename just from the command line parameter.

sigh

dir /b | xyz would address #1. “/b” eliminates all the extra information.

#2: If executable ‘xyz’ could handle a list of filenames we wouldn’t be here in the first place.

The question has been answered a couple miles back with the for-in constructions. There is no need for any additional half-baked ideas… the answer is right up there. I even tried to come up with a pure text-type solution and without "sed’ or similar I couldn’t do it.

Just for the record, “>”, “>>”, and “|” only redirect stdout, not stderr. That’s how you see the error message on the console if you try to do something like so:

dir /invalidparam > test.txt