Search code examples
batch-fileoutput-redirect

batch redirection of start command but not the process


I need to redirect output of the start command to the log.txt file (in case of the path is wrong for example) but not of the process it launches. Because if the process.exe is running longer time, the next output can not be written to the log file, because it is locked by the process.

start "" "path\to the\process.exe" >> log.txt 2>&1
echo next output >> log.txt

Solution

  • One option:

    @set _FILE_NOT_FOUND=2
    @set _fileToRun=path\to the\process.exe
    @if not exist "%_fileToRun%" @echo File to run does not exist: %_fileToRun% >> log.txt & @exit /b -%_FILE_NOT_FOUND%
    @start "" "%_fileToRun%"
    @if %ERRORLEVEL% neq 0 @echo Start failed with: %ERRORLEVEL%
    @echo next output >> log.txt