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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FileSearch Property

Status
Not open for further replies.

fumei

Technical User
Oct 23, 2002
9,349
CA
OK, really, I have tried looking. Can anyone tell me the syntax for TextOrProperty using Application.FileSearch.

From Help:

"Returns or sets the word or phrase to be searched for, in either the body of a file or the file's properties, during the file search."

Except ALL the examples I have found googling like mad, searching MSDN like mad, use text IN the document.

I know how to use TextOrProperty using a text string within the file. However, WHAT is the syntax for searching a file property???? TextOrProperty is itself a string, so I have tried:

"Number of Pages"
"Author"
"DocProperty(Author)"

yadda yadda

Say it is the Subject file property. The Subject property = "Can I really do it?". It is real. The file property IS that.

.TextOrProperty = "Can I really do it?" will NOT find the file.

It darn well always looks for a text string IN the file. I want to find a file property value.

I am missing something in the syntax, but I darn well can not figure it out, and I can not find a single example of a file property use. Just text IN a file.

Grrrrrr.

Oh......never mind. I finally figured it out. You need to use not TextOrProperty - although you'd think if it uses file properties, then you COULD - but make a PropertyTest. Like this:
Code:
Dim StartLocation As String
Dim msg As String
Dim var

StartLocation = "C:\Test\test2"
With Application.FileSearch
   .NewSearch
   .LookIn = StartLocation
   .FileName = "*.doc"
   .FileType = msoFileTypeAllFiles
   .PropertyTests.Add Name:="Subject", _
      Condition:=msoConditionBeginsWith, _
      Value:="Can"
   .Execute
End With

If Application.FileSearch.FoundFiles.Count > 0 Then
   For var = 1 To Application.FileSearch.FoundFiles.Count
      msg = msg & Application.FileSearch.FoundFiles(var) & _
         vbCrLf
   Next
   MsgBox msg
Else
   Msgbox "Nope, no files in that folder have a Subject starting with Can."
End If
will give me a list of all .doc files with Subjects that start with "Can".

Whew.....finally.

However, if someone DOES know a different syntax with TextOrProperty that actually uses a property, I would like to know it.

faq219-2884

Gerry
My paintings and sculpture
 
Gerry,

It frustrates the heck out of me when VBA doesn't work as it appears that it should; it obviously has the same effect on you. When I looked at your problem it seemed (seemed = illogical belief based on pure hunch) that there was something wrong with the basic syntax, so I played with it for a few minutes against some of my own files. Here is the result:

Code:
Dim msg As String
Dim Var
Sub FinderTest()
With Application.FileSearch
    .NewSearch
    .LookIn = "u:\Spreadsheets"
    .Filename = "*.xls"
    .SearchSubFolders = False
    .TextOrProperty = "OHA*"
    .FileType = msoFileTypeAllFiles
        If .Execute() > 0 Then
            For Var = 1 To .FoundFiles.Count
            msg = msg & .FoundFiles(Var) & _
                vbCrLf
            Next
        MsgBox msg
    End If
End With
msg = ""
End Sub

This found not only all of the instances of "OHA" text inside files, but also the names of all the files in this directory that had that string in their name. As shown, in my case the preliminary seaching was limited to one directory and one file type in order to save time, but I did run one test against everything; as you might presume, it took a lot longer, but it did work.

I hope this is of some use to you.


[thumbsup2]

P.S. After all the help you've given me, it feels good to be able to solve one of your problems for a change.

----------------------------------------------------------------------------------
"A committee is a life form with six or more legs and no brain." -- L. Long
 
This found not only all of the instances of "OHA" text inside files, but also the names of all the files in this directory that had that string in their name.
Yes, I have had that working for some time. That is not the issue.

My issue is not with a string either inside the file as content, nor in the filename.

My issue is with a string as value of a file property.

Filename: biddaGerryWhatever.doc
Inside the file: Gerry

I can find "Gerry" easy.

Author: (a file property) Mahatma Ghandhi

I want to find Mahatma Ghandhi. I am only interested in the file (and its content) IF the Author is Mahatma Ghandhi.
Code:
With Application.FileSearch
   .NewSearch
   .LookIn = StartLocation
   .SearchSubFolders = True
   .FileName = "*.doc"
   .FileType = msoFileTypeAllFiles
   .PropertyTests.Add Name:="Author", _
      Condition:=msoConditionBeginsWith, _
      Value:="Mahatma Ghandhi"
   .Execute
End With
This works. It will search the starting folder, and all subfolders, for .doc files whose Author is Mahatma Ghandhi.

I am happy to have figured that out. I am still very dumb about about the msoCondition parameters. The above does NOT work using msoConditionEqualsTo.

Some of the available msoCondition with PropertyTest are down right over my head.

msoConditionNotEqualToWaitingForSomeoneElse

What the heck is THAT?

However, my original question remains.

Help states that .TextOrProperty:

"Returns or sets the word or phrase to be searched for, in either the body of a file or the file's properties, during the file search."

If this true - and why would Microsoft lie, although of course they could simply be in error....

[rofl]

...if this is true, what the heck is the syntax? How do I use .TextOrProperty to test the value of the file property "Author"?

If VBA makes any sense...

[rofl]

then it has got to be a matter of syntax. I think.




faq219-2884

Gerry
My paintings and sculpture
 
Gerry,

Okay, I admit there is some confusion on my part; okay a LOT of confusion. When I did my test it did indeed find the tested-for string in the Title property as well as in the body of the files. It was my impression this was what you were trying to accomplish. A quick test substituting my last name for the "OHA" parameter did indeed bring up a list of all of the files where I am shown as Author, which as also a Property.

If this is not what you were looking for then I'm totally lost. A re-reading of your original post still seems to say that this is exactly what you're trying to do, so it is apparent that I'm missing some key fact. All I can say with any certainty is that my code did find every Property that I tested for, without exception. Could the problem be that the way the result is being displayed shows only the Titles of the found files, rather than the tested-for parameter?

One way or the other, it's clear that I need you to clarify what you're trying to do with this code: I can't be too far afield or I wouldn't be getting such consistent results.

[hairpull]

----------------------------------------------------------------------------------
"A committee is a life form with six or more legs and no brain." -- L. Long
 
No, you have it correct. Yes, that does work.

BUT: it is not actually performing a test on Author. It is not returning an explicit value for an explicit Property.

It will make .Execute > 0 (it found a file), and yes by doing so, can return the filename.

Let me put it this way.

Filename: yadda.doc
Author: Gerry Knight
Somewhere in the document: "Mahatma Ghandhi"

Then your code will return .Execute > 0...it found a file with "Mahatma Ghandhi".

Except, I don't want that file. I only want files with a specific Author.

It seems that .TextOrProperty is precisely that. Text OR Property. FileSearch uses the string to return a Found if it finds that string anywhere - either in text OR in a property.

Shrug. OK, that makes sense. It is that "Or".

Fortunately, I did find the way to actually test a specific property - eg. Author - using PropertyTest. So, essentially, it is solved.

.TextOrProperty is global, with no way to distinguish whether a Found is in text, OR, a property.

.PropertyTest is explicit.

faq219-2884

Gerry
My paintings and sculpture
 
What I am saying is:
All I can say with any certainty is that my code did find every Property that I tested for, without exception.
is not quite correct.

It did not find every Property you tested for. It found every string you tested for, REGARDLESS of where that string was located.

I wanted to test for a string in a specific Property.

.TextOrProperty will not do this. It is a global search/test.

Example:

Filename: yadda1.doc
Comments: (in file property) "needs review"
Author: Bob Marley

Filename: yaddayadda.doc
Author: Carlos Santana
Somewhere in the document: "needs review"

.TextOrProperty = "needs review"

Will return Found for: yadda1.doc AND yaddayadda.doc
as it makes no distinction. TextOrProperty

.PropertyTest.Add Name:="Comments", Value:= "needs review"

will return Found for: ONLY yadda1.doc

faq219-2884

Gerry
My paintings and sculpture
 
Gerry,

You're quite right, it was not discriminating between files where the AUTHOR was "MyLastName" and files where that string was simply text buried somewhere in the file. Since I was doing the test searchs in one of my own libraries it was almost guaranteed that no files would come up with my name anywhere other than in the Author property; I'm tracking now. The next time something like this comes up you can be sure that the testing will be done in a public folder.

And once again, thanks for correcting my foolish error. Perhaps someday I'll get proficient enough with VBA that I really will be able to assist you with a problem. In the meantime I'll just put a few more dents in my forehead and go home.

[banghead]

----------------------------------------------------------------------------------
"A committee is a life form with six or more legs and no brain." -- L. Long
 
Hey, don't get your knickers in a knot. I admire and respect your involvement.

What foolish error? I see no foolish error. Relax.
In the meantime I'll just put a few more dents in my forehead and go home.
No need. Here....have a pillow.

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top