4

I have a program that I want to automate runs for, since it takes awhile to complete. For some reason it outputs everything to stderr instead of stdout, and I'd like to check on its progress, so I find myself needing to redirect stderr output within a start command.

I tried this:

start "My_Program" "C:\Users\Me\my_program.exe" --some --presets --for 
--my_program.exe --output "C:\Users\Me\output_file_for_my_program" 
"C:\Users\Me\input_file_for_my_program" 2>"C:\Users\Me\my_program_output.log"

But it turns out that the redirect is being picked up by start, so that I get a 0-byte file with the result of start - namely, nothing. Is there any way to make the output redirection attach in some way to the output of my_program?

I've experimented with escaping, and neither ^2> nor 2^> seem to work. Any help would be greatly appreciated!

Stu Thompson
  • 38,370
  • 19
  • 110
  • 156

5 Answers5

5

Try this:

start "My_Program" "%SystemRoot%\System32\cmd.exe" /c ""C:\Users\Me\my_program.exe" --some --presets --for --my_program.exe --output "C:\Users\Me\output_file_for_my_program" "C:\Users\Me\input_file_for_my_program" 2>"C:\Users\Me\my_program_output.log""

Obviously, not having "My Program" here I can't test this, per se. If we take that the built-in "FIND.EXE" command returns "File not found - filename" on STDERR, the following is working for me:

start "My_Program" "%SystemRoot%\System32\cmd.exe" /c "find /v /i "blarg" "c:\not a real file.txt" 2> C:\stderr.txt"
Evan Anderson
  • 14,051
  • 3
  • 21
  • 14
5

Use /B switch. No new window created and redirections remain, but the command is run in background, just as needed.

start /B test.bat >test.txt <nul

test.bat:

@echo off
echo bbb
sleep 10
echo ccc
exit
basin
  • 3,949
  • 2
  • 27
  • 63
2

I used the following command and it worked:

start /affinity 2 /wait cmd.exe /C myprog.exe parameter1 parameter2 1^> .\a.log 2>> .\b.log

Ref: http://www.pcreview.co.uk/forums/redirect-standard-output-w-start-command-t1467634.html

Abhishek

2

How about putting the call to your command with the redirects in a batch file, and using start on the batch file?

MattB
  • 121
  • 2
  • I'd rather not use batch files, because this particular command needs to be run multiple times with different input and output files, as well as different arguments. As a last resort, I'll try them out, but for now I'm hoping there's another solution. On a side note, is there any way to close this question? I realised this isn't the right place to ask this question and have posted it on StackOverflow. –  Aug 20 '09 at 21:15
  • I flagged it. With any luck a moderator will wander along and move it to SO then we can close the one already on SO since it hasn't gotten a response yet. – EBGreen Aug 20 '09 at 21:19
  • 1
    If it comes to it - you can pass parameters into batch files as well. – MattB Aug 20 '09 at 21:28
  • 50/50 if it's SO or SF, as it's mostly SFers who do scripting. Anyhow MattB is correct, you can use %1 %2 %3 %4 etc as parameters, so it could be start "My_Program" "C:\Users\Me\my_program.exe" %1 %2 %3 %4 --output %5 %6 2>%7, and execute it as *"mybatch.bat" --some --presets --for --my_program.exe "C:\Users\Me\output_file_for_my_program" "C:\Users\Me\input_file_for_my_program" "C:\Users\Me\my_program_output.log"* – Mark Henderson Aug 20 '09 at 21:54
  • Mind you I had this problem with a command line program as well, and running it from a batch file and piping the output still didn't work. But it might work for you... – Mark Henderson Aug 20 '09 at 21:55
0

The following syntax seems to do the trick:

start /wait /b my_program.exe

This runs the program in the background and waits for it to complete. Redirect stdout and stderr as you prefer:

start /wait /b my_program.exe > output.txt 2>&1
Sam
  • 214
  • 1
  • 3
  • 7