简体   繁体   中英

Batch File - Format Date/Time For Logging

I wrote a script that's executed by Task Scheduler every night at 23:00 to have NAS01 power on NAS02 if it's not already on, execute a GoodSync job to backup NAS01, and shutdown NAS02 upon completion. The script isn't pretty, but it does the job. The issue that I have is the timestamps. I set variables to set the format for the date and time, however, this will just reference the timestamp of the variable for the entire script. How do I keep the formatting, but be able to get the date and time at each point it's called?

As you can see from the script, there's a date/time format for the log filename, and a date/time format for the logging within the log file. What happens in my formatting:

  • Forced 24H time
  • Leading space in the hour is replaced with a '0' (IE " 6:30" becomes "06:30")
  • Time fomatting is HHMM for filename and HH:MM:SS for logging
  • Date formatting is YYYYMMDD for filename and YYYY/MM/DD for logging

Script:

@ECHO off
REM Change CMD window color and size.
color 37
mode 180,50

REM ### Set variables
SET IPADDRESS1=11.11.11.20
SET IPADDRESS2=10.0.3.1
SET HH=%time:~-11,2%
SET MM=%time:~-8,2%
SET SS=%time:~-5,2%
SET NAMETIMESTR=%HH: =0%%MM%
SET LOGTIMESTR=%HH: =0%:%MM%:%SS%
SET MYDATE=%DATE:~4,10%
SET NAMEDATESTR=%MYDATE:~6,4%%MYDATE:~0,2%%MYDATE:~3,2%
SET LOGDATESTR=%MYDATE:~6,4%/%MYDATE:~0,2%/%MYDATE:~3,2%
SET LOGFILE=C:\!SCRIPTS\logs\7xNAS02-GSync-%NAMEDATESTR%_%NAMETIMESTR%.txt


:START
    REM ### Checks IF 7xNAS02 is online. IF not, power it on.
    ECHO ################################################################################################################################ >> %LOGFILE%
    ECHO %LOGDATESTR% %LOGTIMESTR%  EXECUTING GSYNC SCRIPT >> %LOGFILE%
    ECHO ################################################################################################################################ >> %LOGFILE%
    ECHO.
    ECHO This is an automated script to backup the storage array on 7xNAS01 onto 7xNAS02.
    ECHO IF 7xNAS02 isn't powered on, the script will boot 7xNAS02, wait for it to come online,
    ECHO and then execute the "STORAGE" backup job in GoodSync.
    ECHO.
    ECHO %LOGDATESTR% %LOGTIMESTR%  Checking power status of 7xNAS02... >> %LOGFILE%
    ECHO Checking power status of 7xNAS02...
    ECHO.
    ping -n 1 %ipaddress1% | findstr TTL && GOTO ALREADY_ON
    ping -n 1 %ipaddress1% | findstr TTL || GOTO POWER_ON


:ALREADY_ON
    REM Sets variable to dictate that the server was already on before executing the script.
    ECHO.
    ECHO %LOGDATESTR% %LOGTIMESTR%  7xNAS02 is already powered on and available. >> %LOGFILE%
    ECHO 7xNAS02 is already powered on and available.
    ECHO.
    SET previously_off=1
    GOTO EXECUTE_BACKUP


:POWER_ON
    REM ### Powers on 7xNAS02
    ECHO %LOGDATESTR% %LOGTIMESTR%  7xNAS02 is offline. Powering on now. >> %LOGFILE%
    ECHO 7xNAS02 is offline. Powering on now.
    ECHO.
    SET previously_off=0
    PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'C:\!SCRIPTS\7xNAS02-Power-On.ps1'" >> %LOGFILE%
    ECHO %LOGDATESTR% %LOGTIMESTR%  Pinging 7xNAS02 until it's available... >> %LOGFILE%
    ECHO Pinging 7xNAS02 until it's available...
    ECHO.
    GOTO CHECK_IF_READY


:CHECK_IF_READY
    REM ### Continually pings 7xNAS02 until it is online.

    ping -n 1 %ipaddress1% | findstr TTL || GOTO CHECK_IF_READY
    ping -n 1 %ipaddress1% | findstr TTL && GOTO EXECUTE_BACKUP


