Executing for loop to call Windows application - vbscript

I have an existing VBScript which calls an application, then loops if the application exits via user intervention, if a certain user is logged in, otherwise it runs Explorer:
' Declare variables
dim fso
dim shell
dim oWshNet
set shell = CreateObject("WScript.Shell")
' get name of user logging in
set oWshNet = Wscript.CreateObject("Wscript.Network")
sUser = oWshNet.Username
' Run Aplication if user is user1
if oWshnet.Username = "user1" Then
do
shell.exec "C:\Program Files\Canon\MP Navigator EX 4.0\mpnex40.exe",0,true
loop
Else
shell.run "C:\windows\system32\explorer.exe"
end if
I have changed to a new application and now find that it will only run the application using shell.exec. shell.run causes a path not found error. Using shell.exec without the loop would not normally be a problem but as I require a loop which constantly rechecks for the exit of the application, I can't seem to use shell.exec with the ,0,true parameter.
Any ideas how I can get round this?

Stick with the Run method. Exec doesn't support additional arguments, and particularly doesn't support waiting for the executed program to return.
The reason why you got the "path not found" error is because your path contains spaces. You need to put double quotes around the path (inside the double quoted string) so that the whole path is recognized as one single token instead of multiple tokens C:\Program, Files\Canon\MP, aso.
Nested double quotes can be added for instance like this (doubling the double quotes escapes them inside a string):
shell.Run """C:\Program Files\Canon\MP Navigator EX 4.0\mpnex40.exe""", 0, True
or by using a quoting function (which is the more versatile approach):
Function qq(str) : qq = """" & str & """" : End Function
...
shell.Run qq("C:\Program Files\Canon\MP Navigator EX 4.0\mpnex40.exe"), 0, True

Related

VBScript using variables value

I am trying to run a VBScript which opens a program with elevated privileges and also pass it parameters.
Set oShell = CreateObject("Shell.Application")
Dim path = "app.exe"
If WScript.Arguments.Count = 1 Then
path = path & WScript.Arguments(0)
End If
oShell.ShellExecute path, , , "runas", 1
I get an error in the 2nd line. I tried using As String but that also didn't work.
Any ideas?
You are not allowed to declare a variable and set a value at the same time. Try this syntax instead.
Dim path
path = "app.exe"
or alternatively:
Dim path : path = "app.exe"
Where is the object WScript coming from? I don't see it initialized.
A great 'feature' to turn on is Option Explicit. When activated you must explicitly declare all variables by using the Dim or ReDim statements. If you try to use an undeclared variable name, an error occurs at compile time. This makes it easier to spot problems.

vbscript cannot find batch file stored in google drive

I am trying to run the following C:\users\jdoe\google drive\bin\script.vbs script on Windows 7:
CreateObject("Wscript.Shell").Run "C:\users\jdoe\google drive\bin\run.bat", 0, True
But I always get the error:
---------------------------
Windows Script Host
---------------------------
Script: C:\Users\jdoe\Google Drive\bin\script.vbs
Line:1
Char: 1
Error: The system cannot find the file specified.
Code: 80070002
Source: (null)
---------------------------
OK
---------------------------
When I change the path of my run.bat file to c:\run.bat and of course move the run.bat file to c:\, the script.vbs runs without problems.
Any way to get my scripts stored in google drive to run? I have the same issue when using the local group policy editor to select a shutdown or logon/logoff script that is stored in google drive...
Thanks a lot!
CreateObject("Wscript.Shell").Run "C:\users\jdoe\google drive\bin\run.bat", 0, True
^..................^ ^...............^
command to run arguments
You need to quote the the command to avoid problems with spaces
CreateObject("Wscript.Shell").Run """C:\users\jdoe\google drive\bin\run.bat""", 0, True
Remember that a double quote inside a string needs to be escaped, writting two double quotes where one must be included.
To avoid problems with spaces : you must try like this way to get rid of this error that comes from spaces in the path of your application:
Option Explicit
Dim Application
Application = "C:\users\jdoe\google drive\bin\run.bat"
Call RunThis(Application)
'*********************************************************************************
Sub RunThis(Application)
Dim Ws,Result
Set Ws = CreateObject("WScript.Shell")
Result = Ws.Run(DblQuote(Application),0,True)
End Sub
'*********************************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'*********************************************************************************

VBS WScript.Run fails after passing Exists test

