Option Explicit
Private Sub Command1_Click()
Dim strDir As String
Dim files As New Collection
Dim strExt As String
Dim strPath As String
Dim file As FileInfo
' Set the path to be enumerated for files
strPath = "C:\SomeFolder\"
' Get the first file in the folder
strDir = Dir(strPath & "*.*", vbNormal)
' Loop this code until there are no more files
Do While Len(strDir) > 0
' Get the files extension
strExt = JustExt(strDir)
' If it's an XXX, YYY or ZZZ extension or the extension
' is a valid date in the format YYMMDD, then process it
If InStr(";xxx;yyy;zzz;", ";" & LCase(strExt) & ";") Or _
IsDate(Format(strExt, "@@-@@-@@")) Then
' Add the file to our collection of files
Call AddFile(files, strPath & strDir)
End If
' Get the next file in the folder
strDir = Dir
Loop
' List the files that have an XXX extension and a file
' association with a YYMMDD format extension
List1.Clear
For Each file In files
If file.HasXXX And file.HasYMD Then
Call List1.AddItem(file.FullPath & file.Name & ".xxx")
End If
Next
End Sub
Private Sub AddFile(ByRef files As Collection, ByVal strFile As String)
Dim file As New FileInfo
Dim strExt As String
Dim flgExists As Boolean
' Trap an error caused if the file isn't
' already in the collection
On Local Error GoTo FileNotFound
' Attempt to get the file from the collection
Set file = files(JustStem(strFile))
' If we get this far, the file exists
flgExists = True
FileNotFound:
' Reset the error handler
On Local Error GoTo 0
' Get the File's extension
strExt = JustExt(strFile)
' Assign the Name of the file (without extension)
file.Name = JustStem(strFile)
' Assign the Path of the file
file.FullPath = JustPath(strFile)
' If the file has an XXX extension, set the XXX flag
If LCase(strExt) = "xxx" Then
file.HasXXX = True
' If the file has an YYY extension, set the YYY flag
ElseIf LCase(strExt) = "yyy" Then
file.HasYYY = True
' If the file has an ZZZ extension, set the ZZZ flag
ElseIf LCase(strExt) = "zzz" Then
file.HasZZZ = True
' If the file has an YYMMDD extension, set the YMD flag
ElseIf IsDate(Format(strExt, "@@-@@-@@")) Then
file.HasYMD = True
End If
' If the file is in the collection, remove it
If flgExists Then
Call files.Remove(file.Name)
End If
' Add the file to the collection
files.Add file, file.Name
End Sub
' Returns just the Path of the supplied filename
Private Function JustPath(ByVal fileName As String) As String
If InStr(fileName, "\") > 0 Then
fileName = Left(fileName, InStrRev(fileName, "\"))
Else
fileName = ""
End If
JustPath = fileName
End Function
' Returns just the File of the supplied file/path name
Private Function JustFile(ByVal fileName As String) As String
If InStr(fileName, "\") > 0 Then
fileName = Mid(fileName, InStrRev(fileName, "\") + 1)
End If
JustFile = fileName
End Function
' Returns just the stem portion of the filename
' (the part without the extension)
Private Function JustStem(ByVal fileName As String) As String
fileName = JustFile(fileName)
If InStr(fileName, ".") > 0 Then
fileName = Left(fileName, InStrRev(fileName, ".") - 1)
End If
JustStem = fileName
End Function
' Returns just the file's extension
Private Function JustExt(ByVal fileName As String) As String
Dim strExt As String
If InStr(fileName, ".") > 0 Then
strExt = Mid(fileName, InStrRev(fileName, ".") + 1)
End If
JustExt = strExt
End Function