[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3698
  • Last Modified:

VBscript: Error when adding network-drive that already exist.

Hi guys.

I have this script:

Option Explicit
Dim objNetwork, objUser, CurrentUser
Dim strGroup

Const Users_Group = "cn=users"

Set objNetwork = CreateObject("WScript.Network")
Set objUser = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & objUser.UserName)
strGroup = LCase(Join(CurrentUser.MemberOf))

If InStr(strGroup, lcase(DomainUsers)) Then
WScript.Echo "Du er medlem af DomainUsers - sætter drev..."
Set objNetwork = CreateObject("WScript.Network")

objNetwork.MapNetworkDrive "h:", "\\fileserver\novellmain" _

ElseIf InStr(strGroup, lcase(Users_Group)) Then
WScript.Echo " Du er ikke medlem af nogen gruppe med printere tilknyttet "
End If
WScript.Quit

This add a network-drive H:, but if the user already has that drive, they get an error:

"The local device name is already in use".

How do I solve this?
0
Billetkontoret
Asked:
Billetkontoret
  • 2
  • 2
  • 2
1 Solution
 
ExcelGuideConsultantCommented:
check out this site:

http://www.computerperformance.co.uk/Logon/Logon_Remove.htm

It should help you out :)

You can you an if statement to do a check whenever you would like to remove the old networkdrive
0
 
BilletkontoretAuthor Commented:
Yes it is more like the "if statement" rather than remove the networkdrive I would use. Do you know how that "if statement" would look like?
0
 
ExcelGuideConsultantCommented:
well, I didnt test this (by head):

{code}
Dim CheckDrive, AlreadyConnected, intDrive
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) ="h:" _
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
'Uncomment if you would like to use the h drive
'objNetwork.RemoveNetworkDrive "h:"
objNetwork.MapNetworkDrive "z:", "\\fileserver\novellmain" _
{code}

In your example I used the new drive "Z:" as replacement. You still should apply this to your own code.

Good luck
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
RobSampsonCommented:
Hi, I find the easiest way to do this is to use the FileSystemObject to check if the drive exists, like so:

Dim objFSO, strDrive, strShare
Set objFSO = CreateObject("Scripting.FileSystemObject")
strDrive = "H:"
strShare = "\\fileserver\novellmain"
If objFSO.DriveExists(strDrive) = False Then
   objNetwork.RemoveNetworkDrive strDrive
End If
objNetwork.MapNetworkDrive strDrive, strShare

Regards,

Rob.
0
 
RobSampsonCommented:
Whoops, this line:
If objFSO.DriveExists(strDrive) = False Then

should be
If objFSO.DriveExists(strDrive) = True Then

Regards,

Rob.
0
 
BilletkontoretAuthor Commented:
Thanks man :)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now