I'm currently writing a batch file in which I'm running an exe that was written by myself. I'm redirecting the standard output as well as the standard error output in a specific file.
My command inside the batch file looks like this:
"MyExe.exe" /weekly config.xml > "C:\MyPath\testlog.log" 2>&1
Everything works fine as long as I'm executing the command from the command line. The output of my application is shown in the same command window. testlog.log then contains all of the output I've just seen in the command window.
As soon as I run it from a batch file, a new second window, besides the command window that was opened by the batch file, opens and shows the output of my application. When the application is finished, the second window closes and the remaining actions in my batch file are executed. The logfile itself is empty.
I already know that there are different processes and that the output of one can't be redirected to the other one.
That's why I used the start
command inside the batch file to let the output show up in the command window that was opened by the batch file.
I already tried these solutions:
start /wait /b "MyExe.exe" /weekly config.xml > "C:\MyPath\testlog.log" 2>&1
start /b "C:\FullPath\MyExe.exe" /weekly config.xml > "C:\MyPath\testlog.log" 2>&1
But none of them make the output of my application show up in the same command window.
Moreover, I already tried all of the solutions from this and this question. I always get an empty output or an empty file.
Is it even possible to redirect the output of my application when calling it like that? Or does it depend on the kind of application, e.g. console/windows application?
EDIT: Here is my batch file:
SET LOGS_LOCAL="C:\MyAgent\EXE\logs\*.log"
SET LOGS_SERVER="\\MyServer\Backups\weekly\logs"
SET SOURCE_ARCHIVES="C:\MyAgent\Backups\weekly\*.zip"
SET TARGET_ARCHIVES_SERVER="\\MyServer\Backups\weekly"
SET CONFIGFILE="config.xml"
SET COUNTER=0
SET MAXLOGS=30
SET Timestamp=%date:~6,4%-%date:~3,2%-%date:~0,2%-%time:~0,2%-%time:~3,2%-%time:~6,2%
echo %Timestamp%
pause
REM Run the exe
"MyExe.exe" /weekly %CONFIGFILE% > "C:\MyAgent\EXE\logs\log %Timestamp%.log" 2>&1
pause
REM Check return value of exe
IF %ERRORLEVEL% EQU 0 OR %ERRORLEVEL% GEQ 8 (
REM Get latest local archive and copy on server
FOR /F "delims=|" %%I IN ('DIR %SOURCE_ARCHIVES% /B /O:D /S') DO SET NewestArchive=%%I
copy "%NewestArchive%" %TARGET_ARCHIVES_SERVER%
REM Get local count of logs
for %%A in (%LOGS_LOCAL%) do set /a COUNTER+=1
REM Delete oldest log whne maximum is reached
IF %COUNTER% LEQ %MAXLOGS% (
REM Everything fine, no work to be done
) else (
REM Get oldest log
FOR /f "delims=|" %%a in ('dir %LOGS_LOCAL% /t:c /a:-d /o:-d /b /s') do set OldestLog="%%a"
REM Delete oldest log
DEL %OldestLog%
)
)