Posted to microsoft.public.excel.programming
|
|
Problem with File.Search
Jim, can filesystemobject search in the file for text, I thought Ken was
needing to do that ...?
"Jim Cone" wrote in message
...
Ken,
I've seen comments in the programming group to the
effect that FileSearch is not always reliable.
See Myrna Larson's comments here as one example...
http://makeashorterlink.com/?C3145206B
The FileSystemObject code, part of the Windows Script Host
included with the Windows operating system (after Windows 95)
seems to be rock solid dependable and can be freely intermixed
with VBA code. For what it's worth, the following is code that
searches for files in the top folder and all sub-folders.
The results are listed on the active sheet.
'------------------------
'Microsoft Windows Script 5.6 Documentation
'http://msdn.microsoft.com/library/default.asp?url=/downloads/list/webdev.asp
Option Explicit
Option Compare Text
Sub ListFoldersAndSubFolderAndFiles()
Jim Cone - San Francisco, USA - May 24, 2005/July,02, 2005
'Requires a project reference to "Microsoft Scripting Runtime"
(scrrun.dll) '***
'List all files and folders in the specified folder.
Dim objFSO As Scripting.FileSystemObject
Dim objFolder As Scripting.Folder
Dim objFile As Scripting.File
Dim strPath As String
Dim strName As String
Dim lngNum As Long
'Specify the folder...
strPath = "C:\Documents and Settings"
'Specify the file to look for...
strName = "*.xls"
Set objFSO = New Scripting.FileSystemObject
Set objFolder = objFSO.GetFolder(strPath)
lngNum = 2
For Each objFile In objFolder.Files
If objFile.Name Like strName Then
Cells(lngNum, 2) = objFile.Path
lngNum = lngNum + 1
End If
Next 'objFile
Set objFile = Nothing
'Call recursive function
DoTheSubFolders objFolder.SubFolders, lngNum, strName
Set objFSO = Nothing
Set objFolder = Nothing
End Sub
'------------------------
Function DoTheSubFolders(ByRef objFolders As Scripting.Folders, _
ByRef lngN As Long, ByRef strTitle As String)
Dim scrFolder As Scripting.Folder
Dim scrFile As Scripting.File
Dim lngCnt As Long
For Each scrFolder In objFolders
For Each scrFile In scrFolder.Files
If scrFile.Name Like strTitle Then
Cells(lngN, 2).Value = scrFile.Path
lngN = lngN + 1
End If
Next 'scrFile
'If there are more sub folders then go back and run function again.
If scrFolder.SubFolders.Count 0 Then
DoTheSubFolders scrFolder.SubFolders, lngN, strTitle
End If
Next 'scrFolder
Set scrFile = Nothing
Set scrFolder = Nothing
End Function
'-------------------------------------
"Ken Loomis" wrote in message
...
This is driving me crazy.
I use the following to search for files that are ".xls" files and contain
a
sub name in the VBA:
MyFilePath is the path to the My Documents folder on whatever system this
runs on
StartTime = Time
With Application.FileSearch
.NewSearch
.FileName = "*.xls"
.LookIn = MyFilePath
.SearchSubFolders = True
.TextOrProperty = "BuildStreetsReports"
.MatchTextExactly = True
.Execute
EndTime = Time
MsgBox ("Done searching. It took " & (EndTime - StartTime) * 24 * 60
& " minutes")
On my Windows XP system that search takes les than 20 seconds.
But when I run it on my Windows 98 system, it can take up to 10 minutes
and
sometimes just hangs and won't come back at all, even if I let it run for
an
hour.
I am beginning to think there is something wrong with Windows or my Office
installation.
Has anyone else ever had this kind of problem with FileSearch, or have any
ideas or suggestions?
|