[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VBScript - Network Drives

Posted on 2007-10-17
4
Medium Priority
?
657 Views
Last Modified: 2012-06-27
Trying to Map 2 drives with the option that the drives might already be conencted using VBscript, and can't get it to run. Heres my script

Option Explicit
Dim strDriveLetter1, strRemotePath1, strDriveLetter2, strRemotePath2
Dim objNetwork, objShell
Dim CheckDrive, intDrive, AlreadyConnected, AlreadyConnected2
strDriveLetter1 = "s:"
strRemotePath1 = "\\ginkgo\shared"
strDriveLetter2 = "g:"
strRemotePath2 = "\\rainbowserver\graphics"
set objShell = CreateObject("WScript.Shell")
set objNetwork = CreateObject("WScript.Network")
set CheckDrive = objNetwork.EnumNetworkDrives()
On Error Resume Next
AlreadyConnected = False
AlreadyConnected2 = False
For intDrive = 0 To CheckDrive.count - 1 Step 2
If DriveCheck.Item(intDrive) =strDriveLetter1_ Then AlreadyConnected = True
Next
If AlreadyConnected = True Then
objNetwork.RemoveNetworkDrive strDriveLetter1
objShell.PopUp "Drive " &strDriveLetter1 &_
"Disconnected"
objNetwork.MapNetworkDrive strDriveLetter1, strRemotePath1
objShell.PopUP "Drive " &strDriveLetter1 &_
"Connected, Welcome to Shared Files"
Else
objNetwork.MapNetworkDrive strDriveLetter1, strRemotePath1
objShell.PopUp "Sharin'"
If DriveCheck.Item(intDrive) =strDriveLetter2_ Then AlreadyConnected2 = True
IF AlreadyConnected2 = True Then
objNetwork.RemoveNetworkDrive strDriveLetter2
objShell.Popup "Graphics Being Removed"
objNetwork.MapNetworkDrive strDriveLetter2 strRemotePath2
objShell.Popup "Graphics Being Installed"
Else
objNetwork.MapNetworkDrive strDriveLetter2 strRemotePath2 End If


Keeps asking for an end command on line 32, but thats in the middle of my script.  As you can see I am no pro at scripting.  Thanks for any help!
0
Comment
Question by:JamesonJendreas
  • 2
  • 2
4 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 750 total points
ID: 20097198
Hi, try this, but what is DriveCheck? Is it a custom function?

'==============
Option Explicit
Dim strDriveLetter1, strRemotePath1, strDriveLetter2, strRemotePath2
Dim objNetwork, objShell
Dim CheckDrive, intDrive, AlreadyConnected, AlreadyConnected2
strDriveLetter1 = "s:"
strRemotePath1 = "\\ginkgo\shared"
strDriveLetter2 = "g:"
strRemotePath2 = "\\rainbowserver\graphics"
set objShell = CreateObject("WScript.Shell")
set objNetwork = CreateObject("WScript.Network")
set CheckDrive = objNetwork.EnumNetworkDrives()
On Error Resume Next
AlreadyConnected = False
AlreadyConnected2 = False
For intDrive = 0 To CheckDrive.count - 1 Step 2
If DriveCheck.Item(intDrive) = strDriveLetter1 Then AlreadyConnected = True
Next
If AlreadyConnected = True Then
objNetwork.RemoveNetworkDrive strDriveLetter1
objShell.PopUp "Drive " &strDriveLetter1 & _
"Disconnected"
objNetwork.MapNetworkDrive strDriveLetter1, strRemotePath1
objShell.PopUP "Drive " &strDriveLetter1 & _
"Connected, Welcome to Shared Files"
Else
objNetwork.MapNetworkDrive strDriveLetter1, strRemotePath1
objShell.PopUp "Sharin'"
If DriveCheck.Item(intDrive) = strDriveLetter2 Then AlreadyConnected2 = True
IF AlreadyConnected2 = True Then
objNetwork.RemoveNetworkDrive strDriveLetter2
objShell.Popup "Graphics Being Removed"
objNetwork.MapNetworkDrive strDriveLetter2 strRemotePath2
objShell.Popup "Graphics Being Installed"
Else
objNetwork.MapNetworkDrive strDriveLetter2 strRemotePath2
End If
'==============

By the way, to check if a drive exists, you can also use the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.DriveExists(strDriveLetter1) Then AlreadyConnected = True

Regards,

Rob.
0
 
LVL 1

Author Comment

by:JamesonJendreas
ID: 20101614
Hi,
Yes DriveCheck is supposed to be a function that I am creating to check if a drive exisits or not.  The above script keeps giving me the same error, so I am going to try and re-write it with the FileSystemObject.
Thanks
0
 
LVL 1

Author Comment

by:JamesonJendreas
ID: 20102141
Ok, So I think I'm close, I just don't know how to end this script, every time I get an Expected "End" or "If" statement on line 32. the last line of the script.  What am i Doing wrong?

Option Explicit
Dim strDriveLetter1, strRemotePath1, strDriveLetter2, strRemotePath2
Dim objNetwork, objShell, objFso
Dim CheckDrive, intDrive, AlreadyConnected, AlreadyConnected2
strDriveLetter1 = "s:"
strRemotePath1 = "\\ginkgo\shared"
strDriveLetter2 = "g:"
strRemotePath2 = "\\rainbowserver\graphics"
set objShell = CreateObject("WScript.Shell")
set objNetwork = CreateObject("WScript.Network")
set CheckDrive = objNetwork.EnumNetworkDrives()
On Error Resume Next
AlreadyConnected = False
AlreadyConnected2 = False
set objFso = CreateObject ("Scripting.FIleSystemObject")
IF objFso.DriveExists(strDriveLetter1) then AlreadyCOnnected = true
If AlreadyConnected = true then
      objNetwork.RemoveNetworkDrive strDriveLetter1
      objShell.PopUp "Disconnecting Shared Drive"
      objNetwork.AddNetworkDrive strDriveLetter1, strRemotePath1
      objShell.PopUp "Reconnecting To Shared Drive"
Else
objNetwork.AddNetworkDrive strDriveLetter1, strRemotePath1
If objFso.DriveExists(strDriveLetter2) then AlreadyConnected2 = true
If AlreadyConnected = True Then
      objNetwork.RemoveNetworkDrive strDriveLetter2
      objShell.PopUp "Disconnecting Graphics"
      objNetwork.AddNetworkDrive strDriveletter2, strRemotePath2
      objShell.PopUp "Reconnecting Graphics"
Else
objNetwork.AddNetworkDrive strdriveLetter2, strRemotePath2
End If


All Help is much appreciated, thanks!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20105361
You're missing an End If after this line:
objNetwork.AddNetworkDrive strDriveLetter1, strRemotePath1

This should work:
'===========
Option Explicit
Dim strDriveLetter1, strRemotePath1, strDriveLetter2, strRemotePath2
Dim objNetwork, objShell, objFso
Dim CheckDrive, intDrive, AlreadyConnected, AlreadyConnected2
strDriveLetter1 = "s:"
strRemotePath1 = "\\ginkgo\shared"
strDriveLetter2 = "g:"
strRemotePath2 = "\\rainbowserver\graphics"
set objShell = CreateObject("WScript.Shell")
set objNetwork = CreateObject("WScript.Network")
set CheckDrive = objNetwork.EnumNetworkDrives()
On Error Resume Next
AlreadyConnected = False
AlreadyConnected2 = False
set objFso = CreateObject ("Scripting.FIleSystemObject")
IF objFso.DriveExists(strDriveLetter1) then AlreadyCOnnected = True
If AlreadyConnected = true Then
      objNetwork.RemoveNetworkDrive strDriveLetter1
      objShell.PopUp "Disconnecting Shared Drive"
      objNetwork.AddNetworkDrive strDriveLetter1, strRemotePath1
      objShell.PopUp "Reconnecting To Shared Drive"
Else
      objNetwork.AddNetworkDrive strDriveLetter1, strRemotePath1
End If
If objFso.DriveExists(strDriveLetter2) then AlreadyConnected2 = true
If AlreadyConnected = True Then
      objNetwork.RemoveNetworkDrive strDriveLetter2
      objShell.PopUp "Disconnecting Graphics"
      objNetwork.AddNetworkDrive strDriveletter2, strRemotePath2
      objShell.PopUp "Reconnecting Graphics"
Else
objNetwork.AddNetworkDrive strdriveLetter2, strRemotePath2
End If
'===========

Regards,

Rob.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
In this article, the configuration steps in Zabbix to monitor devices via SNMP will be discussed with some real examples on Cisco Router/Switch, Catalyst Switch, NAS Synology device.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question