Simple question, as I build up my little library of some useful functions I
want to keep all of these in a "myfuncs.vbs" script. Then when I write a
quick new script I just want to be able to call funtions located in
"myfuncs.vbs". How do I do this?

Thanks for your help in advance!

Regards,
Craig.

Re: How do I include/reference functions in another file? by James

James
Thu Jul 21 12:02:02 CDT 2005

"Craig Bender" <cbender@pxpert.com> wrote in message
news:uCLVEAhjFHA.2852@TK2MSFTNGP14.phx.gbl...
> Simple question, as I build up my little library of some useful functions
I
> want to keep all of these in a "myfuncs.vbs" script. Then when I write a
> quick new script I just want to be able to call funtions located in
> "myfuncs.vbs". How do I do this?
>
> Thanks for your help in advance!

To the best of my knowledge, the only way to do this is to use a Windows
Script File (*.wsf). Something like this:

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<job>
<script language="VBScript" src="c:\MyFunc.vbs"/>
<script language="VBScript">

SomeFunction("some parameter")

</script>
</job>
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Windows Script Files have several advantages over standard VBScript
(*.vbs) files:

http://msdn.microsoft.com/library/en-us/script56/html/wsAdvantagesOfWs.asp



Re: How do I include/reference functions in another file? by Torgeir

Torgeir
Thu Jul 21 12:01:10 CDT 2005

Craig Bender wrote:

> Simple question, as I build up my little library of some useful functions I
> want to keep all of these in a "myfuncs.vbs" script. Then when I write a
> quick new script I just want to be able to call funtions located in
> "myfuncs.vbs". How do I do this?
>
> Thanks for your help in advance!
Hi,

For different methods and some examples of code libraries and their
pros and cons, you can take a look here:

http://groups.google.co.uk/group/microsoft.public.scripting.vbscript/msg/6898681c85413286?dmode=source&hl=en


Some interesting WSC links for future use maybe:

Windows Scripting Components - An Introduction
http://www.aspfree.com/c/a/Windows-Scripting/Windows-Scripting-Components--An-Introduction/



How to generate a WSC type library file automatically:
http://groups.google.co.uk/groups?selm=3EBD6A07.C56E6B71%40hydro.com

Optional methods to reference the WSC that is not registered from
a script:
http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=uKQPMyT1BHA.2736%40tkmsftngp03


--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx

Re: How do I include/reference functions in another file? by Craig

Craig
Fri Jul 22 02:12:12 CDT 2005

I can't believe this is so complicated! Even with my junior program skills
I know this a basic necessity to be able code, otherwise you are constantly
coping and pasting your needed functions back and forth. In perl and I
believe java and most other languages this is just a one liner to Include
another file at the top of your script. And as far as the worry of
duplicate variable names I would assume you could just state Private for
each sub and funtion?

I guess out of all your suggestions, example #1 is the way I would go.
Suggestion #3 is seems way to complicated and I'm not sure it gives me the
flexibility of just drafting a quick script and including myfunc script with
it. And suggestion #2 also doesn't seem flexible. I think it would be
perfect if I can have a "myfuncs.wsc" and then just somehow reference that
in my VBS scripts. As I want to be able to write a VBS script and just
reference a file so that I can call functions that are held in that file.

One last example being I have a function called WriteLog(), which I copy and
paste everything I write a new script. Instead I should be able to just
copy and paste my header (that references/includes this myfuncs script) and
be able to just call WriteLog in my script.

Thanks,
Craig.


"Torgeir Bakken (MVP)" <Torgeir.Bakken-spam@hydro.com> wrote in message
news:uA7nwahjFHA.3540@TK2MSFTNGP14.phx.gbl...
> Craig Bender wrote:
>
>> Simple question, as I build up my little library of some useful functions
>> I want to keep all of these in a "myfuncs.vbs" script. Then when I write
>> a quick new script I just want to be able to call funtions located in
>> "myfuncs.vbs". How do I do this?
>>
>> Thanks for your help in advance!
> Hi,
>
> For different methods and some examples of code libraries and their
> pros and cons, you can take a look here:
>
> http://groups.google.co.uk/group/microsoft.public.scripting.vbscript/msg/6898681c85413286?dmode=source&hl=en
>
>
> Some interesting WSC links for future use maybe:
>
> Windows Scripting Components - An Introduction
> http://www.aspfree.com/c/a/Windows-Scripting/Windows-Scripting-Components--An-Introduction/
>
>
>
> How to generate a WSC type library file automatically:
> http://groups.google.co.uk/groups?selm=3EBD6A07.C56E6B71%40hydro.com
>
> Optional methods to reference the WSC that is not registered from
> a script:
> http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=uKQPMyT1BHA.2736%40tkmsftngp03
>
>
> --
> torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
> Administration scripting examples and an ONLINE version of
> the 1328 page Scripting Guide:
> http://www.microsoft.com/technet/scriptcenter/default.mspx



