Windows Scripting
  Home arrow Windows Scripting arrow Page 5 - 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 
Mobile Linux 
App Generation ROI 
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 - Allowing for platform-specific paths


    (Page 5 of 6 )

    For some people this may be enough, but for me it isn’t.  What if I’m using this in a script that passes a path containing environmental variables?  Maybe I’m calling a script that resides in the Windows directory like this:

    Import "%windir%myscript.vbs"

    We can allow for this too.  Before the last line of code we added, we can stop and expand any environmental variables using the WSH Shell object’s ExpandEnvironmentStrings method.  Don’t forget to create an instance of the WSH Shell object in your subroutine.  Now we have this:

    Sub Import(strFile)

       Set objFs = CreateObject("Scripting.FileSystemObject")

       Set WshShell = CreatObject("Wscript.Shell")

       strFile = WshShell.ExpandEnvironmentStrings(strFile)

       strFile = objFs.GetAbsolutePathName(strFile)

       Set objFile = objFs.OpenTextFile(strFile)

       strCode = objFile.ReadAll

       objFile.Close

       ExecuteGlobal strCode

    End Sub

    Presto!  Just like that our subroutine now works with paths containing environmental strings as well.  Now I have one more thing to take into consideration.  What if I’m using the strFile variable somewhere else in my code and I don’t want it to change?

    The way this subroutine is written now, the strFile variable will contained the fully exploded full path to my file when the subroutine finishes.  I can prevent this from happening very simply by using the ByVal statement when creating my subroutine.

    The ByVal statement will take the strFile attribute (if provided as a variable) by its value.  This means that it will not alter the variable’s contents outside of my subroutine’s namespace.

    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

    We now have a fully customized subroutine that will allow us to use includes in our script.  Let’s put it to the test.

    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

    - Introducing Two-Way Data Binding using Silve...
    - Silverlight 2.0 Application Development with...
    - Burning Multisession CDs with IMAPI2 in WSH
    - Creating a Silverlight 2.0 Application that ...
    - Burning CDs with the IMAPI2 Control
    - Burning CDs in Windows XP with WSH
    - Advanced Word Object Scripting
    - Reading and Printing Word Documents in WSH
    - Scripting Microsoft Word
    - Using WSH to Catalog MP3 Files
    - Reading MP3 ID3 Tags in WSH
    - A Brief Look at Menus in WPF
    - More Examples of Simplified Image Processing...
    - Completing a WPF To-Do List Application
    - Simplified Image Processing in GDI+





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT