1

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%
)

)
Pete Hilde
  • 659
  • 1
  • 10
  • 24
  • Your exe file is an console application, right? You shouldn't use `START`, just same command in batch as from command line. – LS_ᴅᴇᴠ Oct 09 '17 at 12:14
  • @LS_ᴅᴇᴠ Yes, my exe is a console application. I already tried using the same command that I'm using on the command line in the batch file, but the log is still empty when the task was executed. – Pete Hilde Oct 09 '17 at 12:18
  • Makes no sense! Better post your batch. Also, same permissions used from command line/batch? – LS_ᴅᴇᴠ Oct 09 '17 at 12:25
  • My exe requires elevated privileges that I'm giving it by clicking "yes" when the UAC prompt is shown. When the cmd window isn't run as administrator, it asks for permission, otherwise it works fine without any prompts. SAme for the batch file. – Pete Hilde Oct 09 '17 at 12:37
  • 3
    If so, start your batch with elevated privileges. – LS_ᴅᴇᴠ Oct 09 '17 at 12:42
  • That fixed the problem, thanks! But why? Is there any difference between directly running the batch file as an admin and not running it as an admin but granting the privileges by accepting the UAC prompt? – Pete Hilde Oct 09 '17 at 12:58
  • 3
    Your command line `START ... > ...` redirects START output which is none, your application is outputing in another window. – LS_ᴅᴇᴠ Oct 09 '17 at 12:59
  • About use without `START`...Can regular user write in `"C:\MyAgent\EXE\logs"`? – LS_ᴅᴇᴠ Oct 09 '17 at 13:01
  • Yes, a non-admin user can also write in the specified path. – Pete Hilde Oct 09 '17 at 13:03
  • 2
    I'm not sure and can't test right now, but I think with UAC, exe is started in a new window/process and like `start`, no output is received in batch window. – LS_ᴅᴇᴠ Oct 09 '17 at 13:07

1 Answers1

0

you can use script command in this way:

$script out.txt

and then you can execute your program noramally.

Once you are done, press from the keyboard CRTL+D

Mariam
  • 111
  • 2