Condense Day-Date-Time Code for Reports.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Condense Day-Date-Time Code for Reports.

#1 Post by PAB » 14 Feb 2022 15:49

Good evening,

I hope you are ALL well and staying safe.

I have many Scripts that produce Reports, some single Scripts, and one that is menu driven and contains about 80+ Scripts. They ALL work great!

On ALL of them, I like to have the output include WeekDay DD-MMM-YYY HH:MM:SS. So the typical output would look like Created : Monday the 14-Feb-2022 at 21:01:32 for example.

With the menu driven Script, I usually open it in the morning and leave it open throughout the day, so in order to get it to show the correct output, regardless of when one of the Scripts within it is run during the day, I use the second Script.

::============================================================================================================================================

For a single Script that just gets run, I use the following . . .

Code: Select all

:: *** At the TOP of the 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")
set "DD=!LocalDateTime:~6,2!" & set /a "MMM=1!LocalDateTime:~4,2! - 100" & set "YYYY=!LocalDateTime:~0,4!"
set "HH=!LocalDateTime:~8,2!" & set "MM=!LocalDateTime:~10,2!" & set "SS=!LocalDateTime:~12,2!"
for /f "tokens=%MMM%" %%i in ("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec") do set "Month=%%i"
set "Program_Date=!DD!-!Month!-!YYYY!
set "Created=!Week_Day! the !Program_Date! at !HH!:!MM!:!SS!"

:: *** Within the code . . . ***

echo Created : !Created!
For the menu driven Script [ includes about 80+ Scripts ], I use the following, so the CORRECT time of running is always shown . . .

Code: Select all

:: *** At the TOP of the 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")
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 "Program_Date=!DD!-!Month!-!YYYY!

:: *** Within the code . . . ***

Call :Report_Header

:: *** At the BOTTOM of the code . . . ***

: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")
 set "DD=!LocalDateTime:~6,2!" & set /a "MMM=1!LocalDateTime:~4,2! - 100" & set "YYYY=!LocalDateTime:~0,4!"
 set "HH=!LocalDateTime:~8,2!" & set "MM=!LocalDateTime:~10,2!" & set "SS=!LocalDateTime:~12,2!"
 set "Created=!Week_Day! the !Program_Date! at !HH!:!MM!:!SS!"
 echo.
 echo ======================================================================================================================================================================
 echo Created : !Created!
) & Exit /b
I am sure that the above TWO codes can be reduced and still produce the required output.

For the second Script, there are duplicate lines/entries at the top and bottom, because this is the only way that I have found to get it to work, but I am sure that there must be an easier way?

The SECOND Script particularly is the main one that I would like help on please.

Any help will be greatly appreciated.

AR Coding
Posts: 53
Joined: 02 May 2021 21:16

Re: Condense Day-Date-Time Code for Reports.

#2 Post by AR Coding » 14 Feb 2022 19:57

If powershell is allowed... (untested)

Code: Select all

