?
Solved

Find the latest saved file

Posted on 2003-11-19
2
Medium Priority
?
280 Views
Last Modified: 2010-05-01
I would like to find the latest saved file in directory c:\test\ and compare if the same file exist in directory c:\test\old\.
0
Comment
Question by:mikael_lu
2 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 9780334
Create a new project and add two textboxes and a commandbutton.

Regards,

Idle_Mind

' ************************* Code for Form1 *************************
Option Explicit

Private Sub Form_Load()
    Text1.Text = "c:\test\"
    Text2.Text = "c:\test\old\"
    Command1.Caption = "Check Files"
End Sub

Private Sub Command1_Click()
    Dim newestFile As String
    Dim newestDate As Date
    Dim curDate As Date
    Dim files As Collection
    Dim file As Variant
   
    Set files = getFilesInDirectory(Text1.Text, "*")
    If files.Count > 0 Then
        newestFile = files.Item(1)
        newestDate = FileDateTime(newestFile)
        For Each file In files
            curDate = FileDateTime(file)
            If curDate > newestDate Then
                newestDate = curDate
                newestFile = file
            End If
        Next file
        If Dir(Text2.Text, vbDirectory) <> "" Then
            If Right(Text2.Text, 1) <> "\" Then
                Text2.Text = Text2.Text & "\"
            End If
            If Dir(Text2.Text & newestFile) <> "" Then
                MsgBox "File: """ & newestFile & """ Exists in """ & Text2.Text & """"
            Else
                MsgBox "File: """ & newestFile & """ Does Not Exist in """ & Text2.Text & """"
            End If
        Else
            MsgBox "Target Directory does not exist"
        End If
    Else
        MsgBox "Directory is Empty"
    End If
End Sub

Private Function getFilesInDirectory(targetDirectory As String, filePattern As String) As Collection
    Dim fileCollection As Collection
    Dim curFile As String
   
    On Error GoTo noSuchDirectory
   
    Set fileCollection = New Collection
    ChDir targetDirectory
   
    On Error GoTo 0
    curFile = Dir(filePattern)
    Do Until curFile = ""
        fileCollection.Add curFile, curFile
        curFile = Dir()
    Loop
    Set getFilesInDirectory = fileCollection
    Exit Function
   
noSuchDirectory:
    MsgBox "Invalid Directory: " & targetDirectory
    Set getFilesInDirectory = fileCollection
End Function
0
 

Author Comment

by:mikael_lu
ID: 9785083
Hi Idle_Mind!

Thanks for a very fine solution of my problem, I'm very happy.

Regards
Mikael_lu
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

850 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