Search code examples
batch-filebatch-processingdos

Cannot write result of batch file processing using DOS START command


I have a batch file which will run multiple programs using DOS start command. However, I could not write the programs' result to their respective text file.

start program1.exe > result1.txt
start program2.exe > result2.txt

If my batch file is simply

program1.exe > result1.txt

Then the result can be written to result1.txt

Is there a problem in my syntax? Thank you.


Solution

  • As long as the programs write to stdout, you can get the output of the command called by Start by using a separate CMD and escaping the redirection operator

    Try this:

    start "" CMD /C program1.exe^>result1.txt
    start "" CMD /C program2.exe^>result2.txt
    

    Ex:

    c:\Scripts\Batch>start "" CMD /C ping -n 1 localhost>testping1.txt
    
    c:\Scripts\Batch>type testping1.txt
                *Nothing comes up because the file is empty*
    c:\Scripts\Batch>start "" CMD /C ping -n 1 localhost^>testping1.txt
    
    c:\Scripts\Batch>type testping1.txt
    
    Pinging YourComputer [::1] with 32 bytes of data:
    Reply from ::1: time<1ms
    
    Ping statistics for ::1:
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
    Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms