Computer Hope

Software => Computer programming => Topic started by: trekxor on March 31, 2012, 03:55:26 AM

Title: can anyone improve on this? (VBS)
Post by: trekxor on March 31, 2012, 03:55:26 AM
I have this little bit of vbs that starts a number of Programs, here email and web. At work it's x 7. I was hoping to find a way to economise on the code.
Code: [Select]
strProgramPath = """C:\Program Files (x86)\Mozilla Firefox\firefox.exe"""
SET objShell = CREATEOBJECT("Wscript.Shell")
objShell.Run strProgramPath

strProgramPath = """C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe"""
SET objShell = CREATEOBJECT("Wscript.Shell")
objShell.Run strProgramPath
SET objShell = Nothing

thanks
Title: Re: can anyone improve on this? (VBS)
Post by: oldun on March 31, 2012, 05:03:20 AM
You only need to call 'CREATEOBJECT' once.
Code: [Select]
strProgramPath = """C:\Program Files (x86)\Mozilla Firefox\firefox.exe"""
SET objShell = CREATEOBJECT("Wscript.Shell")
objShell.Run strProgramPath

strProgramPath = """C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe"""
objShell.Run strProgramPath
SET objShell = Nothing

Or you could place the paths in an array and use a for loop:
Code: [Select]
arrPgm = Array("""C:\Program Files (x86)\Mozilla Firefox\firefox.exe""","""C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe""")
SET objShell = CREATEOBJECT("Wscript.Shell")
For Each pgm in arrPgm
objShell.Run pgm
wscript.sleep 1000
Next
SET objShell = Nothing
Title: Re: can anyone improve on this? (VBS)
Post by: Salmon Trout on March 31, 2012, 06:00:39 AM
SET objShell = Nothing

If pruning lines is your aim, you can omit this line as well. The script engine automatically clears variables when they go out of scope, so clearing them manually just before then (i.e. at the end of script execution) is pointless and unnecessary.


Title: Re: can anyone improve on this? (VBS)
Post by: trekxor on March 31, 2012, 08:12:46 AM
Thanks...