1
votes

So, I'm just starting programming classes in my college courses and we have to make a script to launch multiple programs. Can anyone help out? This is what I have so far:

Set objShell = CreateObject("WdScript.Shell") 
objShell.Run """C:\Program Files (x86)\Skype\Phone\Skype.exe"""
objShell.Run """C:\Program Files (x86)\Steam\Steam.exe""
Set objShell = Nothing
X=MsgBox("Running Skype and Steam!",0+64,"Succeeded") 
2
Please tell your issue. Also look at How to Ask please - lokusking
Your code looks good except for a typo on line 1 - jeffld
There's a second typo in line 3 (missing double quote at the end of the line). Still, typos aside the code looks fine. Voting to close. - Ansgar Wiechers

2 Answers

2
votes
Set objShell = CreateObject("WScript.Shell") 
0
votes

Give a shout for this code :

Option Explicit
If AppPrevInstance() Then   
    MsgBox "Instance already running",VbExclamation,"Instance already running"    
    WScript.Quit   
Else      
        Call Main(Array("c:\toto1.bat",_
        "c:\toto2.bat",_
        "c:\toto3.bat",_
        "%ProgramFiles%\Internet Explorer\iexplore.exe",_
        "Skype.exe",_
        "Steam.exe"))   
End If   
'**************************************************************************
Sub Main(colProcessPaths)   
    Dim ProcessPath   
    For Each ProcessPath In colProcessPaths     
        RunProcess(ProcessPath)   
    Next   
End Sub   
'**************************************************************************
Sub RunProcess(ProcessPath)   
    Dim ProcessName : ProcessName = StripProcPath(ProcessPath)   
    With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")   
        With .ExecQuery("SELECT * FROM Win32_Process WHERE Commandline LIKE " &  CommandLineLike(ProcessName))   
            If .Count = 0 Then    
                With CreateObject("WScript.Shell")  
                    .Run DblQuote(ProcessPath) 
                End With    
            Else    
                Exit Sub    
            End if   
        End With   
    End With   
End Sub   
'**************************************************************************
Function AppPrevInstance()   
    With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")   
        With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
        " AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")   
            AppPrevInstance = (.Count > 1)   
        End With   
    End With   
End Function    
'**************************************************************************
Function StripProcPath(ProcessPath)   
    Dim arrStr : arrStr = Split(ProcessPath, "\")   
    StripProcPath = arrStr(UBound(arrStr))   
End Function   
'**************************************************************************
Function CommandLineLike(ProcessPath)   
    ProcessPath = Replace(ProcessPath, "\", "\\")   
    CommandLineLike = "'%" & ProcessPath & "%'"   
End Function
'**************************************************************************
'Fonction pour ajouter les doubles quotes dans une variable
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'**************************************************************************