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?
LVL 2
BilletkontoretAsked:
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.

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
Get your problem seen by more experts

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

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

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 :)
0
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.