:EXECUTE_BACKUP
    REM ### Executes GoodSync backup job, "STORAGE."
    ECHO %LOGDATESTR% %LOGTIMESTR%  Executing "STORAGE" backup job. >> %LOGFILE%
    ECHO Executing "STORAGE" backup job.
    ECHO.
    C:\"Program Files"\"Siber Systems"\Goodsync\gsync sync "STORAGE" >> %LOGFILE%
    IF %previously_off% == 0 (
        ECHO.
        ECHO %LOGDATESTR% %LOGTIMESTR%  Sync job done. Shutting down 7xNAS02... >> %LOGFILE%
        ECHO Sync job done. Shutting down 7xNAS02...
        shutdown /m \\7xNAS02 /s /f /d p:0:0 /c "Executed by 7xNAS02-GSync-Script on 7xNAS01."
        GOTO SHUTDOWNCHECK

    )
    IF %previously_off% == 1 (
        ECHO.
        ECHO %LOGDATESTR% %LOGTIMESTR%  Sync job done. 7xNAS02 will stay powered on. >> %LOGFILE%
        ECHO Sync job done. 7xNAS02 will stay powered on.
        ECHO.
    )
    GOTO END


:SHUTDOWNCHECK
    ping -n 5 %ipaddress1% >nul
    IF ERRORLEVEL 1 (
        REM ### 7xNAS02 has powered off.
        ECHO.
        ECHO %LOGDATESTR% %LOGTIMESTR%  7xNAS02 has powered off. >> %LOGFILE%
        ECHO 7xNAS02 has powered off.
        GOTO END
    )
    REM ### 7xNAS02 is still on. Looping to check IF the server has shut down.
    ECHO ...
    GOTO SHUTDOWNCHECK  


:END
    REM pause
    ECHO %LOGDATESTR% %LOGTIMESTR%  END OF SCRIPT >> %LOGFILE%
    exit

EDIT: Thanks to @Mofi, the updated and working script is below.

@ECHO off
REM Change CMD window color and size.
color 37
mode 180,50

REM ### Set variables
SET IPADDRESS1=11.11.11.20
SET IPADDRESS2=10.0.3.1
CALL :GETDATETIME
SET LOGFILE=C:\!SCRIPTS\logs\7xNAS02-GSync-%NAMEDATESTR%_%NAMETIMESTR%.txt


:START
    REM ### Checks IF 7xNAS02 is online. IF not, power it on.
    ECHO ################################################################################################################################ >> %LOGFILE%
    CALL :GETDATETIME
    ECHO %LOGDATESTR% %LOGTIMESTR%  EXECUTING GSYNC SCRIPT >> %LOGFILE%
    ECHO ################################################################################################################################ >> %LOGFILE%
    ECHO/
    ECHO This is an automated script to backup the storage array on 7xNAS01 onto 7xNAS02.
    ECHO IF 7xNAS02 isn't powered on, the script will boot 7xNAS02, wait for it to come online,
    ECHO and then execute the "STORAGE" backup job in GoodSync.
    ECHO/
    CALL :GETDATETIME
    ECHO %LOGDATESTR% %LOGTIMESTR%  Checking power status of 7xNAS02... >> %LOGFILE%
    ECHO Checking power status of 7xNAS02...
    ECHO/
    ping -n 1 %ipaddress1% | findstr TTL && GOTO ALREADY_ON
    ping -n 1 %ipaddress1% | findstr TTL || GOTO POWER_ON


:ALREADY_ON
    REM Sets variable to dictate that the server was already on before executing the script.
    ECHO/
    CALL :GETDATETIME
    ECHO %LOGDATESTR% %LOGTIMESTR%  7xNAS02 is already powered on and available. >> %LOGFILE%
    ECHO 7xNAS02 is already powered on and available.
    ECHO/
    SET previously_off=1
    GOTO EXECUTE_BACKUP


:POWER_ON
    REM ### Powers on 7xNAS02
    CALL :GETDATETIME
    ECHO %LOGDATESTR% %LOGTIMESTR%  7xNAS02 is offline. Powering on now. >> %LOGFILE%
    ECHO 7xNAS02 is offline. Powering on now.
    ECHO/
    SET previously_off=0
    PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'C:\!SCRIPTS\7xNAS02-Power-On.ps1'" >> %LOGFILE%
    CALL :GETDATETIME
    ECHO %LOGDATESTR% %LOGTIMESTR%  Pinging 7xNAS02 until it's available... >> %LOGFILE%
    ECHO Pinging 7xNAS02 until it's available...
    ECHO/
    GOTO CHECK_IF_READY


