46

I would like to run two programs simultaneously from a batch file, and redirect the first program's output into a text file like:

start python 1st.py arg1 arg2 > out.txt
start 2nd.exe %1 arg2 arg3

While the programs run as expected, all output is shown on stdout.

4
  • This ought to work. Without knowing anything, I wonder if 1st.py is doing something funny with output.
    – zpletan
    Sep 21, 2011 at 17:17
  • No, but it writes to stderr.
    – none
    Sep 21, 2011 at 17:25
  • That's why—> only redirects stdout (I think). In any case, it looks like you already have what you need.
    – zpletan
    Sep 21, 2011 at 20:51
  • The reason this doesn't work is because you are redirecting the output of the start command and not python.exe, and there's no way to start something directly with a redirection when using start. You should just remove start entirely May 3, 2021 at 16:44

4 Answers 4

52

You might need to do it this way:

start cmd /c python 1st.py arg1 arg2 ^> out.txt
5
  • 1
    Thank you, but it does not work for me (out.txt is not even created in the local directory)
    – none
    Sep 21, 2011 at 17:52
  • I tested it with CScript and a VBscript, so the idea is sound. Are you doing something "extra" in your command line that's not listed here, like double quotes? Sep 21, 2011 at 18:19
  • Sorry, something went awfully wrong - it works!
    – none
    Sep 21, 2011 at 18:27
  • @Patrick Seymour, thank you for your answer! How can I do same thing without .bat file? This doesn't work from cmd: python.exe ^> python_out.txt. I had an error: python.exe: can't open file '>': [Errno 22] Invalid argument.
    – neo
    Dec 8, 2016 at 18:30
  • 3
    Note that for append mode, >> needs to be escaped like this: ^>^>
    – Fabian
    Dec 16, 2019 at 6:05
9

Additionally, if you want to redirect both stderr and stdout this works for me

start call delay.bat ^1^> log.txt ^2^>^&^1

It seems every character basically needs to be escaped. This command normally looks like this:

delay.bat 1> log.txt 2>&1

8

Redirection is applied to the start command, but somehow not to the cmd.exe instance it runs.

If the > operator is escaped, everything should work:

start 1st.py arg1 arg2 ^> out.txt

(If you want to redirect stderr as well, use 2^> for it.)

2
  • 6
    Thank you, but it does not work for me.
    – none
    Sep 21, 2011 at 17:52
  • 1
    This wouldnt work because the redirection is meaningless here, you aren't starting a command, you're starting a program. You only escape when youre starting cmd.exe /c where you are starting a program and supplying a command to it and you need to make sure that it interprets it as being part of the command string being passed to cmd May 3, 2021 at 16:44
8

What did the trick for me was moving the command into a separate batch file:

rem this first batch file triggers the second one:
start the_second.bat arg1 arg2 out.txt

the_second.bat then looks like this:

python 1st.py %1 %2 > %3
5
  • This is a very inelegant workaround compared to the correct answer.
    – DavidPostill
    Jul 27, 2016 at 6:58
  • 6
    The "correct answer" didn't work for me with a similiar problem and I thought it might be a solution people at least might want to know about. Jul 27, 2016 at 12:44
  • I have to disagree. Learning the correct way to escape characters in command is a much better idea.
    – DavidPostill
    Jul 27, 2016 at 12:46
  • 1
    Please teach me how to escape attaching output using >>. Jul 28, 2016 at 7:53
  • I already told you. Read the accepted answer.
    – DavidPostill
    Jul 28, 2016 at 8:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .