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:

@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

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

Answer Summary:
Cancel
0 Comments   [ + ] Show Comments

Comments

Please log in to comment

Community Chosen Answer

3

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
      Next

      MsgBox strOutput
Please log in to comment

Answers

0

Try the following vbscript

Set oShell=CreateObject("WScript.Shell")
Set oFSO=CreateObject("Scripting.FileSystemObject")
strPgmFiles=oshell.ExpandEnvironmentStrings("%ProgramFiles%")
strFldSearch=strPgmFiles&"\Java"
Set RootFolder=oFSO.GetFolder(strFldSearch)
Set SubFolder=RootFolder.SubFolders

ForEach folderIn SubFolder
strFolder=strFldSearch&"\"&folder.Name&"\bin\javaw.exe"
If oFSO.FileExists(strFolder) Then
MsgBox folder.Name
EndIf
Next

Answered 07/05/2012 by: SilentKiller
Orange Belt

Please log in to comment
Answer this question or Comment on this question for clarity
Admin Script Editor
Admin Script Editor is an integrated scripting environment available free here at ITNinja

Share