06/19/2012 13808 views

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

Answer Summary:
0 Comments   [ + ] Show comments


Community Chosen Answer


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
Answered 06/19/2012 by: dugullett
Red Belt

  • 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?
    • 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

All Answers


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

Answered 07/05/2012 by: SilentKiller
Orange Belt