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

File Search in VBS

Status
Not open for further replies.

snotmare

Programmer
Jan 15, 2004
262
US
Greetings!

I have some code that works in access (VBA), but I can't figgure out the VBS equivalent. Here is sample code:

With Application.FileSearch
.NewSearch
.LookIn = "C:\WINNT\"
.SearchSubFolders = True
'(more statements)
End With

The problem is that "FileSearch" needs to be part of "Application". You can't use "Application" in VBS, so the question is, how do I use "FileSearch" in VBS?

Thank you much!
The Ben
 
Dude, what are you trying to accomplish? Are you looking for a particular file or folder on the system - a recursive search?

Any file/folder work is done using 'CreateObject("Scripting.FileSystemObject")'. Give me some more details on your goal and I can help you out.
 
Here is the complete code that I have (explanation at bottom):

With Application.FileSearch
.NewSearch
.LookIn = "C:\"
.SearchSubFolders = True
.FileName = "*Test*"
.MatchAllWordForms = True
.MatchTextExactly = False
.FileType = 1
.TextOrProperty = "*test*"

If .Execute() > 0 Then
MsgBox "There were " & .FoundFiles.Count & " file(s) found."

For intCounter = 1 To .FoundFiles.Count
MsgBox .FoundFiles(intCounter)
Next
Else
MsgBox "There were no files found."
End If
End With

This code functions just like doing a search in windows explorer. You can set it to look for the file names, or text within the file. For our purposes, we need it to look within the file, reguardless of what the file type is.

I have tried using FileSystemObject.OpenAsTextStream, but this will only return text in compatable text formats (.txt, .bat, .dat). When you try to use TextStream on a word document, it will return unreadable characters.

This code works exactly as I need it to in Access, but I need it to work in VBScript. The only problem with the code is the statement "Application.FileSearch". "Application" is not valid in VBS, nor is "WScript.FileSearch", "(file system object).FileSearch", and "FileSearch" by itself. How can I use "FileSearch" in VBS????

Thanks a bunch!
The Ben
 
This code will enumerate all the files in a folder.


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService. _
ExecQuery("Select * from CIM_DataFile where Path = '\\Scripts\\'")
For Each objFile in colFiles
Wscript.Echo objFile.Name
Next



This code will enumerate all the sub folders:

Set FSO = CreateObject("Scripting.FileSystemObject")
ShowSubfolders FSO.GetFolder("C:\Scripts")
Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
Wscript.Echo Subfolder.Path
ShowSubFolders Subfolder
Next
End Sub


You should be able to nest the two scripts easily enough to get the effect you are looking for.
 
Hmm... I've only seen FileSystemObject used for working with folders, so I'm pretty sure we'll have to stick with it for this purpose.

________________________________________________________
Set objfso = CreateObject("Scripting.FileSystemObject")
Set oFolder = objfso.GetFolder(<InsertStartFolder>)
Set oSubFolder = oFolder.SubFolders
For Each Folder in oSubFolders
Set oFiles = Folder.Files
For Each oFile in oFiles
[Do whatever work you want here]
Next

[Do whatever work you need against each folder]
Next
**************************************************

VBScript's ability to open and read a file is limited to 'text stream', so encoded files will error out. You can use it to launch the appropriate app and load the file, but I dont know what you'd do from there - unless you wanted to mess with 'SendKeys'.

I am unclear why you need to open and read the contents of each file. If you can supply more information about what you're searching for in each file then I might be able to come up with something.

Sorry man, I spoke up too quick about being able to help you, but I still have some tricks up my sleeve.

Let me know

kh
 
That's cool, I'll soak in any ideas that people have. Here is what we're trying to do...

Our team has a lot of documentation in the formats .txt, .pdf, .doc, and .rtf. I'm new to our team, so as I learn the applications we use and how to solve problems, it's hard for me to find documentation on error codes, steps to do a certain process, etc. We want to house all this info in a documentation database so it's organized and a newbie like me can go to one resource to find my info. We're considering Lotus, Access, and a few other options. I want to be able to type in a key word and create a list of documents that have info on that key word.

My code will accomplish this in microsoft access, but I'm having trouble getting it to work in VBS. I hope this helps clear things up. If not, let me know and I'll see if I can make it clearer :).

Thanks,
The Ben
 
Have you tried this ?
Set wdApp=CreateObject(&quot;Word.Application&quot;)
Set FileSearch=wdApp.FileSearch
With FileSearch
...
End With
Set FileSearch=Nothing
wdApp.Quit
Set wdApp=Nothing
You can replace Word by any other Office application, as the FileSearch class is common to all of them.

Hope This Help
PH.
 
I think the thing that is going to hurt you the most is the desire to be able to scan PDF files too.

You might need to rethink your approach and go with something that you know works. The Windows search works, so why not automate it? Using a combination of AppActivate and SendKeys I would think you should be able to do what you are looking for.
 
I agree with PHV that your shortest path to a solution will be to obtain an automation reference to one of the Office applications, and then use the accessor property FileSearch to go from there.

This is how you must proceed to gain access to the shared Office components, as decribed in:


Though that web page doesn't show a VBScript example, PHV's has already given you the esssentials.

The main caution here is to avoid this technique in unattended server script such as an ASP page. Automation of Office components is not advised in such situations for several reasons:


Despite repeated warnings of this nature from Microsoft, ASP developers in particular often proceed full speed ahead anyway, damn the torpedos. Then they come back here and ask &quot;why me?&quot; ;-)

But for a desktop script this should work just find, and be very reliable as compared with SendKeys-based techniques. This cannot be emphasized enough:

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top