How to find the newest file containing a txt string

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

How to find the newest file containing a txt string

#1 Post by MKANET » 17 Apr 2012 18:31

I'm trying to figure out the simplest way to find the most recently created file which contains the below text string (in a local directory). Hopefully someone could be nice enough to give me a working example I can patch the text string right in. I ultimately need to end up with a variable that equals the last created file containing that string:

<created_time_local>2012-04-17

Hopefully, I can modify the proposed batch file so it can also look for the text string below as well in an identical scenario.
End: 3/29/2012

In all these examples, they all follow with a respective timestamp; which can help decide which is the most recent file. For example:

<created_time_local>2012-04-14 03:30:01
and
End: 3/29/2012 12:42:12

I would prefer to determine which file is the newest based on the timestamp within the file instead of the file time/date stamp. All timestamps are between 12:01am-11:00am (however the "AM" part is not displayed).

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to find the newest file containing a txt string

#2 Post by dbenham » 17 Apr 2012 20:46

There is a massive difference between:
<created_time_local>2012-04-17 03:30:01
and
End: 3/29/2012 12:42:12

The 1st example sorts in chronological order with a simple string sort. The second does not.

The solution for the first example is fairly simple as long as the lines are always formatted as you have shown.

Code: Select all

@echo off
setlocal
set tempFile="%temp%\timeStamps%random%.txt"
(
  for /f "tokens=1* eol=: delims=:" %%A in ('findstr /c:"<created_time_local>" *.txt') do echo %%B?%%A
) >%tempFile%
set tempFile
type %tempFile%
for /f "tokens=2 delims=?" %%F in ('sort /r %tempFile%') do (
  set recentFile="%%F"
  goto :done
)
:done
del %tempFile%
set recentFile

The solution for the second example is much more complicated because the lines with the timestamp will have to be parsed and converted into a string that can be sorted chronologically. You state that all times should be between 12am and 11am, and the 2nd example has a time of 12:42:12. To get the proper chronological sort the time will have to be converted to military (24 hour) time. Also the date will need to be parsed into year/month/day format. Parsing and formatting isn't too bad if you know the format of the source data, but it is difficult if you do not know the format ahead of time.

Dave Benham
Last edited by dbenham on 17 Apr 2012 20:51, edited 1 time in total.

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: How to find the newest file containing a txt string

#3 Post by MKANET » 17 Apr 2012 20:51

Thanks for the quick reply. Sorry if I wasn't clear, but the second example is also 12:42:12 AM, not PM.

Edit: Also all the strings I'll be searching for will fall under the first example or second example in respect to date format.

Thanks again for your help.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to find the newest file containing a txt string

#4 Post by dbenham » 17 Apr 2012 22:32

Exactly - I had already deduced that. And it causes a problem because 12 needs to sort as earlier than 03. That is why I say you need to convert the time into 24 hour format so that the times sort properly.

Dave Benham

miskox
Posts: 666
Joined: 28 Jun 2010 03:46

Re: How to find the newest file containing a txt string

#5 Post by miskox » 18 Apr 2012 00:40

Yes. 12:00 AM should be written as 00:00.

Saso

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: How to find the newest file containing a txt string

#6 Post by MKANET » 18 Apr 2012 09:20

I just tested the samplecode above. Unfortunately, the text files are Unicode based, not asciii. So, the findstr command wont work. Im trying to figure out how to use the find command instead. However, it doesn't conveniently place the filename on the same line as the text string its looking for in the search results. Dave, or anyone, can you please help do this using the find command or any other way that supports Unicode? Thanks a million!!

EDIT: Good news, I just found a consistent piece of string/format to go by in all the files.. They even have AM/PM. All files fall into two categories:

Files that end with *.txt (containing string format below)

Code: Select all

<end>3/20/2012 1:05:06 AM [


Files that end with *.xml (containing string format below)

Code: Select all

End:   3/29/2012 12:42:08 AM [


All strings end with a [ symbol.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to find the newest file containing a txt string

#7 Post by foxidrive » 18 Apr 2012 10:11

MKANET wrote:I just tested the samplecode above. Unfortunately, the text files are Unicode based, not asciii. So, the findstr command wont work. Im trying to figure out how to use the find command instead.


Does find.exe really support unicode?

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: How to find the newest file containing a txt string

#8 Post by MKANET » 18 Apr 2012 10:45

Yes, find supports unicode.

Based on Dave's code, I modified it to support Unicode. Works perfectly. Everything gets sorted correctly. However, I haven't tested it with 12:XX AM yet.

Code: Select all

@echo off
cls
setlocal

(for /f "tokens=1 eol=[ delims=[" %%A in ('find "<end>3/20/2012" BK*.xml') do echo %%A) > temp.lis

for /f "tokens=1,2" %%A in (temp.lis) DO (
if "%%A"=="----------" set recentFile=%%B
)

del temp.lis
echo %recentFile%
:end


Edit: In the example above I'm looking for all BK*.xml files with the date of 3/20/2012; and, present me with the latest file containing that date.

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: How to find the newest file containing a txt string

#9 Post by MKANET » 18 Apr 2012 10:58

Damn it, :( Everything works in the code I posted, except for 12:XX AM. Just as you guys predicted. The find utility presumes PM, not AM.

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: How to find the newest file containing a txt string

#10 Post by MKANET » 18 Apr 2012 11:09

Any help to enhance the code to support military time would be greatly appreciated.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to find the newest file containing a txt string

#11 Post by foxidrive » 18 Apr 2012 11:20

This seems to work here with the xml sample format.


Code: Select all

@echo off

setlocal enabledelayedexpansion

del "outfile.txt" 2>nul

for %%z in (*.xml) do (
for /f "tokens=2,3,4,5,6,7 delims=>/: " %%a in ('find "<end>3/20/2012" ^< "%%z"') do (
set year=%%c
set mon=%%a
if !mon! lss 11 set mon=0!mon!
set day=%%b
if !day! lss 11 set day=0!day!
set hr=%%d
if !hr! lss 11 set hr=0!hr!
if !hr! equ 12 set hr=00
set min=%%e
set sec=%%f

>>"outfile.txt" echo !year!-!mon!-!day!-!hr!-!min!-!sec! "%%z"
)
)

for /f "tokens=1*" %%a in ('sort /r ^<"outfile.txt"') do set recentfile=%%b



del "outfile.txt"
echo %recentFile%
pause

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: How to find the newest file containing a txt string

#12 Post by MKANET » 18 Apr 2012 12:01

Thank you so much. Actually, for me, it still reports the same file. It should treat 12:40:28am as earlier than all the other files. However, it still thinks it has the highest value. "test.cmd" outputs the result of BK120320-002.XML.

Image

This is the actual text in the file:
<end>3/20/2012 12:40:28 AM

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to find the newest file containing a txt string

#13 Post by foxidrive » 18 Apr 2012 12:07

post the contents of outfile.txt please.

BK120320-002.XML *is* the earliest file, right?

Oh, sorry. You want the most recent file on that day - IE the latest. Remove the /R from the sort command.
Last edited by foxidrive on 18 Apr 2012 12:12, edited 1 time in total.

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: How to find the newest file containing a txt string

#14 Post by MKANET » 18 Apr 2012 12:11

Look above. :) In the screenshot it types the contents of that file after displaying the recentFile. Is it because you have a reverse sort command? Its supposed to be the earliest file. I need the "latest"

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to find the newest file containing a txt string

#15 Post by foxidrive » 18 Apr 2012 12:12

Yes. I edited above when you posted.


There looks to be a problem in that each file has TWO sections which match. At least that would explain why the filenames are duplicated. The times do not match in the two sections too.
Last edited by foxidrive on 18 Apr 2012 12:18, edited 1 time in total.

Post Reply