Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - Using Includes in VBScript
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Using Includes in VBScript
By: Nilpo/Developer Shed Staff Writer
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2007-03-27

    Table of Contents:
  • Using Includes in VBScript
  • Building the basic routine.
  • Executing the external code
  • Allowing for relative or absolute path names
  • Allowing for platform-specific paths
  • Testing the subroutine

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using Includes in VBScript - Executing the external code


    (Page 3 of 6 )

    There are a couple of ways to execute code dynamically in VBS.  Basically, this means that VBS will interpret a text input and execute it as if it were hard coded into the script.  I’m only going to focus on the method that we need in this particular instance.

    ExecuteGlobal textinput

    The ExecuteGlobal statement accepts a single text string input.  The code is then executed in the global namespace.  Why have I chosen to execute it globally?  The answer is very simple.  I’m calling this method from within a subroutine and I want any contained variables or returns to be available in my script at the point where my subroutine was called.

    The Execute and ExecuteGlobal statements aren’t very well documented.  Both accept a single string input that contains one or more statements to be executed.  The ExecuteGlobal statement executes the code in the global namespace while the Execute statement executes code locally.

    All we need to do now is add an ExecuteGlobal statement to our subroutine and supply it with the contents of our strCode variable.  Our final subroutine looks like this:

    Sub Import(strFile)

       Set objFs = CreateObject("Scripting.FileSystemObject")

       Set objFile = objFs.OpenTextFile(strFile)

       strCode = objFile.ReadAll

       objFile.Close

       ExecuteGlobal strCode

    End Sub

    Well, that was simple enough.  We now have a working subroutine that can emulate includes in VBScript.  Just add your subroutine to the end of your script file and make calls like the following anywhere that you need an include.

    Import "C:myinclude.vbs"

    Myinclude.vbs can be any valid script file that you wish to use.

    Of course, if you know me at all by now, we’re not done with this code.  We need to add some flexibility to it.  We’ll do that in the next section.

    More Windows Scripting Articles
    More By Nilpo/Developer Shed Staff Writer


       · Everyone loves a good workaround. This one lets you take advantages of code...
     

    WINDOWS SCRIPTING ARTICLES

    - A Portable Scripting Toolbox
    - WPF Through an Example: Introduction
    - Beginning SharePoint Web Part Development
    - More Alternative Languages for WSH
    - WPF Control Layout
    - WSH in Other Languages
    - Screen Capturing via GDI+ and GDI
    - Understanding Procedures in VBScript
    - Printing Documents in WSH
    - Generating Outlook Signatures Based on Activ...
    - VBScript: Converting and Formatting with Fun...
    - VBScript: Conversion and Format Functions
    - VBScript: Array Functions
    - VBScript: Strings, You Can`t Function withou...
    - VBScript: More String Functions





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway