computing
  • 3

[Solved] VBScript To Find File And Return Filtered Folder Path

  • 3

I need a .vbs to search subfolders of %programfiles%\adobe and find acrobat.exe (eg. %programfiles%\Adobe\Acrobat 9.0\Acrobat) then return and filter the results to the Acrobat version folder (eg. Acrobat 9.0).

I have an old batch which does this but I’m not sure on the VB side :
@ECHO OFF

PUSHD “%programfiles%\java”
FOR /f “tokens=1-4 delims=\” %%a IN (‘DIR /b /s “javaw.exe”‘) DO ECHO %%a\%%b\%%c\%%d
POPD
PAUSE

This is what I have so far to search %programfiles%\Java and return all files in subdirectories:
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
objStartFolder = “%programfiles%\Java”

Set objFolder = objFSO.GetFolder(objStartFolder)
Wscript.Echo objFolder.Path
Set colFiles = objFolder.Files
For Each objFile in colFiles
Wscript.Echo objFile.Name
Next
Wscript.Echo

ShowSubfolders objFSO.GetFolder(objStartFolder)

Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
Wscript.Echo Subfolder.Path
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
For Each objFile in colFiles
Wscript.Echo objFile.Name
Next
Wscript.Echo
ShowSubFolders Subfolder
Next
End Sub

Share

1 Answer

  1. Best Answer

    Fair enough. There should be a better way, presumably by querying the registry, but I don’t have Acrobat installed, so I can’t go hunting by myself.

    Const startDir = "Adobe"
    Const fileName = "acrobat.exe"
    'ssfPROGRAMFILES = 0x26
    programFiles = CreateObject("Shell.Application").Namespace(&H26;).Self.Path
    Set fso = CreateObject("Scripting.FileSystemObject")
    dir = programFiles & "" & startDir
    
    If fso.FolderExists(dir) Then _
      file = FindFile(LCase(fileName), fso.GetFolder(dir))
    If Len(file) = 0 Then
      WScript.Echo "Error: File Not Found"
      WScript.Quit 2
    End If
    Set folder = fso.GetFolder(file & "\..\..")
    WScript.Echo folder.Name & ": " & folder
    
    
    WScript.Quit
    Function FindFile(ByRef sName, ByRef oFolder) 'As String
      FindFile = ""
      For Each file In oFolder.Files
        If LCase(file.Name) = sName Then
          FindFile = file
          Exit Function
        End If
      Next 'file
      For Each dir In oFolder.SubFolders
        FindFile = FindFile(sName, dir)
        If Len(FindFile) Then _
          Exit Function
      Next 'dir
    End Function

    How To Ask Questions The Smart Way

    • 0