PDA

View Full Version : file search method using msoConditionIsExactly or msoConditionEquals



esfaryn
11-08-2005, 11:06 PM
From what i understand about File Search, the program will pick out the file name that contains the word/phase. However, what if all the files contain the same word/phase?

An example of different file names:
(1) "happy_work_009"
(2) "happy_tuesday_work_001"
(3) "happy_monday_work_001"
(4) "wednesday_work_005"

So basically, I want to retrieve all files that contains just the word "happy"? How do i search for a file in a directory?

And also, how do i condition the file search not to go thru the files whose size are 0kb and also to retrieve file from a period of time using msoConditionAnytimeBetween property??

Im using the vba to write the codings out. need urgent help please?

coding:


fs = application.filesearch
fs.newsearch
fs.lookin = directory
fs.filename = variable & "??????????????.dat"
With fs.PropertyTests
.Add Name:="Last Modified", Condition:=msoConditionAnytimeBetween, _
Value:=Format(dt1, "dd MMM yyyy"), _
SecondValue:=Format(dt2, "dd MMM yyyy")
' .Add name:="File Name", Condition:=msoConditionIsExactly, _
' Value:=variable & "??????????????*.dat"

' .Add name:="File Name", Condition:=msoConditionEquals, _
' Value:=variable & "??????????????*.dat"
end with
fs.execute


the ones marked bold are the ones causing the error. invalid argument or procedure call.

Zack Barresse
11-13-2005, 11:59 AM
Hi,

Have you checked the KB? Check this out, see if it helps any ...

http://vbaexpress.com/kb/getarticle.php?kb_id=694

esfaryn
11-13-2005, 06:02 PM
i tried using the code. the .matchtextexactly, but also doesnt seems to work.

johnske
11-13-2005, 10:53 PM
Does this part work for you? Sub OpenBooksInFolder()

Dim FileFound As String, N As Long
Dim Sheet As Worksheet, FilePath As String

'stipulate the folders path here
FilePath = "c:\windows\desktop\"

DoEvents
Application.ScreenUpdating = False

'list all the books in the folder
With Application.FileSearch
.LookIn = FilePath
.FileType = msoFileTypeExcelWorkbooks

'this should gives partial matches
'i.e. anything with 'happy' in the
'name regardless of case
.Filename = "happy"

'false = only search given folder
.SearchSubFolders = False

On Error Resume Next
If .Execute > 0 Then
For N = 1 To .FoundFiles.Count

'Debug.Print .FoundFiles(N) '< make a choice
Workbooks.Open (.FoundFiles(N)) '< make a choice

'now do what you want to do
'with each open workbook

Next N
Else
MsgBox "The file " & .Filename & " was not found"
End If
End With
Application.ScreenUpdating = True
End Sub

Stefan
05-20-2008, 07:33 AM
Hi,

I have managed to make a work around for the string match.
-First look for the "string in the string?"
-Then comapare string lengths

Implicitly true result!!!
(Partly in swedish)

Function FinnsFil(Filnamn, Folder) As Boolean
Dim Number
'Kontrollera om fil redan finns
Length = Len(Filnamn + Folder)
With Application.FileSearch
.NewSearch
.LookIn = Folder
.Filename = Filnamn
.SearchSubFolders = False
.MatchTextExactly = True
.MatchAllWordForms = False
Fil_Finns = .Execute(SortBy:=msoSortByFileName, SortOrder:=msoSortOrderAscending, AlwaysAccurate:=True) 'SortOrder:=msoSortOrderAscending
Number = .FoundFiles.Count
If Fil_Finns = 0 Then 'No match
FinnsFil = False
Else ' Full or partial match for further test by comparing also string length
'MsgBox "There were " & Number & _
" file(s) found."
'MsgBox CStr(Len(.FoundFiles(I))) + .FoundFiles(I)
I = 0
Do
I = I + 1
comparelength = Len(.FoundFiles(I))
If comparelength = Length Then
FinnsFil = True
Else
FinnsFil = False
End If
Loop Until FinnsFil = True Or I = Number
' End With
End If 'Fil_finns
End With
End Function