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

Recursive File Search vbscript

Status
Not open for further replies.

mupdike

MIS
Dec 5, 2002
3
US
I have searched in here and found a few examples that were close to what I need to do. In this script I am searching the ProgamFiles folder and subfolders to identify the directory that contains excel.exe (due to an error by the person who created the office installations for my company it can be in one of four locations). I then need to use that value and append to it the addins folder (haven't progressed to adding this part yet). This search works fine when I use fso.GetEXtensionName(file) = "exe" (it returns the first exe in a subdir of program files) but I cannot seem to get it to look for the whole file name. All help is greatly appreciated!!!
Mike
[script paste]
Code:
'Option Explicit
 
Dim WshShell, fso, Dir, strIPDFold, strExec, oExec, strProgFold
Set WshShell = WScript.CreateObject("WScript.Shell")                
Set fso = CreateObject("Scripting.FileSystemObject")
Dim strCurrentFolder: strCurrentFolder = fso.getParentFolderName(Wscript.ScriptFullName): If Right(strCurrentFolder, 1) <> "\" Then strCurrentFolder = strCurrentFolder & "\"
Dir = strCurrentFolder

strProgFold = WshShell.ExpandEnvironmentStrings("%ProgramFiles%"): If Right(strProgFold, 1) <> "\" Then strProgFold = strProgFold & "\"
 
 
Dim strDir, objDir, aItem, iItem, bItem
 
strDir = "C:\Program Files\"
 
getInfo(strDir)
 
Function getInfo(Path)
Set Folder = fso.GetFolder(Path)
Set Files = Folder.Files
 
For Each File in Files
 
'If fso.GetExtensionName(file) = "exe" Then
            If fso.FileExists(file) = ("excel") Then
                        MsgBox "FileFound in" & Folder.Path
                        WScript.Quit
            End If
'End If
 
Next
 
Set Subfolders = Folder.SubFolders
 
For Each Subfolder in SubFolders
getInfo(Subfolder.path)
Next
 
 
End Function
 
It looks like you're pretty close, I've written some VBScripts and VB.NET apps similar to this. Inside your For Each File in Files loop you can access the full filename using File.Name

You'll probably want a condition such as
If File.Name = "Excel.exe" Then...
Since the command fso.FileExists(file) will return a value of either True or False. Other than that your recursion looks pretty good, you should be pretty close to getting it working how you want. Hope this helps!
 
Thank you for the response! I made the change suggested and when the script runs the MsgBox does not get triggered. Here is the modified script (in case I made any mistakes in implementing your advice since I am still pretty raw at scripting). I also copied the excel.exe into a folder I know the script has found an active file (using the extensionname process) and still nothing.
Thanks again!!

Code:
'Option Explicit
 
Dim WshShell, fso, Dir, strProgFold
Set WshShell = WScript.CreateObject("WScript.Shell")                
Set fso = CreateObject("Scripting.FileSystemObject")
Dim strCurrentFolder: strCurrentFolder = fso.getParentFolderName(Wscript.ScriptFullName): If Right(strCurrentFolder, 1) <> "\" Then strCurrentFolder = strCurrentFolder & "\"
Dir = strCurrentFolder

strProgFold = WshShell.ExpandEnvironmentStrings("%ProgramFiles%"): If Right(strProgFold, 1) <> "\" Then strProgFold = strProgFold & "\"
 
 
Dim strDir, objDir, aItem, iItem, bItem
 
strDir = "C:\Program Files\"
 
getInfo(strDir)
 
Function getInfo(Path)
Set Folder = fso.GetFolder(Path)
Set Files = Folder.Files
 
For Each File in Files
 
'If fso.GetExtensionName(file) = "exe" Then
            If File.Name = "Excel.exe" Then
                        MsgBox "FileFound in" & Folder.Path
                        WScript.Quit
            End If
'End If
 
Next
 
Set Subfolders = Folder.SubFolders
 
For Each Subfolder in SubFolders
getInfo(Subfolder.path)
Next
 
 
End Function
 
Perhapas this ?
If LCase(File.Name) = "excel.exe" Then

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
How about trying this for the condition:
Code:
If LCase(File.Name) = "excel.exe" Then
     MsgBox "File Found in " & File.Path
     WScript.Quit
End If
 
That did the trick, Thank you for the help!!!

Mike
 
Of course, if it is the seeking of the location of excel.exe rather than wanting a recursion routine that is important then:
Code:
[blue]MsgBox CreateObject("Wscript.Shell").RegRead("HKLM\SOFTWARE\Microsoft\Office\11.0\Excel\InstallRoot\Path")[/blue]
may be of interest (the above is for Excel 2003; Excel XP would have 10.0 rather than 11.0, Excel 2000 would be 9.0, and Excel 97 would be 8.0)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top