Batch Script does NOT Update Time


  1. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #1

    Batch Script does NOT Update Time


    Good evening.

    I have setup a Batch Script that I can run various Reports/Logs from. Now I have setup the Week Day-Date-Time output exactly how I want it and it works brilliantly. BUT, if I open the Batch Script and do NOT run a Report/Log for say 30 minutes for example, when I do run a Report/Log, it shows the EXACT time that the Batch Script was opened, regardless of how many times I run it

    I have tried using setlocal EnableDelayedExpansion [!], and numerous other methods to get around this, but to NO avail.

    Here is the block of Batch Script in its raw form WITHOUT using ANY setlocal EnableDelayedExpansion to make it easier as a starting point . . .

    Code:
    
    setlocal EnableDelayedExpansion
    
    for /f %%i in ('wmic OS Get LocalDateTime /Value ^& wmic Path Win32_LocalTime Get DayOfWeek /Value') do for /f %%j in ("%%i") do (set "%%j") & set /a "DOW=DayOfWeek+1"
    for /f "tokens=%DOW%" %%i in ("Sunday Monday Tuesday Wednesday Thursday Friday Saturday") do (set "Week_Day=%%i")
    for /f %%i in ('wmic OS Get LocalDateTime /format:list ^| find "="') do set "%%i"
    set "DD=%LocalDateTime:~6,2%" & set /a "MMM=1%LocalDateTime:~4,2% - 100" & set "YYYY=%LocalDateTime:~0,4%"
    for /f "tokens=%MMM%" %%i in ("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec") do set "Month=%%i"
    set "HH=%LocalDateTime:~8,2%" & set "MM=%LocalDateTime:~10,2%" & set "SS=%LocalDateTime:~12,2%"
    set "Program_Date=%DD%-%Month%-%YYYY%
    set "Created_Date=%Week_Day% the %Program_Date% at %HH%:%MM%:%SS%"
    

    The output is like this > Tuesday the 12-Oct-2021 at 20:24:45 for example.

    I do NOT want this to be in a DIFFERENT output format other that the above.
    I want to do this WITHOUT using PowerShell.

    This is NOT a big problem, but it would be nice to find an answer for it.

    Thanks in advance.
      My Computer


  2. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #2

    NOT to worry, I found a solution which I will post tomorrow.
      My Computer


  3. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #3

    OK, here is the solution.

    I obviously had to use setlocal EnableDelayedExpansion for some of the Variables.

    I created a Label with the Week Day - Date - Time information and the associated Variables.
    The required format being Wednesday the 13-Oct-2021 at 12:24:45 for example.
    %OFN% is the Variable that holds the Report/Log Filename.
    I then Call it at the appropriate time . . .

    Code:
    
    >> %OFN%      Call :Report_Header
    
    :Report_Header
    
    (setlocal EnableDelayedExpansion
     for /f %%i in ('wmic OS Get LocalDateTime /Value ^& wmic Path Win32_LocalTime Get DayOfWeek /Value') do for /f %%j in ("%%i") do (set "%%j") & set /a "DOW=DayOfWeek+1"
     for /f "tokens=%DOW%" %%i in ("Sunday Monday Tuesday Wednesday Thursday Friday Saturday") do (set "Week_Day=%%i")
     for /f %%i in ('wmic OS Get LocalDateTime /format:list ^| find "="') do set "%%i"
     set "DD=!LocalDateTime:~6,2!" & set /a "MMM=1!LocalDateTime:~4,2! - 100" & set "YYYY=!LocalDateTime:~0,4!"
     for /f "tokens=%MMM%" %%i in ("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec") do set "Month=%%i"
     set "HH=!LocalDateTime:~8,2!" & set "MM=!LocalDateTime:~10,2!" & set "SS=!LocalDateTime:~12,2!"
     set "Program_Date=!DD!-!Month!-!YYYY!
     set "Created_Date=!Week_Day! the !Program_Date! at !HH!:!MM!:!SS!"
     echo.
     echo Title   : %Title%
     echo Author  : %Author%
     echo Filename: %~nx0
     echo Created : !Created!
     echo User    : %UserName%
     echo.
    ) & exit /b
    

    Works brilliantly.

    Hopefully, this solution will help others in the future who are looking for a method to achieve this.

    SOLVED.
    Last edited by Paul Black; 14 Oct 2021 at 08:07.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 21:37.
Find Us




Windows 10 Forums