Werbung: SecurityConsole.de verwaltet Ihre Computer mit Security Essentails aus der Cloud!
30 Tage kostenfrei testen und 20% Rabatt für Ihre Bestellung mit Promocode: WBF2685582
(Promocode gültig bis 31.12.2011)

Group:  English: Windows Server » microsoft.public.windows.server.scripting
Thread: Combining Multiple VB Scripts

HTVi
TV Discussion Newsgroups

Combining Multiple VB Scripts
JCarbuccia 5/9/2007 8:24:01 PM
I'm new in the scripting business. I've found the scripts I need for my
environment but they are all separate files, I need to combine all 5 files
into one large vbs file. Here's a sample of two files I need to combine into
one. Can someone please help.

Thank you,

Script 1:

' Already.vbs Windows Logon Script
' VBScript to map a network drive.
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.7 - April 24th 2005
' ------------------------------------------------------'
Option Explicit
Dim strDriveLetter, strRemotePath
Dim objNetwork, objShell
Dim CheckDrive, AlreadyConnected, intDrive
' The section sets the variables.
strDriveLetter = "P:"
strRemotePath = "\\server\staff"

' This sections creates two objects:
' objShell and objNetwork and counts the drives
Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set CheckDrive = objNetwork.EnumNetworkDrives()

' This section deals with a For ... Next loop
' See how it compares the enumerated drive letters
' with strDriveLetter
On Error Resume Next
AlreadyConnected = False
For intDrive = 0 To CheckDrive.Count - 1 Step 2
If CheckDrive.Item(intDrive) =strDriveLetter _
Then AlreadyConnected =True
Next

' This section uses the If = then, else logic
' This tests to see if the Drive is already mapped.
' If yes then disconnects
If AlreadyConnected = True then
objNetwork.RemoveNetworkDrive strDriveLetter
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath

' The first message box
objShell.PopUp "Drive " & strDriveLetter & _
"Disconnected, then connected successfully."
Else
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
objShell.PopUp "Drive " & strDriveLetter & _
" connected successfully." End if
WScript.Quit

' Guy's Script ends here

Script 2:

' TwoMap.vbs - Map Network Drive to P: and S:
' Example of VBScript Mapping two drives in one script.
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.6 - April 24th 2005
' -----------------------------------------------------------------'
Option Explicit
Dim objNetwork, strRemotePath1, strRemotePath2
Dim strDriveLetter1, strDriveLetter2

strDriveLetter1 = "P:"
strDriveLetter2 = "S:"
strRemotePath1 = "\\server\staff"
strRemotePath2 = "\\server\share"

Set objNetwork = CreateObject("WScript.Network")

' Section which maps two drives, M: and P:
objNetwork.MapNetworkDrive strDriveLetter1, strRemotePath1
objNetwork.MapNetworkDrive strDriveLetter2, strRemotePath2

' Extra code just to add a message box
WScript.Echo "Map drives " & strDriveLetter1 & " & " & strDriveLetter2

Wscript.Quit

' End of Windows Logon Script Example

Re: Combining Multiple VB Scripts
"Al Dunbar" <AlanDrub[ at ]hotmail.com.nospaam> 5/10/2007 3:20:51 AM
Well, first of all, you could just combine the code from both and stick it
into one file. But I doubt that the result would be exactly what you want,
and expect it would throw some errors.

What you really want is a script that performs some (or all) of the things
done by each of these two sample scripts into one. One way to do that would
be to start from scratch and build the new script line by line by picking
the functionality needed from whichever script contains it. That would
actually be a good exercise for a beginner, as you would wind up having to
understand what is going on in the scripts, and why they work. One issue I
see here is that the two sample scripts follow a somewhat different style or
approach.

Another method that might work would be to modify each script such that it
does the same work, but consists entirely of a function or sub, and a call,
i.e.:

script 1 before:

option explicit
a bunch of code

script 1 before:

option explicit
call sub1

sub sub1
a bunch of code
end sub

You can probably guess what the template for the second script will look
like...

