Question

Redirect stderr and stdout from MSDOS promt

Asked by: qbert220

I'm running a perl script from a Windows 2000 Dos Prompt. This script generates output on stdout and stderr. I want to capture both of these into a single file. I can capture them both into seperate files using:

command.pl 1>stdout.txt 2>stderr.txt

What I want to do is:

command.pl 1&2>log.txt

Is this possible?

qbert

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2003-06-25 at 04:00:01ID20659435
Tags

redirect

,

dos

,

stderr

,

stdout

Topic

MS DOS

Participating Experts
4
Points
30
Comments
10

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Capturing STDERR Output
    I have a process that return text using STDERR. I need to run something like: @out=`process command`; To get the output, but this gets only STDOUT. How can I get the STDERR like that in an Easy way?
  2. Capture STDOUT as well as STDERR  in Windows NT
    Say, I have: print STDOUT "Output\n"; print STDERR "Error\n"; How do I in Windows to redirect or capture STDERR since Windows does not support UNIX convention such as `2>/tmp/error.txt`.
  3. Processing stderr
    Hi I was wondering whether it was possible in bash or other shells to do something specific with stderr, rather than just print to screen. Specifically is there anyway that stderr from any app can be printed using a different color on the screen? Other useful things could be ...
  4. redirect of stdout/stderr to stdin
    I need to execute a program and redirect its stdout/stderr to the stdin. Can someone please tell me how to do this or point me in the right direction.
  5. How to capture STDOUT and STDERR from perl embedded i…
    Hello, I have perl embedded in my C++ application approximately as follows: my_perl = perl_alloc(); perl_construct(my_perl); char **env = 0; char *embedding[] = { "", "-e", "0" }; perl_parse(my_perl, NULL, 3, embedding, env); ... Now when u...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: billiousPosted on 2003-06-25 at 06:20:58ID: 8797181

From the "Using command redirection operators" HELP on XP (2K should be the same)

Using the <& operator to redirect input and duplicate
To use the redirection input operator <&, the file you specify must already exist. If the input file exists, Cmd.exe opens it as read-only and sends the characters contained in the file as input to the command as if they were input from the keyboard. If you specify a handle, Cmd.exe duplicates the handle you specify onto the existing handle in the system.

For example, to open File.txt as input read to handle 0 (that is, STDIN), type:

<file.txt

To open File.txt, sort the contents and then send the output to the Command Prompt window (that is, STDOUT), type:

sort<file.txt

To find File.txt, and then redirect handle 1 (that is, STDOUT) and handle 2 (that is, STDERR) to the Search.txt, type:

findfile file.txt>search.txt 2<&1

To duplicate a user-defined handle 3 as input read to handle 0 (that is, STDIN), type:

<&3

Using the >& operator to redirect output and duplicate
If you redirect output to a file and you specify an existing file name, Cmd.exe opens the file as write-only and overwrites the file's contents. If you specify a handle, Cmd.exe duplicates the file onto the existing handle.

To duplicate a user-defined handle 3 into handle 1, type:

>&3

To redirect all of the output, including handle 2 (that is, STDERR), from the ipconfig command to handle 1 (that is, STDOUT), and then redirect the ouput to Output.log, type:

ipconfig.exe>>output.log 2>&1



...Not that Gates makes it easy to find!

...Bill (not /that/ Bill)

 

by: K_2KPosted on 2003-06-25 at 06:24:51ID: 8797223

Yes

   command.pl >log.txt 2>&1

From the W2K help file, Command reference, filter section (i believe this also works on older DOS after 3.2 psuedo-copied from POSIX/XENIX, but that is partly a guess):

The >& sign redirects the output from one of the default I/O streams (stdout, stdin, stderr) into another. For example, command >output_file 2>&1 redirects any error messages from the processing of command from the screen to the standard file output. The numeric values of the standard outputs are as follows: Standard output Numeric equivalent
Stdin 0
Stdout 1
Stderr 2

 

by: billiousPosted on 2003-06-25 at 06:45:51ID: 8797420

K_2K:

works in DOS 3.2+ - no, not even in DOS6.

