Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleList non-directory files modified between two dates
Keywordsfile, modified, date, between, FileDateTime
CategoriesFiles and Directories
 
Use Dir$ to list the files. Use GetAttr to see which are not directories. Use FileDateTime to see when they were created or last modified.
 
Private Sub cmdFind_Click()
Dim ch As String
Dim dir_name As String
Dim path_name As String
Dim pos As Integer
Dim before_date As Date
Dim after_date As Date
Dim file_date As Date
Dim next_file As String
Dim files As String

    On Error GoTo Oops
    lstResults.Clear

    before_date = CDate(txtModifiedBefore.Text)
    after_date = CDate(txtModifiedAfter.Text)

    ' Add 1 day because the file modification
    ' date includes a time.
    before_date = DateAdd("d", 1, before_date)
    after_date = DateAdd("d", 1, after_date)

    ' Get the directory search pattern.
    dir_name = txtDirectory.Text
    ch = Right$(dir_name, 1)
    If ch = "\" Then
        dir_name = dir_name & "*.*"
        txtDirectory.Text = dir_name
    ElseIf ch <> "*" Then
        dir_name = dir_name & "\*.*"
        txtDirectory.Text = dir_name
    End If

    ' Get the directory path.
    pos = InStrRev(dir_name, "\")
    path_name = Left$(dir_name, pos)

    next_file = Dir$(dir_name)
    Do While Len(next_file) > 0
        ' Get the full file name.
        next_file = path_name & next_file

        ' See if it is not a directory.
        If Not (GetAttr(next_file) And vbDirectory) Then
            ' See if it was modified before
            ' the date.
            file_date = FileDateTime(next_file)
            If (file_date >= after_date) And _
               (file_date <= before_date) _
            Then
                ' List this file.
                lstResults.AddItem next_file & _
                    " (" & Format$(file_date) & ")"
            End If
        End If

        next_file = Dir$()
    Loop
    Exit Sub

Oops:
    MsgBox "Error " & Format$(Err.Number) & _
        " making list." & vbCrLf & _
        Err.Description
    Exit Sub
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated