Categories
Archives
Authors
 
14

Using Include in vbscript

 

Since we're writing code in vbscript, the code reuse has been limited only to cut and paste it; developer while writing a new script which had sought a similar code for use in the programming process for the moment; motivated because the code was tested and gave results that were needed. This is functional but not the best way and do nothing to our productivity.

 

In other programming languages such as C++, C#, PHP and others, the code reuse is done using include but unfortunately this very useful feature not included in vbscript. The include allowing us to create code in separate files and import them into your scripts at runtime.

 

In this article we explain a way to enable the use of include in vbscript with the intention of it becoming a standard template to use when we go to reuse code.

 

The objectives covered in this solution are:

  1. The file to be included can be defined from a full path or folder path involving environment variables. If you send only a file name without its directory is searched in the current directory you are using our script.
  2. Will define a directory from which code may be included by default and act as our code repository.
  3. The template must keep track of the code files included to avoid duplication.

Now show a series of examples that the routine include should manage to be successful, assuming that the script myscript.vbs it will include:

'Include code snippet from the temporary folder.
Include "%Temp%\myscript.vbs" 

'Include code from the netlogon folder of the domain controller where we have authenticated.

Include "%LogonServer%\Netlogon\ScriptRepository\Myscript.vbs" 

'Include code from the path c:\folder.

Include "c:\folder\myscript.vbs" 

'Include code without defining file path.

Include "myscript.vbs"

 

Based on the objectives, we write the template code which was based on some code that already existed on the Internet on the topic with key additions we believe to support the proposed objectives, this template is called IncludeTemplate.vbs. Now look like the code was organized to address each goal: The objective #3 is solved in the header template to create object IncludeFilesList dictionary to store the full path of each code file that we included. Before we define the variable RepositoryFolderPath directory that points to our central repository of code and thus resolve the objective # 2. Let's see the code that shows this:

Dim RepositoryFolderPath,IncludeFilesList:Set IncludeFilesList=CreateObject("Scripting.Dictionary")

 

Then we show the code of the routine Include which is the heart of the solution:

The routine Include essentially reads the contents of the file you sent as a parameter and make it available for our use, this is accomplished using a variety of functions and objects vbscript described below:

 

ExpandEnvironmentStrings Function: This function is used to resolve file paths that contain environment variables like %temp%, and this allows us the directory path of our central repository of code can also contain environment variables. Example:%LOGONSERVER%\Netlogon\ScriptRepository.

 

GetAbsolutePathName Function: We use it to include a file in the path of our current script only send the filename as a parameter.

 

ExecuteGlobal command: With this instruction the contents of the file is included in the overall execution of our script. If the content is just a code snippet that is executed immediately, otherwise it is a definition of class is ready to be used in the creation of object instances of the same.

 

 

The template IncludeTemplate.vbs is available on our Download  Page by searching for "Template for using include in vbscript".

 

On the next page will explain an example of how to include a code snippet using this template.

 

We apologize for any errors in the syntax and semantics of the articles in the English version, these were done with our current knowledge of the English language and with the support of the site http://translate.google.com.


Pages: 1 of 3 Next Page
Posted in: MyClass, VbScript

Comentarios

Actualmente no hay comentarios, sera el primero en publicar uno.

Digite su comentario

Name (required)

Email (required)

Website

Enter the code shown above:

 
Inspired by Nina