Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FileSearch within a FileSearch

Status
Not open for further replies.

phudgens

Technical User
Jul 8, 2004
117
US
I created VBA code recently for Excel 2003 in which I use the code:

Set fs2 = Application.FileSearch
fs2.LookIn = CheckFolder
fs2.Filename = "*.*;*"
fs2.SearchSubFolders = True
fs2.Execute

to create a list of files. I then loop through the FoundFiles, opening them one by one and extracting various pieces of information. The code worked great. I was then asked to create additional functionality that required creating a second list of files for which I created an fs3 object with code similar to the above. When the fs3.Execute line is run, files from the fs3 file list replace files in the fs2 file list, which obviously screws up my program. The two lists of files are being drawn from different folders, on the same drive. Is there some reason that a second Application.Filesearch cannot be executed from within a loop that is cycling through a list of files from the first Application.Filesearch?

Thanks,
Paul Hudgens
 
This is clearly stated in the VBA help that there is one single Application.FileSearch object.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
It's because even though you dimension them differently they will both use the same FoundFiles collection (illustrated here):
Code:
Dim fs2

Dim fs3

Set fs2 = Application.FileSearch

Set fs3 = Application.FileSearch

fs2.LookIn = "C:\"
fs2.Filename = "*.txt"
fs2.SearchSubFolders = False
fs2.Execute

If fs2.FoundFiles.Count > 0 Then
    MsgBox fs2.FoundFiles.Count ' 11 (for me)
    MsgBox fs3.FoundFiles.Count ' 11 (for me)
End If
Nowif someone was just writing an aternative to FileSearch.. [wink]

HarleyQuinn
---------------------------------
Black coat, white shoes, black hat, cadillac. The boy's a timebomb!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before post
 
>Unfortnately there is only one FileSearch object in an application. FS2 and FS3 are, in essence, simply pointers to that object. Change one and you inevitably change the other, I'm afraid
 
Thanks very much to everyone for your replies. I had assumed that having two different objects (ie fs2 & fs3) would keep the lists of found files separate. I'm somewhat surprised to find that that is not the case.

PS: I did check the online help for FileSearch and found nothing that refers to: "one single Application.FileSearch object". But then I've come to the conclusion that MS online help is scarcely thorough.

Thanks
Paul H.
 
Why, simply dim a collection variable and fill it with your first results before exectuing your second file search.
That'll leave you with two collections of files...

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top