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:
@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
Answers (2)
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
Comments:
-
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 12 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
Next
MsgBox strOutput - dugullett 12 years ago
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
For Each folder In SubFolder
strFolder=strFldSearch&"\"&folder.Name&"\bin\javaw.exe"
If oFSO.FileExists(strFolder) Then
MsgBox folder.Name
End If
Next