The content of "a bunch of code" may need to be modified slightly should
you find the result not identical. Keeping "option explicit" at the top of
the script level code is one such modification. When both scripts have been
massaged this way, you could combine them like this:

option explicit
call sub1
call sub2

sub sub1
a bunch of code
end sub

sub sub2
a bunch of more code
end sub

If this has been done correctly, the result should be the same as running
the original script 1 and then running the original script 2.

This is a simple illustration of one of the values of using subs or
functions: they allow chunks of code to co-exist within a larger script
without conflicting with each other. Each has its own local variables that
are completely distinct from other same-named variables in other subs.

But the real power of the technique is not in taking individual main-line
programs and converting them to subs so they can co-exist in one script, but
in writing all your scripts that way so they are ready to be repackaged.


/Al

"JCarbuccia" <JCarbuccia[ at ]discussions.microsoft.com> wrote in message
news:965107FB-9F77-4099-AE10-FF3D25C8F4C0[ at ]microsoft.com...
[Quoted Text]
> I'm new in the scripting business. I've found the scripts I need for my
> environment but they are all separate files, I need to combine all 5 files
> into one large vbs file. Here's a sample of two files I need to combine
> into
> one. Can someone please help.
>
> Thank you,
>
> Script 1:
>
> ' Already.vbs Windows Logon Script
> ' VBScript to map a network drive.
> ' Author Guy Thomas http://computerperformance.co.uk/
> ' Version 1.7 - April 24th 2005
> ' ------------------------------------------------------'
> Option Explicit
> Dim strDriveLetter, strRemotePath
> Dim objNetwork, objShell
> Dim CheckDrive, AlreadyConnected, intDrive
> ' The section sets the variables.
> strDriveLetter = "P:"
> strRemotePath = "\\server\staff"
>
> ' This sections creates two objects:
> ' objShell and objNetwork and counts the drives
> Set objShell = CreateObject("WScript.Shell")
> Set objNetwork = CreateObject("WScript.Network")
> Set CheckDrive = objNetwork.EnumNetworkDrives()
>
> ' This section deals with a For ... Next loop
> ' See how it compares the enumerated drive letters
> ' with strDriveLetter
> On Error Resume Next
> AlreadyConnected = False
> For intDrive = 0 To CheckDrive.Count - 1 Step 2
> If CheckDrive.Item(intDrive) =strDriveLetter _
> Then AlreadyConnected =True
> Next
>
> ' This section uses the If = then, else logic
> ' This tests to see if the Drive is already mapped.
> ' If yes then disconnects
> If AlreadyConnected = True then
> objNetwork.RemoveNetworkDrive strDriveLetter
> objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
>
> ' The first message box
> objShell.PopUp "Drive " & strDriveLetter & _
> "Disconnected, then connected successfully."
> Else
> objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
> objShell.PopUp "Drive " & strDriveLetter & _
> " connected successfully." End if
> WScript.Quit
>
> ' Guy's Script ends here
>
> Script 2:
>
> ' TwoMap.vbs - Map Network Drive to P: and S:
> ' Example of VBScript Mapping two drives in one script.
> ' Author Guy Thomas http://computerperformance.co.uk/
> ' Version 1.6 - April 24th 2005
> ' -----------------------------------------------------------------'
> Option Explicit
> Dim objNetwork, strRemotePath1, strRemotePath2
> Dim strDriveLetter1, strDriveLetter2
>
> strDriveLetter1 = "P:"
> strDriveLetter2 = "S:"
> strRemotePath1 = "\\server\staff"
> strRemotePath2 = "\\server\share"
>
> Set objNetwork = CreateObject("WScript.Network")
>
> ' Section which maps two drives, M: and P:
> objNetwork.MapNetworkDrive strDriveLetter1, strRemotePath1
> objNetwork.MapNetworkDrive strDriveLetter2, strRemotePath2
>
> ' Extra code just to add a message box
> WScript.Echo "Map drives " & strDriveLetter1 & " & " & strDriveLetter2
>
> Wscript.Quit
>
> ' End of Windows Logon Script Example
>


Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net
Suche nach Orten, Städten, Postleitzahlen, Vorwahlen, Kfz-Kennzeichen