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

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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

check out this site:


It should help you out :)

You can you an if statement to do a check whenever you would like to remove the old networkdrive
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?
well, I didnt test this (by head):

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

' 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" _

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

Good luck
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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


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

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



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BilletkontoretAuthor Commented:
Thanks man :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.