1

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
Guestík
  • 113
  • 1
  • 9
  • Do you have tried the __START__ command line posted by you? It writes the error message output by __START__ into file `log.txt` in addition to the displayed GUI error message prompt. It would be necessary to escape each redirection operator `>` and `&` with `^` to be not interpreted as redirection of output of command __START__ in current command process. – Mofi Oct 03 '18 at 17:29
  • Of course I have tried. When there is an error, it is redirected to the log.txt successfully. But when it finds the process and run it, the output of the process is also being redirected, which I do not want. – Guestík Oct 03 '18 at 18:03
  • When I tried to adjust the line as you told me (start "" "path\to the\process.exe" ^>^> log.txt 2^>^&1), the process is not being redirected (which is fine), but the error message is not redirected to the log file too. Am I missing something? – Guestík Oct 03 '18 at 18:06
  • 2
    I can't reproduce your problem. The started process does not block the next echo in my hands. Also the started process does not write to the log. Please give more information about your process, and make sure you tell us all START parameters you are using. If at all possible, you should give us the exact start line you are using. – dbenham Oct 03 '18 at 18:41
  • Talking about this line: start "" "path\to the\process.exe" ^>^> log.txt 2^>^&1 What I want to achive is, when the PATH to the process is wrongly written or just simply the process.exe does not exist, redirect the output error message to the log.txt file. Because now, it writes the error message to the console window. – Guestík Oct 03 '18 at 18:57
  • @Guestík I wrote that `start "" "path\to the\process.exe" >> log.txt 2>&1` results in getting error message of __START__ written into the file `log.txt` which you want. `start "" "path\to the\process.exe" ^>^> log.txt 2^>^&1` is the example for writing standard and error output of `process.exe` into file `log.txt` which you don't want. So as [dbenham](https://stackoverflow.com/users/1012053/dbenham) wrote it is unclear for us what is your problem because what you posted is what you want and what works. – Mofi Oct 04 '18 at 05:11

1 Answers1

1

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
jwdonahue
  • 6,199
  • 2
  • 21
  • 43
  • While your answer is working I have nevertheless two suggestions for improvements. 1. On all operating systems it is highly recommended not exiting an application or script with a negative number. It is possible, but used should be 0 for success and a positive number on an error. So please change end of second line to `exit /b 1`. 2. Avoid using `if %ERRORLEVEL% NEQ 0` as this works only outside a command block . Better is using `if errorlevel 1` which works anywhere in a batch file and means if exit code of `start` is greater or equal 1, i.e. if there was an error on starting the executable. – Mofi Oct 04 '18 at 05:26
  • Run in a command prompt window `if /?` for details on `if errorlevel X` syntax. The opposite is `if not errorlevel 1` which means if exit code is less than 1, i.e. is equal 0 if the application/script does not exit with a negative value which is true for all applications and scripts I have ever seen except this one. See [What are the ERRORLEVEL values set by internal cmd.exe commands?](https://stackoverflow.com/questions/34987885/) for exit codes of command __START__. – Mofi Oct 04 '18 at 05:31
  • @Mofi, thanks for the tips. I forgot to lookup the correct error message! -1 is my "note to self".... I don't ever put if statements inside a command block. I avoid multi-line code blocks all-together, they should be avoided at all costs. – jwdonahue Oct 04 '18 at 17:32
  • The `if errorlevel 1` semantics are an [oxymoron](https://www.dictionary.com/browse/oxymoron). BTW, many programs return negative numbers for errors and zero or positive numbers for success. You should never assume that scripts return positive values for failures. Windows batch scripting language is weak and full of bugs. – jwdonahue Oct 04 '18 at 17:49
  • What `if errorlevel 1` means is clearly explained in help of command __IF__. As always in programming world the documentation of a function/command matters and not the user's understanding of the function/command. I would be really interested in which programs return a negative number. In 25 years of computer usage I have never seen one. There is clearly written by Microsoft for Windows and in Linux kernel documentation that negative numbers should not be used by applications. Best is to use only values in range 0 to 255 (unsigned char value range) or 0 to 65535 (unsigned short value range). – Mofi Oct 05 '18 at 05:03
  • The reason is the function `int main(int argc, char* argv[])`. `int` being defined in C/C++ of being capable of containing __at least__ the [−32,767, +32,767] range. So `int` can be just 16 bit as on 16 bit controllers, 32 bit or 64 bit depending on used compiler and for which processor/controller the program is compiled. Using negative values can cause troubles on used compiler interprets `int` with a different bit width than the used operating system or another application evaluating the exit code. For that reason the usage of a negative value is not forbidden, but really not recommended. – Mofi Oct 05 '18 at 05:11
  • But you are right, nobody should ever assume what a program returns, but read its documentation, or find it out if the documentation does not describe it, or contact the author of the program and ask for clarification regarding to exit codes. For example [HTML Help Workshop](https://stackoverflow.com/a/39040033/3074564) returns 0 on an error and 1 on success. That's the only application I know which use these opposite exit codes as usually used by programs according to the guidelines of the programmers of the operating systems. – Mofi Oct 05 '18 at 05:18