Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBS Include 1

Status
Not open for further replies.

Frink

Programmer
Mar 16, 2001
798
GB
Hallo,

Being able to 'Include' a file in VBS is useful, to allow code re-use etc.

I have read around and come up with the following as about the neatest way to do it:
Code:
ExecuteGlobal CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\My Library.vbs",1).ReadAll

I'm not overly familiar with the specifics of VBS, so can anyone tell me if there is a problem with this (or any way to improve it)?
I'm not sure if the file and file system object are correctly closed, or how to verify that they are.

- Frink
 
Most script hosts have some way of doing script includes. For WSH you simply use the .WSF script file format instead of the older naked .VBS files.

<script> Element (Windows Script Host)

This has been an option since around 1999 or 2000 when WSH 2.0 came out.
 
Have a look here:
faq329-3419
 
Hallo,

Cheers for the responses. I didn't want to use wsf, and I think that cutting the VBS include to a single line is prett neat, but I wasn't sure whether it closed all its handles properly, and wasn't sure how to test it to see if it did.

- Frink
 
>but I wasn't sure whether it closed all its handles properly,...
It does, no worry in that regard.
 
Cheers jsuji, did you run some tests to prove this, or is it just that you know that VBS tidies up after itself?
I only ask because VBA used to have problems if you didn't set objects to Nothing, but I am going back a few years now...

- Frink
 
The statement would be the leaks for this line
>ExecuteGlobal CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\My Library.vbs",1).ReadAll
and this line
[tt]wscript.exe C:\My Library.vbs[/tt]
are idempotent.
 
>VBA used to have problems if you didn't set objects to Nothing

No, it didn't. Not really. The main problem was/is circular references (which is really consequence of the design of COM rather than a specific problem with VBA). And, contrary to popular belief, simply setting an object to nothing doesn't fix that problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top