0

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.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
devilgemini
  • 33
  • 1
  • 6
  • Use `Scripting.FileSystemObject`. Check [MCVE](https://stackoverflow.com/help/mcve). – omegastripes Aug 12 '18 at 02:06
  • Take a look at [this answer](https://superuser.com/q/60173/367122). – omegastripes Aug 12 '18 at 11:38
  • 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 '18 at 09:14

1 Answers1

2

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
  • I've just brain-dumped this code. It's not tested. YMMV.
Phil.Wheeler
  • 16,748
  • 10
  • 99
  • 155
  • 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 '18 at 03: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 '18 at 03:21