0

hey guys i know this may sound stupid, but i am stuck with this question in my head...im really new to this wscript or vbscripting....at the time of writing i figured out how to open IE using wscript...heres the code

Set WshShell = WScript.CreateObject("WScript.Shell")
Return = WshShell.Run("iexplore.exe www.bbc.co.uk", 1)

but i cant figure out how to check if firefox is installed, then open firefox, if chrome is installed, open chrome, and the same thing goes for all the browser types.....

Update:

I did a little research and thought why not check the registry for that, so i came up with this script for checking the registry, now i dont know why but this always gives the same output "key does not exists" event though i have this registry in my system

keyTest = keyExists("HKEY_LOCAL_MACHINE\SOFTWARE\Mozilla\Mozilla Firefox")
If keyTest = False Then
    wscript.echo "Key does not exist"
Elseif keyTest = True then
    wscript.echo "Key exists"
End if

Function keyExists (RegistryKey)
    If (Right(RegistryKey, 1) <> "\") Then
        RegistryKeyExists = false
    Else
        On Error Resume Next
        WshShell.RegRead RegistryKey
        Select Case Err

            Case 0:
                keyExists = true
            Case &h80070002:
                ErrDescription = Replace(Err.description, RegistryKey, "")
                Err.clear
                WshShell.RegRead "HKEY_ERROR\"
            If (ErrDescription <> Replace(Err.description, _
            "HKEY_ERROR\", "")) Then
                keyExists = true
            Else
                RegistryKeyExists = false
            End If
            Case Else:
                keyExists = false
        End Select
        On Error Goto 0
    End If
End Function
Hmxa Mughal
  • 394
  • 1
  • 4
  • 17

2 Answers2

0

Problems in your example:

  • In keyExists(), a variable named RegistryKeyExists is being used for the return value from the function when keyExists is intended.

  • The Shell object variable WshShell is never instantiated via CreateObject().

  • The value of the registry key of interest does not end with a backslash.

Here's my streamlined version of your code which I believe accomplishes your objective:

Option Explicit  ' programming with your seatbelt on :-)

Dim keys(4)
keys(0) = "HKEY_LOCAL_MACHINE\SOFTWARE\Mozilla\Mozilla Firefox"
keys(1) = "HKEY_LOCAL_MACHINE\SOFTWARE\Mozilla\Mozilla Firefox\"
keys(2) = "HKEY_LOCAL_MACHINE\Bad\Key\"
keys(3) = "BAD\Root\On\This\Key\Causes\Exception"
keys(4) = "HKLM\SOFTWARE\Microsoft\Internet Explorer\"

On Error Resume Next

Dim i, key
For i = 0 To UBound(keys)
    key = keyExists(keys(i))

    If Err Then
        WScript.Echo "An exception occurred reading registry key" _
               & " '" & keys(i) & "':" _
               & " [" & Err.Number & "] " _
               & Err.Description _
               & ""
    Else
        If keyExists(keys(i)) Then
            WScript.Echo "Key *exists*: [" & keys(i) & "]"
        Else
            WScript.Echo "Key does *not* exist: [" & keys(i) & "]"
        End If
    End If

    WScript.Echo "--"
Next

Function keyExists (RegistryKey)
    Dim keyVal, errNum, errDesc

    keyExists = False
    On Error Resume Next

    Dim WshShell : Set WshShell = CreateObject("WScript.Shell")
    keyVal = WshShell.RegRead(RegistryKey)

    Select Case Err
        Case 0
            keyExists = True

        Case &h80070002
            ' key does not exist

        Case Else
            errNum = Err.Number
            errDesc = Err.Description
            On Error GoTo 0
            Err.Raise vbObjectError + 1, "WScript.Shell", _
               "Something went wrong reading the registry:" _
               & " [" & Hex(errNum) & "] " & errDesc
    End Select

    On Error GoTo 0
    Set WshShell = Nothing
End Function

' End
DavidRR
  • 18,291
  • 25
  • 109
  • 191
  • @Hmxa Mughal: After a good night sleep, it sank in what you were trying to do with your exception handling. So, I revised my example to incorporate improved exception handling and added several unit tests. – DavidRR Jul 07 '12 at 13:16
0

Generally following code can be used to find out to get List of All Installed Software. Here I have used Message box to display this list, you can use if condition to find out desired software is installed or not............

' List All Installed Software
  Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
  strComputer = "."
  strKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
  strEntry1a = "DisplayName"


  Set objReg = GetObject("winmgmts://" & strComputer & _
   "/root/default:StdRegProv")
  objReg.EnumKey HKLM, strKey, arrSubkeys

  For Each strSubkey In arrSubkeys
      intRet1 = objReg.GetStringValue(HKLM, strKey & strSubkey, _
      strEntry1a, strValue1)

      If strValue1 <> "" Then
          MsgBox  VbCrLf & "Display Name: " & strValue1
      End If
  Next

I have tried this code on machine & found that,it just listing Firefox browser, even when i have installed chrome & IE.So this regular method wont work surely for everyone. After that I have checked registry and found that,all browser are listed on.....

HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet\

So we can write code to find is is particular browser is installed or not. Following sample code to check if Chrome & Firefox is installed or not and if installed open it with URL passed

Set WshShell = CreateObject("WScript.Shell")
Const HKEY_LOCAL_MACHINE = &H80000002 
strComputer = "." 

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
strComputer & "\root\default:StdRegProv") 

strKeyPath = "SOFTWARE\Clients\StartMenuInternet\chrome.exe\shell\open\command\" 
strValueName = "" 
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue 
If InStr(1,strValue,"chrome",vbTextCompare) Then WshShell.Run("chrome www.google.com")


strKeyPath = "SOFTWARE\Clients\StartMenuInternet\FIREFOX.EXE\shell\open\command\" 
strValueName = "" 
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue 
If InStr(1,strValue,"firefox",vbTextCompare) Then WshShell.Run("firefox www.google.com")

Similarly you can modify this code for IE, Opera & Safari Hope this helps.......

Amol Chavan
  • 3,835
  • 1
  • 21
  • 32
  • hey 4M01, thanx for your reply....the code you sent me worked perfectly for the firefox, even though i have the chrome installed in my system...it still opens the firefox....is there any code or script using which i can see for example: if(firefox) open firefox else if(chrome) open chrome else open IE – Hmxa Mughal Jul 09 '12 at 14:15
  • just copy the above code from last code box, give it any name, save with .vbs extn & double click on vbs file – Amol Chavan Jul 09 '12 at 14:19