-
1
- #1
Writing a function that search any drive specified. In that drive specified it must search all directories and their subsequent subdirectories for a specified excel file and display file name parent drive and location found. For example
1. Search each folder for an excel file
2. If excel file not found in folder and subfolder say so then move to next folder untill all folders are searched
3. If excel found display folder name, location
I have tried to do it but as you can see it does not work and when I managed to do it it never checks for none existence for search sun directories and only does it one.
Public Function SearchForFile()
On Error GoTo SearchForFile_Fail
Dim strDriveLoc As String
Dim strSearchFor As String
Dim strPath As String
Dim strPathMatch As String
strDriveLoc = "G:\" ' Must not say exact location of folder
strSearchFor = "test1.xlsx"
strPath = strDriveLoc & "\" & strSearchFor
strPath = strDriveLoc & "\" & strSearchFor
sFile = Dir(strPath, vbDirectory)
Do While Len(sFile) > 0
'If Left(sFile, 1) <> "." Then
If strDriveLoc <> "." And strDriveLoc <> ".." Then
If (GetAttr(strDriveLoc) And vbDirectory) = vbDirectory Then
strPathMatch = sFile
Debug.Print sFile 'display file name
Debug.Print Dir(strPath, vbDirectory) 'display parent directory
Debug.Print strPath 'display path
'Exit Do
End If
Else
Debug.Print "file not fount in that directory:" & Dir(strPath, vbDirectory)
End If
sFile = Dir
Loop
Exit_SearchForFile:
Exit Function
SearchForFile_Fail:
MsgBox "Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & Chr(13) & Err.Description
Resume Exit_SearchForFile
End Function
Again thank you in advance for all your help and will be much appreciated.
1. Search each folder for an excel file
2. If excel file not found in folder and subfolder say so then move to next folder untill all folders are searched
3. If excel found display folder name, location
I have tried to do it but as you can see it does not work and when I managed to do it it never checks for none existence for search sun directories and only does it one.
Public Function SearchForFile()
On Error GoTo SearchForFile_Fail
Dim strDriveLoc As String
Dim strSearchFor As String
Dim strPath As String
Dim strPathMatch As String
strDriveLoc = "G:\" ' Must not say exact location of folder
strSearchFor = "test1.xlsx"
strPath = strDriveLoc & "\" & strSearchFor
strPath = strDriveLoc & "\" & strSearchFor
sFile = Dir(strPath, vbDirectory)
Do While Len(sFile) > 0
'If Left(sFile, 1) <> "." Then
If strDriveLoc <> "." And strDriveLoc <> ".." Then
If (GetAttr(strDriveLoc) And vbDirectory) = vbDirectory Then
strPathMatch = sFile
Debug.Print sFile 'display file name
Debug.Print Dir(strPath, vbDirectory) 'display parent directory
Debug.Print strPath 'display path
'Exit Do
End If
Else
Debug.Print "file not fount in that directory:" & Dir(strPath, vbDirectory)
End If
sFile = Dir
Loop
Exit_SearchForFile:
Exit Function
SearchForFile_Fail:
MsgBox "Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & Chr(13) & Err.Description
Resume Exit_SearchForFile
End Function
Again thank you in advance for all your help and will be much appreciated.