Windows Scripting
  Home arrow Windows Scripting arrow Page 4 - 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 - Allowing for relative or absolute path names


    (Page 4 of 6 )

    First things first, I want this code to work with either absolute or relative file paths.  To do that, we’re going to add a single line before trying to open the input file.

    Sub Import(strFile)

       Set objFs = CreateObject("Scripting.FileSystemObject")

       strFile = objFs.GetAbsolutePathName(strFile)

       Set objFile = objFs.OpenTextFile(strFile)

       strCode = objFile.ReadAll

       objFile.Close

       ExecuteGlobal strCode

    End Sub

    In this line, I use the FileSystemObject’s GetAbsolutePathName to reassign the strFile variable with a full path to the file to be included.  What’s nice is how the GetAbsolutePathName works in this particular case.

    If you are using a script editor with a debugger to test this script, it may fail at this point.  This is because most debuggers will not execute your code from the directory where your script resides.  If this happens, try hard coding a path while testing your script.

    The GetAbsolutePathName method will return a text string containing a full file path.  If a full path is not supplied to it, it will create one by assuming the partial path is relative to the script’s execution directory.  Here’s what that means.

    If we supply only a filename, it will assume the file resides in the same directory as our script and return a full path to that file.  If we supply something like “includesmyinclude.vbs”, it will assume that the “includes” directory resides in the same directory as our script and return a full path accordingly.

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


       · 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 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek