How to search keyword based on text files modified date?
Clash Royale CLAN TAG#URR8PPP
How to search keyword based on text files modified date?
I have multiple text files that are generated everyday.
I would like to search "apple" string from the latest modified text file.
If the string is not found, i will need to search from yesterday's text file.
If it still not found, i will need to search from day before yesterday's text file. The process keep going until i find the string.
Take an example:
Text file created today named : 08112018.txt
Text file created yesterday named : 08102018.txt
Text file created day before yesterday named : 08192018.txt
I will start my search for "apple" in 08112018.txt first.
If it's not found, i will search "apple" in 08102018.txt.
The process continues until "apple" is found.
Scripting.FileSystemObject
Take a look at this answer.
– omegastripes
Aug 12 at 11:38
If you find that an answer resolved your problem please consider accepting it. Posting your working code as a self-answer is not necessary and you should only do it when you come up with a solution yourself.
– Ansgar Wiechers
Aug 13 at 7:35
NOTE: Look folks, normally I wouldn't use a comment for this but come on! Take a look at what you have here. A new user has joined the site, asked a reasonable question and got a useful answer. Rather than the community trying to be inclusive and helpful by guiding their contribution, you're all taking the lazy option and downvoting their first question. Try harder! Offer help. Suggest / submit edits to the question. Don't penalise people on their first visit by flogging their reputation, voting to close the question and leaving. This community needs to try harder.
– Phil.Wheeler
Aug 13 at 9:14
1 Answer
1
Here's what I think will give you the best result:
Start by listing all your files into a disconnected record set:
Set fso = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
Set list = CreateObject("ADOR.Recordset")
list.Fields.Append "name", 200, 255
list.Fields.Append "date", 7
list.Open
For Each f In fso.GetFolder("C:your-folder-location").Files
list.AddNew
list("name").Value = f.Path
list("date").Value = f.DateLastModified
list.Update
Next
list.MoveFirst
You can then sort those by date last modified:
list.Sort = "date DESC"
Now you can start from the top of the list and work your way through.
Dim foundApple
list.MoveFirst
Do Until list.EOF Or foundApple
Do Until objTextFile.AtEndOfStream
Set objTextFile = fso.OpenTextFile(list("name"), ForReading)
strLine = objTextFile.ReadLine()
If InStr(strLine, "apple") <> 0 then foundApple = True
Loop
' If foundApple = True Then (Do whatever stuff you need)
list.MoveNext
Loop
list.Close
Works great! Thanks! I just need to put "Set objTextFile = fso.OpenTextFile(list("name"), ForReading)" before " Do Until list.EOF Or foundApple". I am working on how to echo which text file i found "Apple". Any idea?
– devilgemini
Aug 12 at 3:02
You could use
MsgBox list("name")
if you wanted a quick and dirty way to find the file immediately, otherwise if you wanted the found file logged somewhere you could create and write to a separate file? Depends what your needs are.– Phil.Wheeler
Aug 12 at 3:21
MsgBox list("name")
Awesome!!!! Thanks Phil! You da man.
– devilgemini
Aug 12 at 3:27
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Use
Scripting.FileSystemObject
. Check MCVE.– omegastripes
Aug 12 at 2:06