2

I am redirecting the output of a Python script to a file. It works fine if there is no error. But if there is any error , I wish to capture the error in another file which is not happening now.

Below is the script which I have written.

@echo off
mode con cp select=65001
set dt=%Date:~10,4%-%Date:~4,2%-%Date:~7,2%

cd C:\API_DOC\softeon_project\script
python -u softeon_main.py >>C:\API_DOC\softeon_project\log\log_%dt%.txt 2>>C:\API_DOC\softeon_project\log\logerr_%dt%.txt 
echo "after python path"
pause
exit

Any help would be appreciable.

Mofi
  • 46,139
  • 17
  • 80
  • 143
Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45
  • `python test.py > log.log 2> err.log` works for me on ubuntu. Maybe it doesn't work on windows? – Sraw Dec 26 '17 at 08:22
  • I tried it in windows it works. maybe you should first run the code without output redirection and check whether any error is getting displayed or not. – eiram_mahera Dec 26 '17 at 08:58
  • @Sraw: Yes, it is not working on windows 10. I have all the proper folder structure. – Ranadip Dutta Dec 26 '17 at 09:56
  • @eiram_mahera: No, without redirection it is working fine. Even the output is getting captured. But in case of error, it is not coming – Ranadip Dutta Dec 26 '17 at 09:56
  • It seems the issue is lying `log_%dt%.txt`, the date variable is causing the issue. How to create the file with the current date and passing it as variable – Ranadip Dutta Dec 26 '17 at 10:03

1 Answers1

1

The usage of dynamic environment variable DATE depends on Windows Region setting as defined for the currently used user account.

For example with getting Tue 12/26/2017 written into a command prompt window on running in same window echo %DATE% it is possible to use either

set "dt=%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%"

or better

set "dt=%DATE:~-4%-%DATE:~-10,2%-%DATE:~-7,2%"

Both command lines use string substitutions to get environment variable dt defined with string 2017-12-26. The difference is that the first command line references the characters in date string from left while the second command line references them from right. Therefore the second command line works also with no abbreviated weekday at beginning.

The help output for command SET on running set /? in a command prompt window explains string substitutions as used here.

A region independent solution to get current local date in format yyyy-MM-dd would be:

for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "dt=%%I"
set "dt=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%"

This variant is explained in detail in answer on Why does %date% produce a different result in batch file executed as scheduled task?

The disadvantage is that WMIC takes more than a second to output the local date and time which makes this solution much slower than the solution using dynamic environment variable DATE.

I suggest to use:

@echo off
rem Define encoding UTF-8 for console.
%SystemRoot%\System32\mode.com CON CP SELECT=65001

rem Get current local date in format yyyy-MM-dd.
set "dt=%DATE:~-4%-%DATE:~-10,2%-%DATE:~-7,2%"

rem Change the current directory independent on current drive.
cd /D C:\API_DOC\softeon_project\script

rem Execute Python interpreter and redirect standard output messages
rem to file log_%dt%.txt and error messages to logerr_%dt%.txt.
python.exe -u softeon_main.py >>C:\API_DOC\softeon_project\log\log_%dt%.txt 2>>C:\API_DOC\softeon_project\log\logerr_%dt%.txt

echo "After python path"
pause

There was a trailing space in Python command line which is removed in code above. See the answers on Why does ECHO command print some extra trailing space into the file? and
Why is no string output with 'echo %var%' after using 'set var = text' on command line? why a trailing space in a batch file could result in an unexpected output into a file or even unexpected behavior on execution of a batch file.

And python was extended with file extension .exe to avoid that by chance a file python.bat or python.cmd is found first by Windows command interpreter as in this case the next line would not be executed anymore because batch files must be called from within a batch file with command CALL to return to calling batch file on finishing execution of called batch file.

Read also the Microsoft article about Using command redirection operators for an explanation of >> and 2>>.

Mofi
  • 46,139
  • 17
  • 80
  • 143