We help IT Professionals succeed at work.

How to launch files from wsh / vb script

11,424 Views
Last Modified: 2008-01-09
Hi

I have created a bat file that I use to start several scripts and sqlcmd commands, something like this:

CScript C:\MyTests\FS\Scripts\script01.vbs

sqlcmd -S SERV2K3STD\SQLEXPRESS -i C:\MyTests\FS\Scripts\sql_statement_01.sql

bcp [Mondial_LAM].[dbo].[FileTest] in C:\MyTests\FS\Scripts\some_file.txt -S SERV2K3STD\SQLEXPRESS -c -T
sqlcmd -S SERV2K3STD\SQLEXPRESS -i C:\MyTests\FS\Scripts\3sql_statement_02.sql


My question is how to create a WSH / vb script instead of bat file that would launch those files as a scheduled task. I mean, how do I create such vbs file to be able then launch it through Scheduled tasks?

Thanks!
Comment
Watch Question

Do you want to integrate that into VBScript so that you just have one nice script that you can run?

I would actually suggest converting the script01.vbs to a batch script if possible.
I was under the impression that the WHost ability to launch External programs was removed a long time ago, because of obvious reasons with vbs attachments...

that is my understanding anyway!!

BB

Author

Commented:
actually what I need is to schedule this task so that it won't open a command prompt window when run. I thought that creating and scheduling a vbs script will do.
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
>> actually what I need is to schedule this task so that it won't open a command prompt window when run.

Why?

>> I thought that creating and scheduling a vbs script will do

Yep that will indeed stop a command prompt coming up.

Author

Commented:
Well, the reason is that we already have some scripts running on this machine, plus this particular script will be set to run every several minutes. Most of the work could have be done in SQL Server Agent on standard or enterpise versions of SQL Server, but the one we use right now is Express Edition. For that reason I have to automate many tasks using script files. By the way, the script execution time is about  seconds right now and I expect it will take more in the future as my database grows.

Actually, looking at it, I suggest you move your entire script into a stored procedure, then use Windows Scheduler/VBScript to just run your stored procedure. I'm biased though because I know my way around stored procedures.

To put it another way it should be done in the language you are most comfortabe with -  VBScript or T-SQL.
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
One of the easiest ways to implement a VBS solution would be to create another VBS file, as well as this batch file, that simply calls the batch file in a hidden DOS window.

Create a RunBatch.vbs file, and paste this code:
'================
Set objShell = CreateObject("WScript.Shell")
strBatch = "C:\RunSQLScript.bat"
objShell.Run strBatch, 0, True
Set objShell = Nothing
'================

It is the zero in the objShell.Run command that runs the hidden window.

Then, when creating your sheduled task, make sure the command line it runs is
wscript C:\RunBatch.vbs

Regards,

Rob.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.