how to return the folder for Application.FileSearch.FoundFiles

M

muyBN

I need to return the folder in which I find a file with folder for
Application.FileSearch.FoundFiles. It is found in subfolders and from there I
need to know how to find the folder based on the file. Everything I've seen
here does pretty much the reverse. I found something in MS help on "returning
folders" but in actuality it only told about Dir, which returns files in
their examples, not folders. So I have done my homework and I came up with
nada.

Below is my macro code thus far, with comments expressing my frustration.
Thanks in advance for the last piece of the puzzle.

'returns file's extension, thus allowing me to set a .FileType in the
search
strFileType = Mid(strFile, InStr(strFile, ".") + 1)
If strFileType = "xls" Or strFileType = "mdb" Or strFileType = "doc" Or
strFileType = "ppt" Then
strFileType = msoFileTypeOfficeFiles
Else
strFileType = msoFileTypeAllFiles
End If
Set fs = Application.FileSearch
With fs
.NewSearch
.FileName = strName 'search for person's name in the file
.LookIn = "C:\[upper-level folder]"
.SearchSubFolders = True
.FileType = strFileType 'use the msoFileType feature provisioned above
.Execute
intFiles = .FoundFiles.count

'Loop through the list of found files and return
'the path of each one to variable strPath
strDetails = LCase(strName)
intCnt = 1
If intFiles > 0 Then
For intCnt = 1 To intFiles
If InStr(.FoundFiles.Item(intCnt), strDetails) Then
'Break out of the loop
intCnt = .FoundFiles.count
'return file's path
strPath = Dir(.FoundFiles.Item(intCnt))
'ABOVE ONLY RETURNS NAME OF FILE! not the folder
End If
Next intCnt
Else
MsgBox "No files found."
End If
End With
 
P

Perry

Parse the foldername from FoundFiles(intCnt), like in:

sFile = .FoundFiles.Item(intCnt)
strFolder = Left(sFile, InstrRev(sFile, ".") -1)

You also use FileSystemObject to loop through files/folders.

Krgrds,
Perry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top