Searching for dates within a text file

Posted on 2005-04-20
Medium Priority
Last Modified: 2010-05-02
I am working on a program that will scroll through text files within a directory tree searching for a date.  The main problem that I am having is that this date can appear on different lines in the file and it is not always the first thing on the line.  I need to read in the value and set a variable with it for use later in the program.

Any help will be appreciated.  Thanks,
Question by:Whah
LVL 23

Expert Comment

ID: 13826894
You would have to use the FSO to read the text file(s) into each of there own array(s) within a for next loop and then from there have an if statement to check each array against the date you are looking for.

www.juicystudio.com is a good starting place to research and use the FSO, you will need a reference to the Microsoft Scripting Runtime :) You can do this by going to Project --> References and checking the one called Microsoft Scripting Runtime ;)

I can post back an example later on if you would like for one text file, however searching through more then one text file is probably going to be more complex because you would also then need to store each of the text file names to be searched through before you can even begin the search :)
LVL 12

Accepted Solution

Bob Lamberson earned 1500 total points
ID: 13831008
you can call the countRecs function from a button on the form.

Option Compare Database
Option Explicit

Function countRecs()
Dim fso, filespec, f, fc, f1, s, ts
Dim strFolders, folderspec, folderCount, dtCount, fileContents, fileLine

Set fso = CreateObject("Scripting.FileSystemObject")
folderspec = "C:\myData\" & strFolders
Set f = fso.GetFolder(folderspec)               'sets the folder to look through
    Set fc = f.Files                                       '
    For Each f1 In fc                           'checks each file in the folder
        folderCount = folderCount + 1
        Set ts = fso.OpenTextFile(folderspec & f1.Name)         'opens the file
        fileLine = ts.ReadLine                                              'loops through each line of the file and the instr() below looks for your date in that line.
        If InStr(1, fileLine, "YourDate") > 0 Then       <<<<<<<<<<<<insert your date or date variable here
            dtCount = dtCount + 1           ' this counts the occurances of the date, you can do anything you want here with the date.
        End If
        If ts.AtEndOfStream Then Exit Do
form1.txtA = folderCount            ' these lines just display the folder and date count in textboxes on the form.
form1.txtB = dtCount
End Function

Hope this helps.

LVL 17
ID: 13831685
If you're looking for any date, not just a specifis one then IsDate ("poss_date_string") will return true if the "poss_date_string" can be converted to a date.
At least if you get False then you know that it can't be. You will need further sanity checking to ensure that is is a sensible date, say within a time period that you are expecting.  
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 35

Expert Comment

by:[ fanpages ]
ID: 13837906
Hi Whah,

What format will the date be in?

e.g. dd/mm/yyyy
or ddd-mmm-yy
or m/d/yyyy
or possibly any valid data format?

Also, is there a limit to the records of the file in which the date will reside?  i.e. if it's not in the first 5 rows of the file, then it isn't there.  Or is there *always* a date in the file regardless of it's position?

Thanks for your clarification.



Author Comment

ID: 13922515
Thanks everyone for the help on this question.  We are going to approch it in a little different manner, but thank you for your help anyway.
LVL 35

Expert Comment

by:[ fanpages ]
ID: 13924409
Sorry we didn't get to discuss further to offer alternative methods.



Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month17 days, 4 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question