> was implemented, but no being able to address stdout differently from stderr. IIRC, stdout was redirected, stderr stubbornly went to the screen (DOS 6 machine not feeling well at present, so I can't check...)


qbert220:
And caution....

using ">" alone, without the stream-specifier needs a space before it, since

command ..... filename.xt2>redir

will be interpreted as
command ..... filename.xt 2>redir

not
command ..... filename.xt2 >redir

...Bill



 

by: K_2KPosted on 2003-06-25 at 07:04:54ID: 8797620

Bill,

Thanks for the correction on my guess, and i would not have written my post during the 4 to 5 minutes while and after yours was up if i knew yours was there and complete.

Qbert,
plz ignore my answer when closing this to PAQ.  I've seen at least one too many questions lately not following EE's guide of "first correct answer gets it unless later answer was absolutely needed to help finish the fix"

Thanks.

2K

 

by: billiousPosted on 2003-06-25 at 07:25:54ID: 8797805

2K: We've discussed this phenomenon. No harm done. I'm no demon-typist so it happens to me, too.

Customer's problem resolved. What it's all about..

...Bill

 

by: happispiderPosted on 2004-02-06 at 16:22:29ID: 10295599

Maybe this will help:  The reserved filename "CON" can be used to copy to the screen in Command Prompt.

 

by: mcicogniPosted on 2004-12-28 at 14:24:18ID: 12915401

I'm running a python script (should be quite similar to how a perl script behaves) and I found out that STDIN redirection won't work, no matter how I write the command line.

STDOUT and STDERR redirection work as expected.

Anyone have a clue on what's going on?

Mauro

 

by: K_2KPosted on 2004-12-30 at 00:06:45ID: 12924576

As expected using Bill's syntax to get them to and from Python?  or as expected inside Python?  If you want different commands inside your python code to redirect from different files or handles, you should ask a fresh question in the Python or Pearle subject area.


Here's an example of a "hello world" dirivative that asks your name, with "Set /P" looking at STDIN:  You could write this in VBScript, Python, C#, or COBOL, but since we are in the MSDOS subject area, I'll choose that:
:::::::::::::::::::
:: test.cmd
::   test stdin redirection
echo off
setlocal
set zx=
set /p zx=Enter name please:
echo.
echo hello %zx%
:: end of test.cmd
:::::::::::::::::::


Now to use that from DOS in a way that demonstrates DOS STDIN redirection:
:::::::::::::::::::
C:\batch> echo harry > test.txt

C:\batch> test.cmd
Enter name please: tom

hello tom

C:\batch> test.cmd 0<test.txt
Enter name please:
hello harry
:::::::::::::::::::


Note that is all the same no matter which language the program is in, although we could create a pipe instead of using redirection:

:::::::::::::::::::
C:\batch> type test.txt | test.cmd
Enter name please:
hello harry
:::::::::::::::::::


So why bother?
Suppose I wanted the batch itself to default from a setup file, in case you do not answer.  That way the customer can change the default without opening the program:
:::::::::::::::::::
:: test.cmd
::   test stdin redirection
echo off
setlocal
0<test.txt set /p zx=
set /p zx=Enter name please (default %zx%) :
echo.
echo hello %zx%
:: end of test.cmd
:::::::::::::::::::


Now harry only needs to press enter (at least until tom changes the default file):
:::::::::::::::::::
C:\batch> test.cmd
Enter name please (default harry ) :

hello harry

C:\batch> test.cmd
Enter name please (default harry ) : tom

hello tom
:::::::::::::::::::


You should be able to plug your python command lines into a batch wrappper similar to that first set line, with as many different file names and python scripts as you need in one wrapper.


Hope This Helps (\o/),
2K

 

by: mcicogniPosted on 2004-12-30 at 01:46:15ID: 12924895

Hi, thanks for your comments.

I know it could be a different question however I found this thread while searching for good guesses to my problem and thought I'd add a comment here because I felt this really isn't a Python question.

Why? Because with "work as expected" I meant that if I wrote a command line like

C:\> script.py <inputfile >outputfile

I'd expect my script to get whatever is inside "inputfile" (assuming it exists) on the standard input, and to write it standard output into a freshly created (or overwritten) file called "outputfile".

What I get instead is that the script will raise an exception no matter what, lamenting that its input is non-existent.

If I write the script to also parse its command line so that I can write

C:\> script.py inputfile >outputfile

i.e. "inputfile" is an argument, and I let the script open and read the named file, I can in fact test that stdout redirection works (and BTW STDERR redirection also works too).

So my guess was that there could be some quirks in how the Windows command shell does things, and I posted the comment.

Meanwhile, experimenting around, I found out that if I invoke the interpreter explicitly, instead of relying on Windows' shell resolution, i.e.

C:\> python.exe script.py <inputfile >outputfile

then STDIN redirection works, too.

I wonder if this happens with any script interpreter (Perl, TCL, Ruby, etc.) or if it really is Python's fault.

 

by: K_2KPosted on 2004-12-30 at 17:02:27ID: 12930061

true,  this is a DOS question and related to this thread,  which is why I answered.  my line about the new question was in case you were having trouble in python, which i did not think was the case but possible.  Now i c you obviously have no such problem.  


I would try each of the following,  as the Python interpreter may be using specific handles when called by way of association as opposed to directly stating the .exe:

C:\>  script.py 0<inputfile >outputfile
C:\>  script.py 1<inputfile >outputfile


Now,  why does it work differently?  well,  you may or may not find that answer in the ftype command.  First enter:
C:\> assoc  .py

.py=pyfile

Then, use the value in the following.  For clarity we'll assume the above result, but you'll most likely need to adjust:
C:\> ftype  pyfile

Now THAT is the command that runs when you call script.py without telling it what program should be used.  For example, if
    pyfile=python.exe "%1" "%2"
then your first two parameters are being quoted and the rest ignored when you use "script.py" but not when you use "python.exe script.py"  
Not to mention, there could be two versions of the python interpreter, and the association may point to an ftype that calls the other one.
If there are other options on that command line, use python's documentation to figure it out,  or change it as you wish.  I recommend writing it in 3 separate easy to find places and mark them all "original" before changing it.

Hope this helps.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...