Can I call a VBscript from another VBscript?

Discussion in 'Scripting' started by Wesley, May 17, 2004.

  1. Wesley

    Wesley Guest

    I just graduated from writing a "hello world", to reading an input file and writing an output file. I can see where I might want to build a library of callable .vbs files instead of defining them all as subprocedures or functions inline. Is there a way of doing that or is there an include statement or something

    I left programming in 1993 and re-entered it in 2003. Still trying to decipher all this new jargon that appears to be based on the object oriented techniques that I never had. I suppose I have to create them as a library of objects or methods or something?
     
    Wesley, May 17, 2004
    #1
    1. Advertisements

  2. Wesley

    Operations Guest

    Hello Wesley. Yes, you can. The exact method depends on the host for your
    script; for instance, from asp 3.0 you'd call
    Server.Execute("vbscript.asp"), or include file="vbscript.asp", asumming
    vbscript.asp is your reusable code; from WSH you could create a WSF file
    with your script listed as an inclusion, and for both you could take
    advantage of Windows Script Components, a mechanism that allows you to
    encapsulate your scripts as COM objects (I personally found this very
    convenient).
    Well, I hope that answers your question, and good luck with your objects!


    and writing an output file. I can see where I might want to build a library
    of callable .vbs files instead of defining them all as subprocedures or
    functions inline. Is there a way of doing that or is there an include
    statement or something?
    decipher all this new jargon that appears to be based on the object oriented
    techniques that I never had. I suppose I have to create them as a library
    of objects or methods or something?
     
    Operations, May 17, 2004
    #2
    1. Advertisements

  3. That is a static include, and probably the best way to deal with what you
    consider library routines. A slightly different method involves writing an
    "include" function that reads in a vbscript and executes it.

    /Al
     
    Al Dunbar [MS-MVP], May 18, 2004
    #3
  4. copy of VBscript Programmers Reference and had found COM objects but I still
    have to read chapter 5 to get up to speed on the concepts.
    look like? Or would prog1 be a .wsf extension instead of a .vbs.
    VM or MVS mainframe. I know REXX exists for Windows systems but not in this
    shop.

    You are welcome.

    I don't use REXX so I cannot comment on the relative complexity. There
    certainly *are* some common scripting techniques that do not have a native
    counterpart in WSH. The main one is the ability (or lack thereof) for one
    script in one file to simply "call" another script in another file.

    The main issue with this is that if you are used to doing this, whether in
    batch, kixtart, or whatever, it may seem like a big loss. This is more so if
    you think of your script files as containing chunks or sequences of
    otherwise unrelated code to be executed in the context of the calling
    script.

    WSH tends to allow you to treat code more like objects, which can take a
    while for a batch scripter to catch on to (and just wait until longhorn
    comes along with its object-oriented update to batch, called monad). But I
    digress...

    Now to answer the questions in your second paragraph... it depends. One of
    the main benefits of the .WSF format is that you can maintain a single copy
    of your "library routines" in one or more .vbs (or .js) files, and
    statically "include" them into any number of .wsf scripts that need them.
    This is miles better than cutting and pasting individual routines into a
    series of separate .vbs scripts. Just think of how much easier it will be to
    provide each separate script with the new and improved version of one of
    your functions - by editing the only copy!

    For all but the simplest scripts I recommend .wsf over .vbs. Of course, the
    XML syntax can be a bother, but that's what script editors such as
    primalscript are for. I use .wsf scripts almost exclusively, but it has been
    a long time since I actually saw the XML code.

    I think I might have also mentioned the generic INCLUDE function. It is not
    build in to vbscript, but must be written for (and included in) any script
    that requires it. Each of us uses our own version, but the basic idea is
    that it is passed the name of a .vbs file, reads the file in, and then
    executes it, typically with the EXECUTEGLOBAL statement.

    It is possible for the included code to be open ended script, written so as
    to use variables defined in the code that calls it and to be "includable"
    whenever needed. A better approach is to have the included script contain
    only definitions, whether of subs, functions, global variables, or global
    constants; to include it only once, and then to call the subs or reference
    the functions it has defined. This is a more efficient approach if the
    routine/script needs to be called many times, and is generally easier to get
    the code right. Both methods have problems, though, in that syntactical and
    other errors are not displayed showing the line number.

    Finally, you can create external COM objects using windows script
    components. These can be referenced in your script code without having to
    write a specialized handler function like include.


    /Al
     
    Al Dunbar [MS-MVP], May 20, 2004
    #4
  5. Hi

    For different methods and some examples of code libraries and their pros and
    cons, you can take a look here (my favorite is WSC):

    http://groups.google.com/groups?selm=


    Some interesting WSC links for future use maybe:

    How to generate a WSC type library file automatically:
    http://groups.google.com/groups?selm=

    Optional methods to reference the WSC that is not registered from a script:
    http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&[email protected]
     
    Torgeir Bakken \(MVP\), May 26, 2004
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.