In a couple of place in my code I check if the file exists (it does) then I try to Run the file as above, or get the DateLastModified, and get errors about file not found or invalid path. How can the script NOT see a file after confirming it exists?
I'm working up a .vbs script that tries to run an Access .mdb file. The WScript.Run command seems to choke on the filename, but putting a MsgBox() before that call to display the path allows Run to work properly. I don't want to display a popup.
Error is:
The directory name is invalid.
How is this possible and how can I get around it?
Here is code:
AccessFileName = "App.mdb"
LocalPath = "C:\Folder\"
SET ws = WScript.CreateObject("WScript.Shell")
path = Chr(34) & LocalPath & AccessFileName & Chr(34)
if (fso.FileExists(LocalPath & AccessFileName)) THEN
'MsgBox(path) 'Uncommenting this line removes the error
ws.Run path 'This line errors
End If
Try to open your file with shell .InvokeVerb method:
AccessFileName = "App.mdb"
LocalPath = "C:\Folder\"
If CreateObject("Scripting.FileSystemObject").FileExists(LocalPath & AccessFileName) Then
CreateObject("Shell.Application").Namespace(LocalPath).ParseName(AccessFileName).InvokeVerb
End If
UPD: Both ActiveX WScript.Shell and Shell.Application uses native windows shell to perform a file execution.The first one launches new process via WSH core located in wscript.exe, cscript.exe, wshom.ocx, jscript.dll, vbscript.dll, ets, .Run and .Exec methods of WsShell object provides wide control on the launched process, and second one located in Shell32.dll, uses .InvokeVerb method of IShellDispatch object, called without name, runs default verb equals to the windows explorer "open" command.In case of any issues connected to WSH, explorer might still works without any proplems. If it does, that is just a work-around, I can't say what's wrong definetely without close look.
Hello the following code worked for me.
Basically this code gets a folder object and loops through all files in a folder and checks if its the one that you named. This it runs the application.
Set fso = CreateObject("Scripting.FileSystemObject")
Set ws = Wscript.CreateObject("Wscript.Shell")
AccessFileName = "App.mdb"
LocalPath = "C:\Folder\"
Set myFolder = fso.GetFolder(LocalPath)
For each myFile in myFolder.Files
If myFile.Name = AccessFileName Then
'Wscript.Echo myFile.Name
ws.Run myFolder.Path & "\" & myFile.Name
End If
Next
You can give this a shot. You probably do not need the quotes around the path, but I included it as a comment if you want to give it a shot. You just put quotes twice if you need to include a quote character in a string:
Set fso = CreateObject("Scripting.FileSystemObject")
AccessFileName = "App.mdb"
LocalPath = "C:\Folder\"
Set ws = WScript.CreateObject("WScript.Shell")
' path = """" & LocalPath & AccessFileName & """" <-- probably unnecessary
path = LocalPath & AccessFileName
If (fso.FileExists(path)) Then
Set file = fso.GetFile(path)
'MsgBox(path) 'Uncommenting this line removes the error
ws.Run file.Path 'This line errors
End If
This does not make any sense. Having a MsgBox line is altering the behavior of the program!!!
I feel it is probably some weird invisible character somewhere which is getting activated when you comment the line.
Try retyping the If block without the MsgBox in between.

VBscript Unable to Run Shell Command

I have set up the following Sub to run shell commands quickly and easily.
This script works for the login scripts at my company without fail.
I am currently developing a script to add a large batch of users to our domain.
When I used this Sub in my new script I receive an error saying that the file cannot be found.
I have tried using the fix in this stackoverflow post, but I recieve the same error even with this code.
VBScript WScript.Shell Run() - The system cannot find the file specified
The part I find puzzling is that this sub works just fine when run from the netlogon folder of our domain controller.
What am I doing wrong?
Thanks,
Sub runcommand(strCommand)
Dim objWshShell, intRC
set objWshShell = WScript.CreateObject("WScript.Shell")
intRC = objWshShell.Run(strCommand, 0, TRUE)
call reportError(intRC,strCommand)
set objWshShell = nothing
end Sub
function reportError(intRC, command)
if intRC <> 0 then
WScript.Echo "Error Code: " & intRC
WScript.Echo "Command: " & command
end if
end function
The previous values for strCommand had no spaces and were very straightforward. Your new script is passing more complex variables to your Sub so you need additional conditional handling, as Alex K. pointed out in his Collusion (i.e., "Comment/Solution") above. Alex K.'s sample above is perfect, so, being a Point Pimp tonight, will post it as the solution:
objWshShell.Run("cmd /k echo Hello World", 1, TRUE)

Run a vbscript from another vbscript

How do I get a vbscript to run another vbscript?
Id imagine its only a few lines of code but not tried doing this before, nothing is passed between the 2, one just needs to call/run the other.
For examples the script being run is called TestScript.vbs, the other script for it to call/run would be called Secondscript.vbs, both of which are located in C:\Temp.
Thanks
Mark
See if the following works
Dim objShell
Set objShell = Wscript.CreateObject("WScript.Shell")
objShell.Run "TestScript.vbs"
' Using Set is mandatory
Set objShell = Nothing
You can try using the Wshshell.Run method which gives you little control of the process you start with it. Or you could use the WshShell.Exec method which will give you control to terminate it, get a response, pass more parameters (other than commandline args), get status, and others
To use Run (Simple Method)
Dim ProgramPath, WshShell, ProgramArgs, WaitOnReturn,intWindowStyle
Set WshShell=CreateObject ("WScript.Shell")
ProgramPath="c:\test run script.vbs"
ProgramArgs="/hello /world"
intWindowStyle=1
WaitOnReturn=True
WshShell.Run Chr (34) & ProgramPath & Chr (34) & Space (1) & ProgramArgs,intWindowStyle, WaitOnReturn
ProgramPath is the full path to your script you want to run
ProgramArgs is the arguments you want to pass to the script. (NOTE: the arguments are separated by a space, if you want to use an argument that contains a space then you will have to enclose that argument in quotes [Safe way to do this is use CHR (34) Example ProgramArgs= chr (34) & "/Hello World" & chr (34)])
IntWindowStyle is the integer that determines how the window will be displayed. More info on this and WaitOnReturn can be found here WshShell.Run Method
WaitOnReturn if true then the script will pause until the command has terminated, if false then the script will continue right after starting command.
NOTE: The Run method can return the exit code but you must set WaitOnReturn to True, and assign the 'WshShell.Run' to a variable. (EX: ExitCode=WshShell.Run (Command,intWindowStyle,True))
To Use EXEC (Advanced Method)
Dim ProgramPath, WshShell, ProgramArgs, Process, ScriptEngine
Set WshShell=CreateObject ("WScript.Shell")
ProgramPath="c:\test run script.vbs"
ProgramArgs="/hello /world"
ScriptEngine="CScript.exe"
Set Process=WshShell.Exec (ScriptEngine & space (1) & Chr(34) & ProgramPath & Chr (34) & Space (1) & ProgramArgs)
Do While Process.Status=0
'Currently Waiting on the program to finish execution.
WScript.Sleep 300
Loop
ProgramPath same as Run READ RUN'S DESCRIPTION
ProgramArgs DITTO
ScriptEngine The Engine you will be using for executing the script. since the exec method requires a win32 application, you need to specify this. Usually either "WScript.exe" or "CScript.exe". Note that in order to use stdin and stdout (we'll cover what these are a bit further down) you must choose "CScript.exe".
Process this is the Object that references to the program the script will start. It has several members and they are: ExitCode, ProcessID, Status, StdErr, StdIn, StdOut, Terminate.
More Details about the members of Process Object
ExitCode This is the exit code that is returned when the process terminates.
ProcessID This is the ID that is assigned to the process, every process has an unique processID.
Status This is a code number that indicates the status of the process, it get set to '-1' when the process terminates.
StdErr This is the object that represents the Standard Error Stream
StdIn This is the Object that represents the Standard Input Stream, use it to write additional parameters or anything you want to pass to the script you are calling. (Process.StdIn.WriteLine "Hello Other Worlds")
StdOut This is the Object that represents the Standard Output Stream, It is READONLY so you can use Process.StdOut.ReadLine. This is the stream that the called script will receive any information sent by the calling script's stdin. If you used the stdin's example then StdOut.Readline will return "Hello Other Worlds". If there is nothing to read then the script will hang while waiting for an output. meaning the script will appear to be Not Responding Note: you can use Read or ReadAll instead of ReadLine if you want. Use Read (X) if you want to read X amount of characters. Or ReadAll if you want the rest of the stream.
Terminate Call this method to force terminate the process.
For more information about WshShell.Exec go to Exec Method Windows Scripting Host
Just to complete, you could send 3 arguments like this:
objShell.Run "TestScript.vbs 42 ""an arg containing spaces"" foo"
You can also load the body of the script and execute it within the same process:
Set fs = CreateObject("Scripting.FileSystemObject")
Set ts = fs.OpenTextFile("script2.vbs")
body = ts.ReadAll
ts.Close
Execute body
In case you don't want to get mad with spaces in arguments and want to use variables try this:
objshell.run "cscript ""99 Writelog.vbs"" /r:" & r & " /f:""" & wscript.scriptname & """ /c:""" & c & ""
where
r=123
c="Whatever comment you like"
I saw the below code working.
Simple, but I guess not documented.
Anyone else used the 'Execute' command ?
Dim body, my_script_file
Set Fso = CreateObject("Scripting.FileSystemObject")
Set my_script_file = fso.OpenTextFile(FILE)
body = my_script_file.ReadAll
my_script_file.Close
Execute body
Try this.
Option Explicit
On error resume next
Dim Shellobj
Set Shellobj = CreateObject("WScript.Shell")
Shellobj.Run "Test.vbs"
Set Shellobj = Nothing
As Martin's Answer didn't work at all for me ("File not found") and atesio's Answer does not allow to call two scripts which include repeating variable definitions, here is another alternative which finally worked for me:
filepath = Chr(34) & "C:\...\helloworld.vbs" & Chr(34)
Set objshell= CreateObject("WScript.Shell")
objshell.Run "wscript " & filepath, , True
Set objshell= Nothing
(Windows 8.1)

Resources