Searching for dates within a text file

Posted on 2005-04-20
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

    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. 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

    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
    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.  
    LVL 35

    Expert Comment

    by:[ fanpages ]
    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

    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 ]
    Sorry we didn't get to discuss further to offer alternative methods.



    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
    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…
    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…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now