Discussion:
[WScript] Import/include VBS files from within a VBS file?
(too old to reply)
JJ
2015-01-28 20:58:12 UTC
Permalink
Similar like C language's header files via #include.

The only thing I could think of is to merge the VBS library files with the
main VBS file at runtime into a temporary VBS file, then run it. Of course,
there would be codes to check whether the VBS file was executed with the
merged libraries or not, in order to avoid infinite loop.

I want to know if there's a better way to do it without using WSF files or
writing into the file system.
Evertjan.
2015-01-28 21:30:27 UTC
Permalink
Post by JJ
Similar like C language's header files via #include.
We don't know about that here.
Post by JJ
The only thing I could think of is to merge the VBS library files with the
main VBS file at runtime into a temporary VBS file, then run it. Of course,
there would be codes to check whether the VBS file was executed with the
merged libraries or not, in order to avoid infinite loop.
I want to know if there's a better way to do it without using WSF files or
writing into the file system.
To do what?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Mayayana
2015-01-28 22:57:27 UTC
Permalink
You have two options, for HTML or VBS. For
HTML (like HTAs) you can just add a script tag
for the file:

<SCRIPT SRC="extracode.vbs"> </SCRIPT>

For VBS you can use the following routine that
uses ExecuteGlobal. Note, however, that this
will add the file's code to the global scope of
the script. I use it a lot for adding large classes,
but if you're just going to add in various routines
you'll need to be careful about variable and function
names. I typically do something like write a class
and then add something like _c1 to the end of all
variable names. That makes for a very ugly class,
but it avoids problems. I never use _ for *anything*
normally, so it's a handy way to make unique private
class variables. (As you probably know, any code in
the class will see any global variables in the parent
script.)

If you really just want typical includes, like a long list
of your personal, custom VBS constants, then this will
also be a good way to do that.

Sub AddCode(spath) 'path of .vbs file to add.
Dim fso, s, f
Set fso = CreateObject("Scripting.FileSystemObject")
if fso.FileExists(spath) = false then
exit sub
end if
set f = fso.OpenTextFile(spath, 1, false)
s = f.ReadAll
f.Close
set f = nothing
ExecuteGlobal s
End Sub
JJ
2015-01-29 03:37:48 UTC
Permalink
Post by Mayayana
You have two options, for HTML or VBS. For
HTML (like HTAs) you can just add a script tag
<SCRIPT SRC="extracode.vbs"> </SCRIPT>
For VBS you can use the following routine that
uses ExecuteGlobal. Note, however, that this
will add the file's code to the global scope of
the script. I use it a lot for adding large classes,
but if you're just going to add in various routines
you'll need to be careful about variable and function
names. I typically do something like write a class
and then add something like _c1 to the end of all
variable names. That makes for a very ugly class,
but it avoids problems. I never use _ for *anything*
normally, so it's a handy way to make unique private
class variables. (As you probably know, any code in
the class will see any global variables in the parent
script.)
If you really just want typical includes, like a long list
of your personal, custom VBS constants, then this will
also be a good way to do that.
Sub AddCode(spath) 'path of .vbs file to add.
Dim fso, s, f
Set fso = CreateObject("Scripting.FileSystemObject")
if fso.FileExists(spath) = false then
exit sub
end if
set f = fso.OpenTextFile(spath, 1, false)
s = f.ReadAll
f.Close
set f = nothing
ExecuteGlobal s
End Sub
ExecuteGlobal will do just fine. Seems that I overlooked it.

And yes, the VBS library files would be my own code.I already aware of
possible conflict when adding new declaration in the global context.

Thank you.

Loading...