Windows Scripting
  Home arrow Windows Scripting arrow Page 6 - 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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 - Testing the subroutine


    (Page 6 of 6 )

    We’re going to create two files.  The first one, called test.vbs, will be our test script and the second one, called include.vbs, will contain our include code.  The first should look like this:

    ' test.vbs

     

    Import "include.vbs"

     

    Sub Import(ByVal strFile)

       Set objFs = CreateObject("Scripting.FileSystemObject")

       Set WshShell = CreateObject("WScript.Shell")

       strFile = WshShell.ExpandEnvironmentStrings(strFile)

       file = objFs.GetAbsolutePathName(strFile)

       Set objFile = objFs.OpenTextFile(strFile)

       strCode = objFile.ReadAll

       objFile.Close

       ExecuteGlobal(strCode)

    End Sub

    Our include file is going to be very simple.  We’ll just use the Wscript Echo method to create a “Hello World” script.

    ' include.vbs

     

    Wscript.Echo "Hello World!"

    Save both files to the same folder and double click test.vbs.  Just like magic, you should get a VBScript message box saying “Hello World!”.

    There you have it.  A simple subroutine that you can reuse in any of your scripts to add file include capabilities to VBScript.  I like to use this to include reusable classes.  This way, if I make updates to those classes I can easily update all of my other scripts as well without having to update each one individually.

    I hope that you have found this solution as helpful as I did.  Find ways to customize it or expand upon it to make it fit your own needs.  That’s all for me this time.  Until next time, keep coding!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Everyone loves a good workaround. This one lets you take advantages of code...
       · Thank you so much for posting your code. This is EXACTLY what I was looking for....
       · Thank you so much for you kind words. I'm glad you found this code useful.
     

    WINDOWS SCRIPTING ARTICLES

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek