Hi,
I am writing a program in VB6 where I'm adding an "Import/Export" option to the file menu. Starting with export, I basically need to find all files matching a particular filename and zip and export them to a folder. Currently I am simply finding all the files (with different extensions) and showing them in a list box. It works fine the first time I run it, but second time the list box is empty. Can anyone help? Code is posted below.
Public Sub FILE_Export()
'Entry point
Dim foundFile As String
Do
foundFile = FileFindFirst(ExamplesDIR, EditExecuteFileName & ".*")
If Len(foundFile) Then
foundFile = GetFileName(foundFile)
frmExport.lstFiles.AddItem (foundFile)
Else
Exit Do
End If
Loop
frmExport.Show vbModal
End Sub
'Purpose : Performs a recursive search starting from the specified directory
' to find the next matching file (uses the file scripting object)
'Inputs : sInitialDirectory The directory to begin the seach from
' sFilePattern The file pattern to seach for eg. "*.xls"
'Outputs : Returns the full path and name of the next matching file
'Notes : Can be called recursively to find all instances of the specified file pattern
Function FileFindFirst(ByVal sInitialDirectory As String, ByVal sFilePattern As String) As String
Static fso As Scripting.FileSystemObject, oDirectory As Scripting.Folder, oThisDir As Scripting.Folder
Static ssLastPattern As String, ssLastFiles As String
Dim sThisPath As String, sResString As String, sTestFile As String
If (fso Is Nothing) = True Then
Set fso = New Scripting.FileSystemObject
End If
If Right$(sInitialDirectory, 1) <> "\" Then
sInitialDirectory = sInitialDirectory & "\"
End If
'Seach current directory
sThisPath = sInitialDirectory
sTestFile = dir$(sThisPath & sFilePattern)
Do
If FileExists(sThisPath & sTestFile) Then
If InStr(1, ssLastFiles, "|" & sThisPath & sTestFile) = 0 Then
'Found next matching file
sResString = sThisPath & sTestFile
Exit Do
End If
Else
'No more matching files in this directory
Exit Do
End If
'Get next matching file
sTestFile = dir$
Loop
If Len(sResString) = 0 Then
'File not found in sInitialDirectory, search sub directories...
Set oDirectory = fso.GetFolder(sInitialDirectory)
For Each oThisDir In oDirectory.SubFolders
sThisPath = oThisDir.path
If Right$(sThisPath, 1) <> "\" Then
sThisPath = sThisPath & "\"
End If
sTestFile = dir$(sThisPath & sFilePattern)
Do
If FileExists(sTestFile) Then
If InStr(1, ssLastFiles, "|" & sThisPath & sTestFile) = 0 Then
'Found next matching file
sResString = sInitialDirectory & sTestFile
End If
Else
'No more matching files in this directory, check its subfolders
sTestFile = FileFindFirst(sThisPath, sFilePattern)
If FileExists(sTestFile) Then
If InStr(1, ssLastFiles, "|" & sThisPath & sTestFile) = 0 Then
'Found next matching file
sResString = sTestFile
Exit Do
End If
Else
'File not found in sub folder
Exit Do
End If
End If
sTestFile = dir$
Loop
If Len(sResString) Then
'Found next matching file
Exit For
End If
Next
End If
If Len(sResString) Then
'Store search parameters
If sFilePattern = ssLastPattern Then
'Routine has been called with same parameters, store all previously matching files
ssLastFiles = ssLastFiles & "|" & sResString
Else
'Store matching file
ssLastFiles = "|" & sResString
End If
ssLastPattern = sFilePattern
'Return result
FileFindFirst = sResString
End If
End Function