[SOLVED] Extract rows with today's date.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

[SOLVED] Extract rows with today's date.

#1 Post by PAB » 18 May 2020 06:03

Good afternoon,

I have a long text file that I want to extract ALL the lines with Today's Date. I have tried several ways. This is one of the attempts . . .

Code: Select all

@echo off

>"%UserProfile%\Desktop\Report_DISM.txt" FindSTR "%date%" "%UserProfile%\Desktop\dism.log"

Pause
I have tried %Date%, %Today%, etc.
The files text output at the beginning of each line is in the date format YYYY-MM-DD which could well be different to the local date format.
If I put "2020-05-18" after the FindSTR it works and extracts ALL the rows with Todays Date, but I would like it to automatically pick up Today's Date in the format "YYYY-MM-DD".

Thanks in advance.

UPDATE:

Tried these but they didn't work . . .

Code: Select all

>"%UserProfile%\Desktop\Report_DISM.txt" FindSTR "(date.exe +"%%Y-%%m-%%d")" "%UserProfile%\Desktop\dism.log"

Code: Select all

>"%UserProfile%\Desktop\Report_DISM.txt" FindSTR "(date.exe +"%%YYYY-%%MM-%%DD")" "%UserProfile%\Desktop\dism.log"
Last edited by PAB on 22 May 2020 09:23, edited 1 time in total.

aGerman
Expert
Posts: 3869
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Extract rows with today's date.

#2 Post by aGerman » 18 May 2020 07:35

… FindSTR "%date%" …
If you run ...

Code: Select all

echo %date%
... and the format of the date doesn't match the format in your file then it can't be found. The format of the date variable is depending on local settings and not reliable.
We already have lengthy threads about how to get date and time values in a unique format. To cut a long story short - use WMIC, process its output using string manipulation, and assemble it new (in the order you need and with the separators you need).

Code: Select all

@echo off &setlocal

for /f %%i in ('WMIC OS Get LocalDateTime /value') do for /f %%j in ("%%i") do set "%%j"

echo %LocalDateTime%

set "y=%LocalDateTime:~0,4%"
set "m=%LocalDateTime:~4,2%"
set "d=%LocalDateTime:~6,2%"
echo %y%-%m%-%d%

pause
Steffen

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

Re: Extract rows with today's date.

#3 Post by PAB » 18 May 2020 07:58

Thanks for the reply and explanation Steffen, it is appreciated!

The local settings are in the date format DD-MM-YYYY, and the file output [ dism.log ] is in the date format YYYY-MM-DD.
As EVERY line of the file output [ dism.log ] starts with the date in YYYY-MM-DD format, is there any way to extract the FIRST 10 characters of the LAST line and then use that in the FindSTR?

That should work!

As an example, here are the last few lines of the file . . .

Code: Select all

2020-05-18 12:02:14, Info                  DISM   DISM Manager: PID=8232 TID=11920 Closing session event handle 0x1f0 - CDISMManager::CleanupImageSessionEntry
2020-05-18 12:02:14, Info                  DISM   DISM.EXE: Image session has been closed. Reboot required=no.
2020-05-18 12:02:14, Info                  DISM   DISM.EXE: 
2020-05-18 12:02:14, Info                  DISM   DISM.EXE: <----- Ending Dism.exe session ----->
2020-05-18 12:02:14, Info                  DISM   DISM.EXE: 
2020-05-18 12:02:14, Info                  DISM   DISM Provider Store: PID=8232 TID=11920 Found the OSServices.  Waiting to finalize it until all other providers are unloaded. - CDISMProviderStore::Final_OnDisconnect
2020-05-18 12:02:14, Info                  DISM   DISM Provider Store: PID=8232 TID=11920 Disconnecting Provider: FolderManager - CDISMProviderStore::Internal_DisconnectProvider
2020-05-18 12:02:14, Info                  DISM   DISM Provider Store: PID=8232 TID=11920 Releasing the local reference to DISMLogger.  Stop logging. - CDISMProviderStore::Internal_DisconnectProvider
I can't use words like DISM or Info etc because they are present in previous dates.

Thanks in advance.

aGerman
Expert
Posts: 3869
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Extract rows with today's date.

#4 Post by aGerman » 18 May 2020 08:48

Did you run my example code and did it display the date in the format you need it? If so, just use it.

Steffen

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

Re: Extract rows with today's date.

#5 Post by PAB » 18 May 2020 08:59

Yes, the second date was in the CORRECT format => echo %y%-%m%-%d%.
I did try to use it but I couldn't get it to output any data in the newly created file!

Code: Select all

>"%UserProfile%\Desktop\Report_DISM.txt" FindSTR "%%y%%-%%m%%-%%d%%" "%UserProfile%\Desktop\dism.log"

Code: Select all

>"%UserProfile%\Desktop\Report_DISM.txt" FindSTR ("%%y%%-%%m%%-%%d%%") "%UserProfile%\Desktop\dism.log"
Thanks in advance.

UPDATE:

I have cracked it . . .

Code: Select all

>"%UserProfile%\Desktop\Report_DISM.txt" FindSTR "%y%-%m%-%d%" "%UserProfile%\Desktop\dism.log"
Thank you so much Steffen, it is really appreciated!

Post Reply