user5578435
user5578435

Reputation: 25

Log file time stamp of a batch file is weird when called from second batch, work correct in silo

I have created 2 batch files called SetEnv.bat and Upload_File.bat. The upload_file.bat contains the SetEnv.bat to call first and then process other lines.

SetEnv.bat:

@echo off

    REM SET LOGFILE
    SETLOCAL
    SET DATE=%DATE:~-4%-%DATE:~-7,-5%-%DATE:~-10,-8%
    SET TIME=%Time:~0,2%%Time:~3,2%%Time:~6,2%
    SET LOGFILE=%~dp0\Logs\SetEnv_%DATE%-%TIME%.log
    CALL :Logit > %LOGFILE% 
    EXIT /b 0

    :Logit
    REM SET PARAMETERS
    SET LOGINID=******
    SET PASSWORD=C:\Users\xyz\pwd.epw
    SET URL=https://example.com
    SET DOMAIN=abc

    REM APPLICATION LOGIN`enter code here`
    CALL epmautomate login %LOGINID% %PASSWORD% %URL% %DOMAIN%
    IF %ERRORLEVEL% NEQ 0 (
    ECHO Login into source environment failed with error %ERRORLEVEL%.
    GOTO :END
    )

    :END
    SET returnValue=%ERRORLEVEL%
    EXIT /B %returnValue%

upload_file.bat:

    @echo off

    REM SET LOGFILE
    SETLOCAL
    SET DATE=%DATE:~-4%-%DATE:~-7,-5%-%DATE:~-10,-8%
    SET TIME=%Time:~0,2%%Time:~3,2%%Time:~6,2%
    SET LOGFILE=%~dp0\Logs\Upload_GL_%DATE%-%TIME%.log
    CALL :Logit > %LOGFILE% 
    EXIT /b 0


    :Logit

    REM LOGIN TO APPLICATION
    CALL SetEnv.bat

    REM OTHER LINES OF CODES
    ---------------------
    ---------------------
    ---------------------

Now the question is when I run SetEnv.bat outside of Upload_file.bat, it creates log file as SetEnv_2015-11-20-135220.log but When I call it inside the upload_file.bat, the log is showing SetEnv_1-20-5--20-1444.

I am not getting the reason behind it. Can I get any help?

Upvotes: 1

Views: 86

Answers (1)

Jason Faulkner
Jason Faulkner

Reputation: 6558

When you make your call to SetEnv.bat, you have already formatted the %DATE% and %TIME% variables in your upload_file.bat script, so the formatting you apply within your call to SetEnv.bat is with respect to values:

2015-11-20 (for Date)

135220 (for Time)

I would suggest not using the system %Date% and %Time% variables - instead use your own so you can detect if they have already been set:

IF NOT DEFINED LogDATE SET LogDATE=%DATE:~-4%-%DATE:~-7,-5%-%DATE:~-10,-8%
IF NOT DEFINED LogTIME SET LogTIME=%Time:~0,2%%Time:~3,2%%Time:~6,2%
SET LOGFILE=%~dp0\Logs\Upload_GL_%LogDATE%-%LogTIME%.log

The above will first check to see if %LogDATE% and %LogTIME% already have a value. If so, it will move on without recalcuating. Otherwise it will calculate the values.

This method allows you to set the timestamp in one script and then all other scripts will use it without redefining so you are going to get a consistent timestamp.

Upvotes: 1

Related Questions