Solved

Problem Calling Bat File from VBS Script

Posted on 2014-11-18
21
827 Views
Last Modified: 2014-11-18
I am trying to use a .vbs file to run a .bat file using the following:

"Set WshShell = WScript.CreateObject("WScript.Shell")
WSHShell.run "\\servername\folder\subfolder\batfile.bat"

but when I do it always tells me the following:

Line: 2
Char: 1
Error: The system cannot find the file specified
Code: 80070002
Source: (null)"

The location specified is definitely correct. If I change the script and location of the bat file to a local drive it runs without issue. I have no idea what is wrong and I'm not familiar with writing scripts. Any help would greatly be appreciated.
0
Comment
Question by:Rob Sanders
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 6
  • 3
  • +2
21 Comments
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40450819
does the account that the vbs script is running in have access to the server and path?
Can you execute  \\servername\folder\subfolder\batfile.bat  directly from a cmd prompt on the computer that fails to run the vbscript?

Is this called by a user or is it maybe in a GPO?
0
 
LVL 56

Expert Comment

by:HainKurt
ID: 40450824
try

Set WshShell = WScript.CreateObject("WScript.Shell")
dim myfile = "\\servername\folder\subfolder\batfile.bat"
WSHShell.run myfile
0
 
LVL 56

Expert Comment

by:HainKurt
ID: 40450828
or maybe

Set WshShell = WScript.CreateObject("WScript.Shell")
WSHShell.run "\\\\servername\\folder\\subfolder\\batfile.bat"
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Rob Sanders
ID: 40450854
@NeilSr I can run the bat file manually as the user. Right now that is all I am trying to do before I attempt to run it from a GPO.

@HainKurt After implementing your change it is now saying the following when I run the .vbs file:

Line: 2
Char: 2
Error: The network path was not found.
Code: 80070035
Source: (null)
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40450860
May I ask why you need to run both a vbscript AND a batch file at the same time? Can you not just have a script that does it all OR have more than one login script listed in the GPO?
0
 
LVL 1

Author Comment

by:Rob Sanders
ID: 40450874
In this environment when we attempt to run a bat file directly upon logon the user receives a Windows security prompt asking if they want to run the file. I have previously worked around this issue by calling the .bat file from a .vbs file. That is what I would like to do here.
0
 
LVL 56

Expert Comment

by:HainKurt
ID: 40450921
i am just guessing :)

maybe you have something in bat file which creates that message!!!

and you have this exactly in your vbs file

Set WshShell = WScript.CreateObject("WScript.Shell")
WSHShell.run "\\servername\folder\subfolder\batfile.bat"

Open in new window


in your original post, you have " in front
0
 
LVL 56

Expert Comment

by:HainKurt
ID: 40450953
any log in event viewer?
0
 
LVL 1

Author Comment

by:Rob Sanders
ID: 40450967
The " in front of the code is not in the actual .vbs file. The application and system event viewer logs do not show anything.

I supposed it is possible that something in the .bat file is causing the message, but again, they work when running them locally. The problem only happens when trying to run the script from a UNC or mapped drive path.
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 40450976
I did a test of your original post and it worked fine here, so I don't think it's a VBS problem per se.  I suspect it's privilege related, etc.

~bp
0
 
LVL 1

Author Comment

by:Rob Sanders
ID: 40451052
I logged on to the workstation as a domain administrator and ran the .vbs file and had the same problem just to make sure there wasn't a permissions issue.
0
 
LVL 56

Expert Comment

by:HainKurt
ID: 40451073
any log in event viewer?
how do you run this vbs? double click, from cmd prompt?
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 40451101
Are there spaces in your path?  If so you I think you will need off hand:

Set WshShell = WScript.CreateObject("WScript.Shell")
WSHShell.run """\\server\share\my path\my file.cmd"""

Open in new window


Steve
0
 
LVL 1

Author Comment

by:Rob Sanders
ID: 40451180
@HainKurt There was nothing in event viewer.

@Steve Knight Yes, there are spaces in the path. I tried adding the three sets of quotes around the path but it made no difference.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40451216
OK.  can you simplify.

Make a short batch file,

@echo off
echo Running batch file %~f0
pause

Open in new window


Save that as \\server\share\batch1.cmd , i.e. short filename, no spaces in easy location on share.

Then try JUST this in a VBScript:

Set WshShell = WScript.CreateObject("WScript.Shell")
WSHShell.run """\\server\share\batch1.cmd"""

Open in new window


Start cmd.exe prompt and:
cscript yourtest.vbs
wscript yourtest.vbs

Any different?  If that works try a file with space in the path or filename.  If not how about if you run a local .cmd file.

Having just tested it like I thought earlier, I get specifically the error you mention when running a file with spaces in the name and they go away including the extra " at the beginning and end?

Steve
0
 
LVL 1

Author Comment

by:Rob Sanders
ID: 40451221
With the simplified bat file I still encounter the same issue.
0
 
LVL 1

Author Comment

by:Rob Sanders
ID: 40451223
wait, hold on. I didn't read your entire post. Let me try everything and get back with you.
0
 
LVL 1

Author Comment

by:Rob Sanders
ID: 40451262
Set WshShell = WScript.CreateObject("WScript.Shell")
 WSHShell.run """\\servername\folder\sub folder with spaces\batfile.bat"""

I guess in all the different things I tried, I never tried this specific combination.
0
 
LVL 1

Author Comment

by:Rob Sanders
ID: 40451266
Whoops, meant to mention in the above post that I just made is that was the code that finally worked and we got this resolved now.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40451285
No problem... if you imagine what you type in the Wshshell.run command is what you would have to put in, say, the START command, e.g.

START \\servername\folder\sub folder\batch file.cmd

Open in new window


then it will run \\servername\folder\sub

so you would have to run it with quotes to run the whole correct path:

START "TITLE" "\\servername\folder\sub folder\batch file.cmd"

Open in new window


You have to add the title to START as the first item in " " is the title so without it the batch file does not run.

The first " starts the string, the next one escapes the one after it to include in the string which is why you end up with 3.

Steve
0
 
LVL 56

Expert Comment

by:HainKurt
ID: 40451510
if you put your question like

WSHShell.run "\\servername\some folder\sub folder\batfile.bat"

or

WSHShell.run "\\SRV001273X\User Files\Test Files\batfile.bat"

or

just post the actual path, then you would get the answer immediately :)
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question