VBScript to search for file and return filtered folder

I need a .vbs that will search for javaw.exe in subfolder of %programfiles%\Java\ and return the path of the version folder. eg. C:\Program Files\Java\jre1.5.0_17\bin\javaw.exe and return C:\Program Files\Java\jre1.5.0_17. Then filter the output to be only "jre1.5.0_17" or whatever the installed version folder name is.

Here is the .bat that does it:


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

I've been searching on recursive searches but I'm hopeless with VB

0 Comments   [ + ] Show comments

Answers (2)

Answer Summary:
Posted by: dugullett 10 years ago
Red Belt

For whatever reason I skipped VB and went straight to powershell. You can use this.

$dir = "${Env:ProgramFiles}\java"

Get-ChildItem -path $dir -recurse -Include java.exe|Format-Table directory -HideTableHeaders

This will give you an output of the directory. If you want to output it to a text use out-file.

$dir = "${Env:ProgramFiles}\java"
Get-ChildItem -path $dir -recurse -Include java.exe|Format-Table directory -HideTableHeaders|Out-file c:\temp\nameoffile.txt

  • Thanks Dugullett, unfortunately I need it to be VB as I'm running it from a custom action in a MSI. Any ideas on the VB side to return the folder name? - Micka007 10 years ago
    • My knowledge of VBScript is not even close to Powershell. I did find this. Maybe it could be a start. I think you just need to work on how it outputs.

      Option Explicit
      Dim strFolderToSearch, objFSO, objRootFolder, objFolder, colSubfolders, strOutput

      strFolderToSearch = "c:\program files\java"

      Set objFSO = CreateObject("Scripting.FileSystemObject")
      Set objRootFolder = objFSO.GetFolder(strFolderToSearch)
      Set colSubfolders = objRootFolder.SubFolders

      For Each objFolder in colSubfolders
      strOutput = strOutput & objFolder.name
      strOutput = strOutput & vbCrLf

      MsgBox strOutput - dugullett 10 years ago
Posted by: SilentKiller 10 years ago
Orange Belt

Try the following vbscript

Set oShell=CreateObject("WScript.Shell")
Set oFSO=CreateObject("Scripting.FileSystemObject")
Set RootFolder=oFSO.GetFolder(strFldSearch)
Set SubFolder=RootFolder.SubFolders

ForEach folderIn SubFolder
If oFSO.FileExists(strFolder) Then
MsgBox folder.Name

This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