wshshell.run can not find file - vbscript

From a command line I can run
oradim.exe -delete -sid DataWare
Its in my path so I can do it from any location (I have tested this)
What is failing is
oraCMD = "oradim.exe -delete -sid DataWare"
errCMD = wshShell.exec(oraCMD)
if errCMD <> 0 Then
msgbox "ERROR: " & errCMD
wscript.quit
end if
In this configuration I get the error "Object doesn't support this property or method". If I use .run instead of .exec I get "The system cannot find the file specified".
Any ideas?

Your code mixes .Run (returning a numerical error code) and .Exec (returning an object and needing an assignment with Set). If the process started by WSHShell can't find oradim, then this process didn't get the PATH of the shell you used for testing. How did you start the .vbs? Easy way out: specify full path to oradim.exe.
On second thought: remember to quote the file spec if it contains spaces:
oraCMD = """c:\program files\ora labora\oradim.exe"" -delete -sid DataWare"

What solved the problem was calling the CMD in the command line
oraCMD = "CMD /c oradim.exe -delete -sid"

Did you try errCMD = wshShell.run(oraCMD) ?

Related

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
'*********************************************************************************

wshShell.Exec Error 80070002 "The system cannot find the file specified"

I'm trying to use either wshShell.Exec or wshShell.Run to run a script through another script. I have tried both methods, and both methods give me the same error. I've Googled the issue and can't find anything that seems to fix the issue. The only suggestion that really was very relevant was to try using wshShell.Run instead of Exec.
Here's the relevant part of my script:
strScriptPath = "T:\IT resources\Scripts\Shutdown Scripts"
strForceShutdown = "ForceShutdown.vbs"
For j = 0 to 99
Set objActive = wshShell.Run(strForceShutdown)
' In case I ever need to get this working to run it from another folder.
' Set objActive = wshShell.Exec("cd " & strScriptPath & "")
' Set objActive = wshShell.Exec("wscript " & strForceShutdown & "")
constConf = MsgBox("Automatic shutdown initializing. Continue?" & chr(10) & "Y=Shutdown N=Postpone 30 minutes",4,"Automatic Shutdown Notification")
If constConf = 7 Then
objActive.Terminate
Wscript.Sleep(1800000)
Else
objActive.Terminate
Exit For
End If
Next
Thanks for any help!
Shell.Run returns an integer, so you can't call a method (Terminate) on its return value. You also can't Set it since it's not an object.
You can call your shutdown script by just running it. Give it the full path, however, not a relative path. Scripts launched from Task Scheduler often have different "starting folders" than those launched manually so don't rely on your script finding the other one relatively.
Also, you'll have to add Chr(34) before and after your path to account for any spaces.
strForceShutdown = "c:\path\to\ForceShutdown.vbs"
wshShell.Run Chr(34) & strForceShutdown & Chr(34)
Finally, why launch the script and then ask whether to shutdown? Why not just launch your script after the user has responded and then you don't have to worry about terminating a running process.

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)

How can I find path of file

In the end, I am trying to return the 'Product Version' of Robocopy.exe (in order to avoid using XP026; broken return code). I can use Shell.Application, NameSpace, ParseName to get the 'Product Version', but I would need to know the NameSpace (folder|directory|path) beforehand.
I can't write a NameSpace path into the script since it will run on different computers and Robocopy.exe could be in one or more directories listed in the system PATH environment variable. I am only interested in the ones found in PATH, and further, the instance that will execute as a result of the WScript.Shell Run method. WScript.Shell Run will execute the first instance found in the system search path when absolute path is not specified. That's the one I'm interested in finding.
My backup plan is to use the where.exe program to find the full path to Robocopy.exe, return it to my vbs script using WScript.Shell Exec oExec.StdOut and extract the path to use as NameSpace in the code above.
I have been searching for a vbs or com control implementation of the winapi searchpath function/method and have had no luck. I'm surprised it's not already implemented in FileSystemObject or Shell.Application. I appreciate any help, referrals, or ideas.
To search the PATH variable for a given file you would have to get value of the variable, split it by the ";" character into an array and then loop through the array of directory paths checking each directory if contains the file you search for. But you can also use a CMD one-liner to achieve that:
sFileName = "robocopy.exe"
Set oShell = WScript.CreateObject("WScript.Shell")
' this will find the file that will be actually run from command line when typed without qualified path
Set oExec = oShell.Exec("cmd /c for %G in (""" & sFileName & """) do #echo.%~$PATH:G")
' this will find all occurances of the file in directories listed in PATH
'Set oExec = oShell.Exec("cmd /c for %G in (""%path:;="" ""%"") do #if exist ""%~dpfxG\" & sFileName & """ echo %~dpfxG\" & sFileName)
Do
line = oExec.StdOut.ReadLine()
WScript.Echo line
' here you can examine the file
Loop While Not oExec.Stdout.atEndOfStream

Resources