calling functions from other vbs file 
Author Message
 calling functions from other vbs file

I want to call functions and subroutines define into a vbs
from other vbs, this is possible?

I have a lot of functionality that can be extracted, so it
is very important if I can do this with vbs files.

David



Fri, 01 Jul 2005 01:45:49 GMT  
 calling functions from other vbs file
Quote:

> I want to call functions and subroutines define into
> a vbs from other vbs, this is possible?

> I have a lot of functionality that can be extracted,
> so it is very important if I can do this with vbs files.

Hi

You should look into WSF and/or WSC files. Myself, I put
commonly used functions and subs in a WSC file.

A comparison with pros and cons beetween different methods
is found in this article:


Subject: Re: Calling one script from another.
Newsgroups: microsoft.public.scripting.wsh

Note, URL below for article above will wrap in your
newsteader, put it on one line!
http://groups.google.com/groups?selm=3D7CD272.614629F7%
40hydro.com

More about WSF/WSC in the thread below:

Subject: Implimenting a common.wsf file
http://groups.google.com/groups?th=cb544426cdee2bdc

Also, if you go the WSC route, be sure not to have this
line in the WSC file if you create a self-registering
typelib:

implements id="Behavior" type="Behavior"

If you have it, the Write method for Scriptlet.TypeLib
will fail.

--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and a ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter



Fri, 01 Jul 2005 02:36:26 GMT  
 calling functions from other vbs file
You have several ways of doing this.  All depend on "importing" code in some
sense, so they will automatically run any code which is NOT wrapped inside a
procedure.  This means that the best general technique for easy code reuse like
this from raw VBScript files is to make files which just contain procedures as
your libraries.

(1) Use a WSF file.
In a WSF, you can include an entire script like this:

<script language="vbscript" src="C:\tmp\vbscode.txt"/>

(2) Use an "include" function.
Since version 5.0, VBScript has had an ExecuteGlobal statement you can use to
dynamically execute imported code.  I have an include procedure I paste into a
script, which looks like this:

Sub Include(FilePath)
 'Given the path to a file, will return entire contents
 ' works with either ANSI or Unicode
 Dim FSO, CurrentFile
 Const ForReading = 1, TristateUseDefault = -2, _
  DoNotCreateFile = False
 Set FSO = createobject("Scripting.FileSystemObject")
 If FSO.FileExists(FilePath) Then
  If FSO.GetFile(FilePath).Size>0 Then
   Set CurrentFile = FSO.OpenTextFile(FilePath, ForReading, _
    False, TristateUseDefault)
   ExecuteGlobal(CurrentFile.ReadAll): CurrentFile.Close
  End If
 End If
End Sub

I then call it like this before starting the main part of my script:

include "C:\data\inc\vbscore.vbs"

(3) There are other techniques, such as putting the desired functions into a
Windows Script Component, but the above method is easiest and also produces fast
execution.

--
Please respond in the newsgroup so everyone may benefit.
 http://dev.remotenetworktechnology.com
 ----------
 Subscribe to Microsoft's Security Bulletins:
 http://www.microsoft.com/technet/security/bulletin/notify.asp


Quote:
> I want to call functions and subroutines define into a vbs
> from other vbs, this is possible?

> I have a lot of functionality that can be extracted, so it
> is very important if I can do this with vbs files.

> David



Fri, 01 Jul 2005 02:40:00 GMT  
 calling functions from other vbs file

Quote:
> (1) Use a WSF file.
> In a WSF, you can include an entire script like this:

> <script language="vbscript" src="C:\tmp\vbscode.txt"/>

> (2) Use an "include" function.
> Since version 5.0, VBScript has had an ExecuteGlobal statement you can use to
> dynamically execute imported code.
> (snip)

> (3) There are other techniques, such as putting the desired functions into a
> Windows Script Component, but the above method is easiest and also produces fast
> execution.

The largest downside for (1) and (2) compared to the WSC method in (3) is the
potential for namespace collisions (variable/proceduer names), so if you have a lot
of common scripts it is much safer to use a WSC, and the execution speed difference
is marginal. Another nice thing about WSC is that you don't need to think about
where the file is located. Also WSC can give you method/property drop down list and
syntax help in editors that supports typelibs (e.g. PrimalScript) :)

As David wrote "I have a lot of functionality that can be extracted", I think maybe
looking into using a WSC would be most beneficial to him.

--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and a ONLINE version of the 1328 page Scripting
Guide: http://www.microsoft.com/technet/scriptcenter



Fri, 01 Jul 2005 03:14:15 GMT  
 calling functions from other vbs file


Quote:

> > (1) Use a WSF file.
> > In a WSF, you can include an entire script like this:

> > <script language="vbscript" src="C:\tmp\vbscode.txt"/>

> > (2) Use an "include" function.
> > Since version 5.0, VBScript has had an ExecuteGlobal statement you can use
to
> > dynamically execute imported code.
> > (snip)
> The largest downside for (1) and (2) compared to the WSC method in (3) is the
> potential for namespace collisions (variable/proceduer names), so if you have
a lot
> of common scripts it is much safer to use a WSC,

Some other tradeoffs here:

- Using includes allows only one level of recursion in (1), and in (2) it allows
more but there is the potential for recursive inclusion problems.  WSCs won't
have that problem...

-  Creating WSCs can be an absolute pain in the rear for the average person.

Quote:
> and the execution speed difference
> is marginal.

- that depends.<g>  I have some function calls that need to be repeated lots of
times; I don't know what happens for those cases - but for passing data and
making a call a couple of times, yeah - you will never notice the difference.

Quote:
> Another nice thing about WSC is that you don't need to think about
> where the file is located.

That's dead on right...


Fri, 01 Jul 2005 05:46:57 GMT  
 calling functions from other vbs file

Quote:
>> Another nice thing about WSC is that you don't need to think about
>> where the file is located.

> That's dead on right...

... and when you realize you need to edit the WSC to fix something, have
fun searching.


Tue, 05 Jul 2005 05:48:22 GMT  
 calling functions from other vbs file
It's easier than looking for an ActiveX binary, though:

WScript.Echo ProgIdToServerPath("Microsoft.CmdLib")

function ProgIdToServerPath(progId)
 Dim Sh, clsId
 Set Sh = CreateObject("WScript.Shell")
 clsId = Sh.RegRead("HKCR\" & progId & "\CLSID\")
 ProgIdToServerPath = Sh.RegRead("HKCR\CLSID\" & clsId & "\ScriptletURL\")
end function

--
Please respond in the newsgroup so everyone may benefit.
 http://dev.remotenetworktechnology.com
 ----------
 Subscribe to Microsoft's Security Bulletins:
 http://www.microsoft.com/technet/security/bulletin/notify.asp


Quote:


> >> Another nice thing about WSC is that you don't need to think about
> >> where the file is located.

> > That's dead on right...

> ... and when you realize you need to edit the WSC to fix something, have
> fun searching.



Tue, 05 Jul 2005 07:29:46 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Calling a vbs-function from js in a wsf-script file

2. Calling *.vbs files to Run From Master VBS File

3. how to call vbs file from vbs file

4. Calling a .vbs file from another vbs

5. HOw to call objects from others Lib in VBscript

6. Calling JS function from VBS

7. call vbs function in ASP

8. Calling JS function from VBS

9. how call one function from vbs

10. Proper syntax for calling JavaScript function from VBS

11. newbie question (call vbs-function)

12. Call JavaScript function from VBS

 

 
Powered by phpBB® Forum Software