:CHECK_IF_READY
    REM ### Continually pings 7xNAS02 until it is online.

    ping -n 1 %ipaddress1% | findstr TTL || GOTO CHECK_IF_READY
    ping -n 1 %ipaddress1% | findstr TTL && GOTO EXECUTE_BACKUP


:EXECUTE_BACKUP
    REM ### Executes GoodSync backup job, "STORAGE."
    CALL :GETDATETIME
    ECHO %LOGDATESTR% %LOGTIMESTR%  Executing "STORAGE" backup job. >> %LOGFILE%
    ECHO Executing "STORAGE" backup job.
    ECHO/
    C:\"Program Files"\"Siber Systems"\Goodsync\gsync sync "STORAGE" >> %LOGFILE%
    IF %previously_off% == 0 (
        ECHO/
        CALL :GETDATETIME
        ECHO %LOGDATESTR% %LOGTIMESTR%  Sync job done. Shutting down 7xNAS02... >> %LOGFILE%
        ECHO Sync job done. Shutting down 7xNAS02...
        shutdown /m \\7xNAS02 /s /f /d p:0:0 /c "Executed by 7xNAS02-GSync-Script on 7xNAS01."
        GOTO SHUTDOWNCHECK

    )
    IF %previously_off% == 1 (
        ECHO/
        CALL :GETDATETIME
        ECHO %LOGDATESTR% %LOGTIMESTR%  Sync job done. 7xNAS02 will stay powered on. >> %LOGFILE%
        ECHO Sync job done. 7xNAS02 will stay powered on.
        ECHO/
    )
    GOTO END


:SHUTDOWNCHECK
    ping -n 5 %ipaddress1% >nul
    IF ERRORLEVEL 1 (
        REM ### 7xNAS02 has powered off.
        ECHO/
        CALL :GETDATETIME
        ECHO %LOGDATESTR% %LOGTIMESTR%  7xNAS02 has powered off. >> %LOGFILE%
        ECHO 7xNAS02 has powered off.
        GOTO END
    )
    REM ### 7xNAS02 is still on. Looping to check IF the server has shut down.
    ECHO ...
    GOTO SHUTDOWNCHECK  


:END
    REM pause
    CALL :GETDATETIME
    ECHO %LOGDATESTR% %LOGTIMESTR%  END OF SCRIPT >> %LOGFILE%
    exit


:GETDATETIME
    SET "HH=%time:~-11,2%"
    SET "MM=%time:~-8,2%"
    SET "SS=%time:~-5,2%"
    SET "NAMETIMESTR=%HH: =0%%MM%"
    SET "LOGTIMESTR=%HH: =0%:%MM%:%SS%"
    SET "MYDATE=%DATE:~4,10%"
    SET "NAMEDATESTR=%MYDATE:~6,4%%MYDATE:~0,2%%MYDATE:~3,2%"
    SET "LOGDATESTR=%MYDATE:~6,4%/%MYDATE:~0,2%/%MYDATE:~3,2%"
    GOTO :EOF

A simple solution for using region dependent but faster accessed DATE and TIME environment variables is the usage of a subroutine.

Append to end of your batch file after last command exit the lines:

:GetDateTime
SET "HH=%time:~-11,2%"
SET "MM=%time:~-8,2%"
SET "SS=%time:~-5,2%"
SET "NAMETIMESTR=%HH: =0%%MM%"
SET "LOGTIMESTR=%HH: =0%:%MM%:%SS%"
SET "MYDATE=%DATE:~4,10%"
SET "NAMEDATESTR=%MYDATE:~6,4%%MYDATE:~0,2%%MYDATE:~3,2%"
SET "LOGDATESTR=%MYDATE:~6,4%/%MYDATE:~0,2%/%MYDATE:~3,2%"
GOTO :EOF

This is the subroutine GetDateTime . And delete the lines at top of your batch file which also set those environment variables.

Above each line using either LOGDATESTR or LOGTIMESTR insert the line:

CALL :GetDateTime

Run in a command prompt window call /? for more information about this method to embed a batch file within a batch file and call it as subroutine.

One more hint: It is recommended to use echo/ instead of echo. , see DosTips forum topic: ECHO. FAILS to give text or blank line - Instead use ECHO/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM