'VBS WScript.Run With parameters?
I am trying to make a script to open up Windows movie player after a designated delay, however I cannot get windows Media Player to open up with a file passed as a parameter.
Heres what I have so far:
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.run "WMplayer" & ""C:\Users\Public\Videos\Sample Videos\Wildlife""
I am getting an error on line 3 char 29 : Expected end of statement.
Can anyone help me? It'd be greatly appreciated.
Solution 1:[1]
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.run "WMplayer ""C:\Users\Public\Videos\Sample Videos\Wildlife""",1,False
no need for &
in between of them
OR
Dim objShell,myMedia
Set objShell = WScript.CreateObject( "WScript.Shell" )
myMedia= chr(34) & "C:\Users\Public\Videos\Sample Videos\Wildlife" & chr(34)
objShell.run "WMplayer "& myMedia &"",1,False
Solution 2:[2]
You need to quote correctly:
objShell.run "wmplayer" & ""C:\Users\Public\Videos\Sample Videos\Wildlife""
==>
objShell.run "wmplayer" & " ""C:\Users\Public\Videos\Sample Videos\Wildlife"""
Evidence:
>> WScript.Echo "wmplayer" & " ""C:\Users\Public\Videos\Sample Videos\Wildlife"""
>>
wmplayer "C:\Users\Public\Videos\Sample Videos\Wildlife"
>>
If that works from a console, it will work from your script.
For a more structured/less error prone/better scaling approach see here.
Solution 3:[3]
If one of the command line parts is a variable, then create the whole string first, using an additional line, e.g.:
Dim objShell, commandlineString
Set objShell = WScript.CreateObject( "WScript.Shell" )
commandlineString = WMplayer & "C:\Users\Public\Videos\Sample Videos\Wildlife"
objShell.run commandlineString,1,False
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Community |
Solution 3 | WillyB |