Help with file search

Russ9517

Registered User.
Local time
Today, 16:32
Joined
Sep 15, 2006
Messages
30
I'm writing some code to read a list of filenames from a text file and searching for the filename in a folder and sub folders. The following code works but it only search 1 folder how can i get it to search any subfolders.
thanks

Private Sub cmdCheck_Click()
Dim strFilename As String
Dim strCheckFile As String
Dim strResult As String
Dim strCheckPath As String
strCheckPath = txtPathToCheck
Open txtFilename For Input As #1
Do While Not EOF(1)
Input #1, strCheckFile
strResult = ""
' Check all folders
If strResult = "" Then
strResult = Dir(strCheckPath & strCheckFile)
End If

If strResult <> "" Then
FileCopy strCheckPath & strCheckFile, txtNewLocation & strCheckFile
Else
Open txtNewLocation & "Missing Files.txt" For Append As #2
Print #2, strCheckFile & " is missing"
Close #2
End If
Loop
Close #1
MsgBox "Done"
End Sub
 
hey Russ,

did you know that your code will allow you to view up to 250 file names? What happens if you dir/subdir contains over that number of files?

I discovered the solution to this problem only a few days ago, you have to write the file names into a temp table. The you can do anything with them, sort them in any way you like and view them either in query view or report view. Any way this is the code I use:

**************************************************
Thanks go to the author of the code whose name, unfortunately,
I cannot remember
**************************************************

Public Function AppendFileList(strFolder As String, IncludeSubFolders As Boolean)
On Error GoTo errHandler

Dim objFSO As FileSystemObject
Dim objFolder As Folder
Dim objFiles As File
Dim strFileNames As String
Dim strSubFolder As Folder

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)


'CREATE TABLE OF FILES
'---------------------

'Drop table TblFiles first
DoCmd.SetWarnings False
'DoCmd.RunSQL "Delete * from TblFileList"
'DoCmd.DeleteObject acTable, "TblFileList"

'Create Table TblFileList
DoCmd.RunSQL "Create Table TblFileList (Filename Text(255), Folder Text(255), [FileSize (KB)] Number);"

'To scan root files
'------------------

For Each objFiles In objFolder.Files

' SysCmd acSysCmdSetStatus, objFolder.Path

DoCmd.SetWarnings False
strCount = strCount + 1
DoCmd.RunSQL ("Insert Into TblFileList Values('" & objFiles.Name & "','" & objFolder.path & "'," & Round(objFiles.Size / 1024, 2) & ");")
DoCmd.SetWarnings True
DoEvents
strFilesFound = strFilesFound & vbCrLf & objFiles.Name

Next

'To scan sub folders
'-------------------
If IncludeSubFolders = True Then

For Each strSubFolder In objFolder.SubFolders
Call AppendFileList(strSubFolder.path, True)
Next

End If

errHandler:

If Err.Number = 3010 Then

Resume Next

End If

End Function

Dave
 
Hi dcobau,
The FileSystemObject, File, Folder is not recognised. What should I do with them?
 

Users who are viewing this thread

Back
Top Bottom