/build/static/layout/Breadcrumb_cap_w.png

Scripting Question


VBScript to search for file and return filtered folder

06/19/2012 14840 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:

@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:
0 Comments   [ + ] Show comments

Comments



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

All 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

 
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