
Running a .vbs script from within a .vbs script
Quote:
> Could someone please help me!! I am trying to run a .vbs
> cript from within another script and the script doesn't
> seem to recognise it. I have tried ws.run and call but
> neither work... Please could someone help me out on this
> one
Post your code, it is easier to help then :)
Anyway, it should work to call another VBScript using the Run method. The
downside is that they cannot share code and variables (variables can of course
be "transferred" using command line parameters or saving then in registry/in a
file).
If you want to share code and variables, there are several ways of doing this.
The different methods all have their pros and cons. Here is a quick overview:
1) Read the second script into a textstream and run ExecuteGlobal or Execute on
it.
Pros:
Can share all variables, functions and subs.
Easy to implement.
Cons:
If you have an error in the included script, you will not get the line number
where the error arised
2) Use a Windows Script Host File (.WSF) file and include scripts with script
tag.
Pros:
Can share all variables, functions and subs.
Easy to run both VBScript and JavaScript code together.
If you have an error in a included script, you will get the line number where
the error arised.
Easy to implement.
3) Use a Windows Script Component (.WSC).
Pros
You will get a COM interface to your script library (that also can be used by
other programs)
Method/property drop down list and syntax help in editors that supports typelibs
Ideal for development environment with more than one developer
Cons
The most "complicated" solution
***********************************************************
Here are some more details for each method:
___________________________________________________________
1) Read the second script into a textstream and run ExecuteGlobal or Execute on
it.
Load the 2. script into the 1. script at runtime (on the fly) and execute it
with ExecuteGlobal. They can then also share variables, functions etc. The sub
below does this loading:
Sub Include(sInstFile)
Dim oFSO, f, s
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set f = oFSO.OpenTextFile(sInstFile)
s = f.ReadAll
f.Close
ExecuteGlobal s
End Sub
___________________________________________________________
2) Use a Windows Script Host File (.WSF) file and include scripts with script
tag.
Using Windows Script Files (.wsf)
http://msdn.microsoft.com/library/en-us/script56/html/wsAdvantagesOfW...
An example:
<?xml version="1.0" ?>
<package>
<job>
' this will run "file1.vbs"
<script language="VBScript" src="file1.vbs" />
' this will run "file2.vbs"
<script language="VBScript" src="file2.vbs" />
<script language="VBScript">
'
' Some script code here if you want ...
'
' You can use variables, subs and functions
' from the included files.
'
</script>
</job>
</package>
___________________________________________________________
3) Use a Windows Script Component (.WSC).
Use a Windows Script Component (.WSC) to get a COM interface for your VBSscript
"library".
You can eg. use Windows Script Component Wizard to create
one. Take a look here for more info:
http://msdn.microsoft.com/library/default.asp?url=/nhp/Default.asp?co...
Also, using an editor like PrimalScript (http://www.primalscript.com/ ) it is
easy to create/maintain a .wsc file (and you will get drop down list and syntax
help on the fly on your .WSC components from this editor editor if you create a
typelib from your .WSC file)
___________________________________________________________
You can also take a look at this newsgroup thread, it has a discussion about
this:
Subject: Call a subroutine located in another .vbs file
Newsgroups: microsoft.public.scripting.vbscript
Date: 2000/04/27
You can use http://groups.google.com/advanced_group_search to fetch it :-)
--
torgeir