0

I have a batch file that creates a log file with the date and time appended to the filename. This batch file is set to run a series of tasks every morning through Windows Task Scheduler. Every morning when the script runs, the batch file will only partially name the log file. It should be named report_log_2020-12-10_03-00-00.log, but the time doesn't always get added, resulting in report_log_2020-12-10_ (so it's not even a .log file, but a file with no extension).

When I manually run the batch script, the file is created properly. The file is also created properly when I manually run the task under Windows Task Scheduler. However, when the task runs in the mornings on it's own, that is when the time and .log is left off of the filename. This is a very odd behavior that has me baffled. Below is the code I am using.

::Created: 08/18/20
::Updated: 12/09/20

@ECHO OFF

::Set date/time
SET LOGFILE=Logs\report_log_%date:~-4,4%-%date:~-10,2%-%date:~-7,2%_%time:~0,2%-%time:~3,2%-%time:~6,2%.log
CALL :LOG >> %LOGFILE%
EXIT /B

:LOG

::Begin script
ECHO %date% %time%: ##### Refresh Begins #####

::Run Alteryx workflow
ECHO %date% %time%: Running Alteryx workflow...
CALL "C:\Program Files\Alteryx\bin\AlteryxEngineCmd.exe" "C:\Users\filepath\Workflow.yxmd"

::Replace Error Checking Files
ECHO %date% %time%: Replacing error checking file...
DEL "C:\Users\filepath\Error Check - Previous.xlsx"
RENAME "C:\Users\filepath\Error Check - New.xlsx" "Error Check - Previous.xlsx"

::Check for log file
ECHO %date% %time%: Checking if Oracle log exists...
PowerShell.exe -Command "& 'C:\Users\filepath\log_check.ps1'"

::End script
ECHO %date% %time%: ##### Refresh Complete #####

::PAUSE

::Finish
aschipfl
  • 33,626
  • 12
  • 54
  • 99
TaRan
  • 37
  • 10
  • 1
    Put quotes: `>> "%LOGFILE%"`… – aschipfl Dec 11 '20 at 03:59
  • Take a look at this thread: [How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?](https://stackoverflow.com/q/203090) – aschipfl Dec 11 '20 at 08:39
  • 1
    @aschipfl Thank you for the solution. It did fix the problem. Or more correctly, it helped identify what the real issue was. Once I put the logfile call in quotes, it began displaying properly in the file name, but showed that there was a leading space in the naming. Magoo's answer below removed the leading space. – TaRan Dec 14 '20 at 15:12

1 Answers1

2

After

SET LOGFILE=Logs\report_log_%date:~-4,4%-%date:~-10,2%-%date:~-7,2%_%time:~0,2%-%time:~3,2%-%time:~6,2%.log

add

SET "LOGFILE=%logfile: =0%"

The time and date formats are user-dependent, and may vary according to the user under which the process runs. Consequently, the time may contain a leading space for hours, so the filename is truncated at the space.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
Magoo
  • 77,302
  • 8
  • 62
  • 84
  • good point, better use local independent date/time `for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do...` – elzooilogico Dec 11 '20 at 06:45
  • Spaces being added to the time makes sense as to why the batch file doesn't complete the naming. However, sometimes it does - still not sure why. So `SET "LOGFILE=%logfile: =0%"` would go after the initial `SET LOGFILE`... and it eliminates the created spaces through `=0`, correct? I want to make sure I understand what it's doing. – TaRan Dec 11 '20 at 16:16
  • That's exactly what it should do. Of course, then there's the embarrassing situation where the date format might be set to a different sequence (m/y/d, d/m/y, y,m,d) or to 2-digit or 4-digit years... – Magoo Dec 11 '20 at 16:50
  • @Magoo Thanks a lot for your help. You hit the nail on the head. The time did have a leading space. The code snippet you provided replaced it with a 0 and worked great. I have a question though, is it usual for the time or date to have a leading space? I don't think I have have ever seen that anywhere else (and seems problematic to me). – TaRan Dec 14 '20 at 15:09