for /f "delims=" %%a In ('powershell Get-Date -format "{dddd \t\h\e dd-MMM-yyyy \a\t HH:mm:ss}"') do set "created=%%a"
Echo(Created on: %created%
Last edited by AR Coding on 14 Feb 2022 20:37, edited 1 time in total.

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Re: Condense Day-Date-Time Code for Reports.

#3 Post by PAB » 14 Feb 2022 20:25

Thanks AR Coding for the reply, it is appreciated.

Yes, I am open to using PowerShell within CMD [ like you have ], but not pure PowerShell.

Unfortunately, that does not work. I assume that was specifically for the first Script?

Thanks again.

AR Coding
Posts: 53
Joined: 02 May 2021 21:16

Re: Condense Day-Date-Time Code for Reports.

#4 Post by AR Coding » 14 Feb 2022 20:40

Small typo. Code fixed try it again

If that doesnt work, then you can try removing the curly brackets from the powershell command, and try again. I Am not sure if they are neccesary.

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Re: Condense Day-Date-Time Code for Reports.

#5 Post by PAB » 14 Feb 2022 21:03

Thanks again for the reply.

Brilliant, that works VERY nicely for the FIRST Script.

It also works for the SECOND Script, I just needed to use !Created! instead of %Created%.

That gets rid of so much code it is unbelievable.

I will test further tomorrow because it is 03:14 a.m. GMT here and I need to go to bed as I have a hospital appointment tomorrow.

Thanks again.

AR Coding
Posts: 53
Joined: 02 May 2021 21:16

Re: Condense Day-Date-Time Code for Reports.

#6 Post by AR Coding » 14 Feb 2022 21:15

Ok. Good night , go get some rest. :D

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Condense Day-Date-Time Code for Reports.

#7 Post by Squashman » 15 Feb 2022 00:02

PAB wrote:
14 Feb 2022 21:03
It also works for the SECOND Script, I just needed to use !Created! instead of %Created%.
You would not need to use delayed expansion for the variable and all the other variables if you just got rid of the UNNECESARY parentheses.

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Re: Condense Day-Date-Time Code for Reports.

#8 Post by PAB » 15 Feb 2022 03:44

Thanks for taking the time to reply Squashman.
Squashman wrote:
15 Feb 2022 00:02
PAB wrote:
14 Feb 2022 21:03
It also works for the SECOND Script, I just needed to use !Created! instead of %Created%.
You would not need to use delayed expansion for the variable and all the other variables if you just got rid of the UNNECESARY parentheses.
I don't quite understand, could you please elaborate?

On the FIRST Script I am using . . .

Code: Select all

:: *** At the TOP of the code . . . ***

for /f "delims=" %%a in ('PowerShell Get-Date -Format "{dddd \t\h\e dd-MMM-yyyy \a\t HH:mm:ss}"') do set "Created=%%a"

:: *** Within the code . . . ***

echo Created : %Created%
On the SECOND Script, I am using . . .

Code: Select all

:: *** Within the code . . . ***

Call :Report_Header

:: *** At the BOTTOM of the code . . . ***

:Report_Header

(setlocal EnableDelayedExpansion
 for /f "delims=" %%a In ('PowerShell Get-Date -Format "{dddd \t\h\e dd-MMM-yyyy \a\t HH:mm:ss}"') do set "Created=%%a"
 echo Created : !Created!
) & Exit /b
One further question please. Why does it have to be MMM to work, when mmm [ puts 2 digit number ] doesn't?

AR Coding
Posts: 53
Joined: 02 May 2021 21:16

Re: Condense Day-Date-Time Code for Reports.

#9 Post by AR Coding » 15 Feb 2022 07:07

MMM Is going to be the abreviated month (ex. Feb)

MM is going to be the two-digit numeric month (ex. 02)


Have a look at this chart https://ss64.com/ps/syntax-dateformat.html
Last edited by AR Coding on 15 Feb 2022 07:24, edited 1 time in total.

AR Coding
Posts: 53
Joined: 02 May 2021 21:16

Re: Condense Day-Date-Time Code for Reports.

#10 Post by AR Coding » 15 Feb 2022 07:16

I think this is what Squashman was reffering to

Code: Select all

for /f "delims=" %%a In ('powershell Get-Date -format "dddd \t\h\e dd-MMM-yyyy \a\t HH:mm:ss"') do set "created=%%a"
Echo(Created on: %created%

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Re: Condense Day-Date-Time Code for Reports.

#11 Post by PAB » 15 Feb 2022 07:38

Thanks again,
AR Coding wrote:
15 Feb 2022 07:07
MMM Is going to be the abreviated month (ex. Feb)

MM is going to be the two-digit numeric month (ex. 02)
I realise that, but when I used LOWER case, it gave me a two digit number, even though I used mmm. It's not a problem, I was just curious.
AR Coding wrote:
15 Feb 2022 07:16
I think this is what Squashman was reffering to

Code: Select all

for /f "delims=" %%a In ('powershell Get-Date -format "dddd \t\h\e dd-MMM-yyyy \a\t HH:mm:ss"') do set "created=%%a"
Echo(Created on: %created%
Thank you, I will try that later.
Any reason why there is an open bracket after Echo, as it works just as well without it?

EDIT:

I had to use setlocal EnableDelayedExpansion because if I ran report 'A', and then waited a couple of minutes and then run report 'B', the report 'B' time was the time that report 'A' was actually run. I tested it paying attention to the times so as to test.

So this works great . . .

Code: Select all

:Report_Header

(setlocal EnableDelayedExpansion
 for /f "delims=" %%a in ('PowerShell Get-Date -Format "{dddd \t\h\e dd-MMM-yyyy \a\t HH:mm:ss}"') do set "Created=%%a"
 echo.
 echo ======================================================================================================================================================================
 echo Created : !Created!
) & Exit /b

It also needed the brackets in order to work.

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Condense Day-Date-Time Code for Reports.

#12 Post by Aacini » 15 Feb 2022 09:17

Mmmm... I assume you want not to use the auxiliary variables (DayOfWeek, DOW, DD, MMM, etc) other than to get the "Created" final variable, but I am not sure about "Program_Date" variable in the second (menu-driven) script. Are you using Program_Date variable in the rest of the script? (I assume the answer is Yes) For that reason you don't get it again at the end? However, in Program_Date variable you are NOT using Week_Day variable, so why you get it in the top part?


This is the way I would do this in a pure Batch-file, that run much, much faster than the PowerShell equivalent:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Put this at the VERY TOP of the program, in the variables initialization section:
set "i=0" & for %%d in (Sunday Monday Tuesday Wednesday Thursday Friday Saturday) do set "DOW[!i!]=%%d" & set /A i+=1
set "i=100" & for %%m in (Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec) do set /A i+=1 & set "Month[!i:~1!]=%%m"



rem To get Program_Date:
for /F "tokens=2 delims==" %%a in ('wmic OS Get LocalDateTime /Value') do set "locDT=%%a"
set "Program_Date=%locDT:~6,2%-!Month[%locDT:~4,2%]!-%locDT:~0,4%"

rem To get Created, add this:
for /F "tokens=2 delims==" %%a in ('wmic Path Win32_LocalTime Get DayOfWeek /Value') do set "Day=%%a"
set "Created=!DOW[%Day%]! the %Program_Date% at %locDT:~8,2%:%locDT:~10,2%:%locDT:~12,2%"

echo Created: %Created%

If you don't use Program_Date variable then you may eliminate it, so to get the Created variable you just need 3 lines:

Code: Select all

for /F "tokens=2 delims==" %%a in ('wmic OS Get LocalDateTime /Value') do set "locDT=%%a"
for /F "tokens=2 delims==" %%a in ('wmic Path Win32_LocalTime Get DayOfWeek /Value') do set "Day=%%a"
set "Created=!DOW[%Day%]! the %locDT:~6,2%-!Month[%locDT:~4,2%]!-%locDT:~0,4% at %locDT:~8,2%:%locDT:~10,2%:%locDT:~12,2%"
Antonio

AR Coding
Posts: 53
Joined: 02 May 2021 21:16

Re: Condense Day-Date-Time Code for Reports.

#13 Post by AR Coding » 15 Feb 2022 20:22

@Aacini: beautiful code! but
Aacini wrote:
15 Feb 2022 09:17
This is the way I would do this in a pure Batch-file, that run much, much faster than the PowerShell equivalent:
was not the OP's original request...
PAB wrote: I am sure that the above TWO codes can be reduced and still produce the required output
Though you definitely win with speed!

EDIT: Suprisingly you actually DONT!
I've ran thru some tests and this is what i got:
My code:

Code: Select all

Elapsed Time: 00:00:00,47
Aacinis code:

Code: Select all

Elapsed Time: 00:00:00,64
running Windows 10

Post Reply