# Redirecting output on DOS batch files?

**URL:** <https://boards.straightdope.com/t/redirecting-output-on-dos-batch-files/636674>\
**Category:** Factual Questions\
**Created:** [October 4, 2012, 11:27am UTC](https://boards.straightdope.com/t/redirecting-output-on-dos-batch-files/636674 "2012-10-04T11:27:43Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dog80](https://avatars.discourse-cdn.com/v4/letter/d/6bbea6/32.png) [@Dog80](https://boards.straightdope.com/u/Dog80)\
**Post date:** [October 4, 2012, 11:27am UTC](https://boards.straightdope.com/t/redirecting-output-on-dos-batch-files/636674/1 "2012-10-04T11:27:43Z")

</div>

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:&gt;xyz 1.txt  
C:&gt;xyz 2.txt  
C:&gt;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?

---

<div class="post-metadata">

**Author:** ![spanna](https://avatars.discourse-cdn.com/v4/letter/s/a6a055/32.png) [@spanna](https://boards.straightdope.com/u/spanna)\
**Post date:** [October 4, 2012, 11:31am UTC](https://boards.straightdope.com/t/redirecting-output-on-dos-batch-files/636674/2 "2012-10-04T11:31:32Z")

</div>

dir /b \> list.txt

from memory

---

<div class="post-metadata">

**Author:** ![allotrope](https://avatars.discourse-cdn.com/v4/letter/a/edb3f5/32.png) [@allotrope](https://boards.straightdope.com/u/allotrope)\
**Post date:** [October 4, 2012, 11:48am UTC](https://boards.straightdope.com/t/redirecting-output-on-dos-batch-files/636674/3 "2012-10-04T11:48:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ximenean](https://avatars.discourse-cdn.com/v4/letter/x/aca169/32.png) [@Ximenean](https://boards.straightdope.com/u/Ximenean)\
**Post date:** [October 4, 2012, 12:49pm UTC](https://boards.straightdope.com/t/redirecting-output-on-dos-batch-files/636674/4 "2012-10-04T12:49:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Oukile](https://avatars.discourse-cdn.com/v4/letter/o/13edae/32.png) [@Oukile](https://boards.straightdope.com/u/Oukile)\
**Post date:** [October 4, 2012, 12:52pm UTC](https://boards.straightdope.com/t/redirecting-output-on-dos-batch-files/636674/5 "2012-10-04T12:52:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ximenean](https://avatars.discourse-cdn.com/v4/letter/x/aca169/32.png) [@Ximenean](https://boards.straightdope.com/u/Ximenean)\
**Post date:** [October 4, 2012, 1:04pm UTC](https://boards.straightdope.com/t/redirecting-output-on-dos-batch-files/636674/6 "2012-10-04T13:04:41Z")

</div>

Yeah, or that :smack:

---

<div class="post-metadata">

**Author:** ![BigT](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/bigt/32/12044_2.png) [@BigT](https://boards.straightdope.com/u/BigT)\
**Post date:** [October 7, 2012, 1:03pm UTC](https://boards.straightdope.com/t/redirecting-output-on-dos-batch-files/636674/7 "2012-10-07T13:03:06Z")

</div>

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

```auto

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

```

Leaving out quotes is a much too common mistake.

---

<div class="post-metadata">

**Author:** ![Saint\_Cad](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/saint_cad/32/18907_2.png) [@Saint\_Cad](https://boards.straightdope.com/u/Saint_Cad)\
**Post date:** [October 7, 2012, 1:44pm UTC](https://boards.straightdope.com/t/redirecting-output-on-dos-batch-files/636674/8 "2012-10-07T13:44:44Z")

</div>

> [@spanna](#):
>
> dir /b \> list.txt
> 
> from memory

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

---

<div class="post-metadata">

**Author:** ![RaftPeople](https://avatars.discourse-cdn.com/v4/letter/r/6f9a4e/32.png) [@RaftPeople](https://boards.straightdope.com/u/RaftPeople)\
**Post date:** [October 7, 2012, 4:49pm UTC](https://boards.straightdope.com/t/redirecting-output-on-dos-batch-files/636674/9 "2012-10-07T16:49:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Saint\_Cad](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/saint_cad/32/18907_2.png) [@Saint\_Cad](https://boards.straightdope.com/u/Saint_Cad)\
**Post date:** [October 7, 2012, 9:08pm UTC](https://boards.straightdope.com/t/redirecting-output-on-dos-batch-files/636674/10 "2012-10-07T21:08:37Z")

</div>

> [@RaftPeople](#):
>
> 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

---

<div class="post-metadata">

**Author:** ![simster](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/simster/32/1047_2.png) [@simster](https://boards.straightdope.com/u/simster)\
**Post date:** [October 7, 2012, 9:13pm UTC](https://boards.straightdope.com/t/redirecting-output-on-dos-batch-files/636674/11 "2012-10-07T21:13:54Z")

</div>

> [@Saint\_Cad](#):
>
> 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

---

<div class="post-metadata">

**Author:** ![simster](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/simster/32/1047_2.png) [@simster](https://boards.straightdope.com/u/simster)\
**Post date:** [October 8, 2012, 2:01am UTC](https://boards.straightdope.com/t/redirecting-output-on-dos-batch-files/636674/12 "2012-10-08T02:01:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Saint\_Cad](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/saint_cad/32/18907_2.png) [@Saint\_Cad](https://boards.straightdope.com/u/Saint_Cad)\
**Post date:** [October 8, 2012, 3:05am UTC](https://boards.straightdope.com/t/redirecting-output-on-dos-batch-files/636674/13 "2012-10-08T03:05:27Z")

</div>

> [@simster](#):
>
> xyz is a text file and therefore it is a \>

No it is not

> [@Dog80](#):
>
> There’s a command line program I use that does operations on files. \<snip\>  
> **C:&gt;xyz 1.txt  
> C:&gt;xyz 2.txt  
> C:&gt;xyz 3.txt**

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

---

<div class="post-metadata">

**Author:** ![simster](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/simster/32/1047_2.png) [@simster](https://boards.straightdope.com/u/simster)\
**Post date:** [October 8, 2012, 1:13pm UTC](https://boards.straightdope.com/t/redirecting-output-on-dos-batch-files/636674/14 "2012-10-08T13:13:10Z")

</div>

> [@Saint\_Cad](#):
>
> 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?

---

<div class="post-metadata">

**Author:** ![Orionizer](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/orionizer/32/4442_2.png) [@Orionizer](https://boards.straightdope.com/u/Orionizer)\
**Post date:** [October 8, 2012, 1:24pm UTC](https://boards.straightdope.com/t/redirecting-output-on-dos-batch-files/636674/15 "2012-10-08T13:24:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![simster](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/simster/32/1047_2.png) [@simster](https://boards.straightdope.com/u/simster)\
**Post date:** [October 8, 2012, 2:12pm UTC](https://boards.straightdope.com/t/redirecting-output-on-dos-batch-files/636674/16 "2012-10-08T14:12:57Z")

</div>

> [@Saint\_Cad](#):
>
> No it is not  
> xyz is a command line **program** , not a text file.

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

---

<div class="post-metadata">

**Author:** ![Orionizer](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/orionizer/32/4442_2.png) [@Orionizer](https://boards.straightdope.com/u/Orionizer)\
**Post date:** [October 8, 2012, 3:01pm UTC](https://boards.straightdope.com/t/redirecting-output-on-dos-batch-files/636674/17 "2012-10-08T15:01:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![simster](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/simster/32/1047_2.png) [@simster](https://boards.straightdope.com/u/simster)\
**Post date:** [October 8, 2012, 3:47pm UTC](https://boards.straightdope.com/t/redirecting-output-on-dos-batch-files/636674/18 "2012-10-08T15:47:33Z")

</div>

> [@Orionizer](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![Saint\_Cad](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/saint_cad/32/18907_2.png) [@Saint\_Cad](https://boards.straightdope.com/u/Saint_Cad)\
**Post date:** [October 8, 2012, 6:37pm UTC](https://boards.straightdope.com/t/redirecting-output-on-dos-batch-files/636674/19 "2012-10-08T18:37:17Z")

</div>

> [@Orionizer](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![core\_dump](https://avatars.discourse-cdn.com/v4/letter/c/dec6dc/32.png) [@core\_dump](https://boards.straightdope.com/u/core_dump)\
**Post date:** [October 9, 2012, 5:59pm UTC](https://boards.straightdope.com/t/redirecting-output-on-dos-batch-files/636674/20 "2012-10-09T17:59:45Z")

</div>

> [@Saint\_Cad](#):
>
> 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

[Next page](https://boards.straightdope.com/t/redirecting-output-on-dos-batch-files/636674.md?page=2)