Re: How do I include/reference functions in another file? by Al

Al
Sun Jul 24 11:36:07 CDT 2005


"Craig Bender" <cbender@pxpert.com> wrote in message
news:e%237QyyojFHA.4000@TK2MSFTNGP12.phx.gbl...
> I can't believe this is so complicated!

Believe it.

> Even with my junior program skills
> I know this a basic necessity to be able code, otherwise you are
constantly
> coping and pasting your needed functions back and forth. In perl and I
> believe java and most other languages this is just a one liner to Include
> another file at the top of your script.

Each language and platform has its own approaches - expecting them all to be
of the same flavour will lead only to frustration.

> And as far as the worry of
> duplicate variable names I would assume you could just state Private for
> each sub and funtion?

"Private" variables are only private within classes. Regardless, I use
"private" to declare a global variable that needs to be persistent, and my
naming convention is that its name starts with the name of the function it
"belongs to:.

> I guess out of all your suggestions, example #1 is the way I would go.

I disagree, but ymmv. Example 1 requires each script to have its own copy of
an "include" function - there's that cutting and pasting that neither of us
likes. It also requires your script to manage the location of the library
files, and to include some sort of generic exception handling in the event
the files are not accessible. Finally, the cons listed are those that make
it difficult to fully debug library routines.

> Suggestion #3 is seems way to complicated and I'm not sure it gives me the
> flexibility of just drafting a quick script and including myfunc script
with
> it.

There I tend to agree with you, but there are others here who swear by (not
at) it. ;-)

> And suggestion #2 also doesn't seem flexible.

That depends on how you work it. I find it "flexible" enough that I use it
(almost) all of the time. Having a reasonably well debugged set of ready to
use library routines results in less adhoc scripting and a more consistently
structured approach. It's not perfect, mind, and there are changes I would
like (but do not expect) to see.

It also helps to work with a .WSF-aware scripting editor (like PrimalScript)
so that one need not spend much time dealing with the XML details.

> I think it would be
> perfect if I can have a "myfuncs.wsc" and then just somehow reference that
> in my VBS scripts. As I want to be able to write a VBS script and just
> reference a file so that I can call functions that are held in that file.

Hey, I'd like it if vbs/wsh were converted to a compiled language complete
with a function library functionality, but it ain't gonna happen.

But I think that the .wsf format comes the closest to allowing you to write
code that is ONLY associated with the CALLING of library functions, and NOT
the management of the library itself.

> One last example being I have a function called WriteLog(), which I copy
and
> paste everything I write a new script. Instead I should be able to just
> copy and paste my header (that references/includes this myfuncs script)
and
> be able to just call WriteLog in my script.

You are pre-supposing the best solution to be one that is not available,
even though it is in other platforms. .WSF does this for me, however, I
don't cut-and-paste anything - I just start a new project and include the
library files I need with script tags once, and forget it. Even easier, when
editing the code itself, I do not even see references to these files in
headers in the code - they are "hidden" in the XML code.

/Al


>
> Thanks,
> Craig.
>
>
> "Torgeir Bakken (MVP)" <Torgeir.Bakken-spam@hydro.com> wrote in message
> news:uA7nwahjFHA.3540@TK2MSFTNGP14.phx.gbl...
> > Craig Bender wrote:
> >
> >> Simple question, as I build up my little library of some useful
functions
> >> I want to keep all of these in a "myfuncs.vbs" script. Then when I
write
> >> a quick new script I just want to be able to call funtions located in
> >> "myfuncs.vbs". How do I do this?
> >>
> >> Thanks for your help in advance!
> > Hi,
> >
> > For different methods and some examples of code libraries and their
> > pros and cons, you can take a look here:
> >
> >
http://groups.google.co.uk/group/microsoft.public.scripting.vbscript/msg/6898681c85413286?dmode=source&hl=en
> >
> >
> > Some interesting WSC links for future use maybe:
> >
> > Windows Scripting Components - An Introduction
> >
http://www.aspfree.com/c/a/Windows-Scripting/Windows-Scripting-Components--An-Introduction/
> >
> >
> >
> > How to generate a WSC type library file automatically:
> > http://groups.google.co.uk/groups?selm=3EBD6A07.C56E6B71%40hydro.com
> >
> > Optional methods to reference the WSC that is not registered from
> > a script:
> >
http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=uKQPMyT1BHA.2736%40tkmsftngp03
> >
> >
> > --
> > torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
> > Administration scripting examples and an ONLINE version of
> > the 1328 page Scripting Guide:
> > http://www.microsoft.com/technet/scriptcenter/default.mspx
>
>



Re: How do I include/reference functions in another file? by mayayana

mayayana
Sun Jul 24 17:35:21 CDT 2005

> I can't believe this is so complicated!

You can just write classes and then either paste
them at the end of your script or call them by using
the ExecuteGlobal method that Torgeir linked to.

That's my preferred method. It seems fairly simple and
easy to keep organized. A class has its own
encapsulated scope, so if you write a big script that
contains only a single, large class, then include that
by using a short sub that reads the file and calls
ExecuteGlobal, then you essentially have a custom
object that you can create in 4 or 5 lines of code
(The reading-in sub).

Personally, I think the whole WSC thing is a case
of too many MS programmers with too much time
on their hands. A text-based, registered COM object
is a ridiculous thing that's just asking for trouble (if files
get moved). All that's doing is keeping your record
of the file path in the Registry - and forcing you to
keep the same path so that COM can find your object
when you call it. And it somewhat defeats the whole
advantage of script being easy to work with and edit.
Not only is your fake COM object now treated as a
compiled object (with none of the benefits of compiling)
but it also gets saddled with a lot of superfluous XML
that serves little purpose beyond that of "fashion
accessory". (How often does one really need VBS and
JS combined, after all?)

But it's all really just a matter of personal preference.

--
--
Craig Bender <cbender@pxpert.com> wrote in message
news:e#7QyyojFHA.4000@TK2MSFTNGP12.phx.gbl...
> I can't believe this is so complicated! Even with my junior program
skills
> I know this a basic necessity to be able code, otherwise you are
constantly
> coping and pasting your needed functions back and forth. In perl and I
> believe java and most other languages this is just a one liner to Include
> another file at the top of your script. And as far as the worry of
> duplicate variable names I would assume you could just state Private for
> each sub and funtion?
>
> I guess out of all your suggestions, example #1 is the way I would go.
> Suggestion #3 is seems way to complicated and I'm not sure it gives me the
> flexibility of just drafting a quick script and including myfunc script
with
> it. And suggestion #2 also doesn't seem flexible. I think it would be
> perfect if I can have a "myfuncs.wsc" and then just somehow reference that
> in my VBS scripts. As I want to be able to write a VBS script and just
> reference a file so that I can call functions that are held in that file.
>
> One last example being I have a function called WriteLog(), which I copy
and
> paste everything I write a new script. Instead I should be able to just
> copy and paste my header (that references/includes this myfuncs script)
and
> be able to just call WriteLog in my script.
>
> Thanks,
> Craig.
>
>
> "Torgeir Bakken (MVP)" <Torgeir.Bakken-spam@hydro.com> wrote in message
> news:uA7nwahjFHA.3540@TK2MSFTNGP14.phx.gbl...
> > Craig Bender wrote:
> >
> >> Simple question, as I build up my little library of some useful
functions
> >> I want to keep all of these in a "myfuncs.vbs" script. Then when I
write
> >> a quick new script I just want to be able to call funtions located in
> >> "myfuncs.vbs". How do I do this?
> >>
> >> Thanks for your help in advance!
> > Hi,
> >
> > For different methods and some examples of code libraries and their
> > pros and cons, you can take a look here:
> >
> >
http://groups.google.co.uk/group/microsoft.public.scripting.vbscript/msg/689
8681c85413286?dmode=source&hl=en
> >
> >
> > Some interesting WSC links for future use maybe:
> >
> > Windows Scripting Components - An Introduction
> >
http://www.aspfree.com/c/a/Windows-Scripting/Windows-Scripting-Components--A
n-Introduction/
> >
> >
> >
> > How to generate a WSC type library file automatically:
> > http://groups.google.co.uk/groups?selm=3EBD6A07.C56E6B71%40hydro.com
> >
> > Optional methods to reference the WSC that is not registered from
> > a script:
> >
http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=uKQPMy
T1BHA.2736%40tkmsftngp03
> >
> >
> > --
> > torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
> > Administration scripting examples and an ONLINE version of
> > the 1328 page Scripting Guide:
> > http://www.microsoft.com/technet/scriptcenter/default.mspx
>
>