Jump to content

Drive mapping problem


Recommended Posts

As my first AutoIT project, I wanted to create a network drive mapper to streamline the process for users. Basically there are two servers with 3 similar shares on each, and the user chooses which server they want for which share.

The script functions fine if there are no existing mappings. However when it comes to removing existing to replace with their new choice, only the first share (G:) will change. The others will remain as they were created originally. I've tried using Sleep to delay each part, but it doesn't seem to help. I have a feeling I've squeezed too much into the one 'function' so it might not be coping well. My problem is that I don't know how to structure it differently. Any advice is appreciated.

Another thing that I couldn't find was how to do error reporting for DriveMapDel since according to the docs it doesnt output to @error. Not really important, but just curious.

This is my full script, just with servernames removed to protect the innocent, and my job :)

*PS - I realise the GUI code is a bit messy, sorry.

#include <GUIConstants.au3>

#Region ### START Koda GUI section ###
$DriveMapper = GUICreate("Drive Mapper v0.1", 442, 558, 285, 63)
GUISetFont(12, 800, 0, "MS Sans Serif")
GUISetBkColor(0xA6CAF0)
$Title = GUICtrlCreateLabel("Network Drive Mapping Tool", 14, 8, 409, 28)
GUICtrlSetFont(-1, 16, 800, 0, "Arial")
$CHOOSE = GUICtrlCreateLabel("Choose your drive mappings...", 96, 80, 244, 24)
$GENERAL = GUICtrlCreateGroup("G: (General/Common)", 102, 112, 233, 89)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$G_SITE = GUICtrlCreateRadio("SITE (Server1\Common)", 116, 144, 273, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$G_TOWN = GUICtrlCreateRadio("TOWN (Server2\General)", 116, 167, 273, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$TRANSFER = GUICtrlCreateGroup("H: (Transfer)", 104, 219, 233, 89)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$h_SITE = GUICtrlCreateRadio("SITE (Server1\Transfer)", 118, 251, 273, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$h_TOWN = GUICtrlCreateRadio("TOWN (Server2\Transfer)", 118, 274, 273, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$USERS = GUICtrlCreateGroup("Z: (Users)", 102, 326, 233, 89)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$z_SITE = GUICtrlCreateRadio("SITE (Server1\Users)", 116, 358, 273, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$z_TOWN = GUICtrlCreateRadio("TOWN (Server2\Users)", 116, 381, 273, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$OK = GUICtrlCreateButton("OK", 128, 496, 75, 25, 0)
$CANCEL = GUICtrlCreateButton("CANCEL", 216, 496, 83, 25, 0)
$VERSION = GUICtrlCreateLabel("v0.1", 312, 536, 119, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x808080)
$PERMASK = GUICtrlCreateCheckbox("If you wish these mappings to be permanent, tick this box", 82, 424, 273, 65, $BS_MULTILINE)
GUICtrlSetFont(-1, 9, 800, 0, "MS Sans Serif")
$WARNING = GUICtrlCreateLabel("WARNING: This will replace existing mappings", 68, 40, 300, 20)
GUICtrlSetFont(-1, 10, 800, 0, "Arial")
GUICtrlSetColor(-1, 0xFF0000)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

; Run the GUI
While 1
    $msg = GUIGetMsg()
    Select
    ;Close GUI on X
        Case $msg = $GUI_EVENh_CLOSE
            ExitLoop
    ;Processing chosen mappings on hitting OK
        Case $msg = $OK
            If GUICtrlRead($G_SITE) = 1 Then
            ;Remove existing G: Map
                DriveMapDel("G:")
                Sleep(300)
            ;Map G: to SITE General Drive
                DriveMapAdd("G:", "\\Server1\Common")
            ;Error reporting
                If @error = 1 Then MsgBox(48, "ERROR", "Error Mapping Drive G: - Unknown Error")
                If @error = 2 Then MsgBox(48, "ERROR", "Error Mapping Drive G: - Access Denied")
                If @error = 3 Then MsgBox(48, "ERROR", "Error Mapping Drive G: - Already Mapped")
                If @error = 5 Then MsgBox(48, "ERROR", "Error Mapping Drive G: - Invalid Share / Share not found")              
            EndIf
            If GUICtrlRead($G_TOWN) = 1 Then
            ;Remove existing G: Map
                DriveMapDel("G:")
                Sleep(300)
            ;Map G: to TOWN General Drive
                DriveMapAdd("G:", "\\Server2\General")
            ;Error reporting
                If @error = 1 Then MsgBox(48, "ERROR", "Error Mapping Drive G: - Unknown Error")
                If @error = 2 Then MsgBox(48, "ERROR", "Error Mapping Drive G: - Access Denied")
                If @error = 3 Then MsgBox(48, "ERROR", "Error Mapping Drive G: - Already Mapped")
                If @error = 5 Then MsgBox(48, "ERROR", "Error Mapping Drive G: - Invalid Share / Share not found")              
            EndIf
            Sleep(500)
            If GUICtrlRead($h_SITE) = 1 Then
            ;Remove existing H: Map
                DriveMapDel("H:")
                Sleep(300)
            ;Map H: to SITE Transfer Drive
                DriveMapAdd("H:", "\\Server1\Transfer")
            ;Error reporting
                If @error = 1 Then MsgBox(48, "ERROR", "Error Mapping Drive H: - Unknown Error")
                If @error = 2 Then MsgBox(48, "ERROR", "Error Mapping Drive H: - Access Denied")
                If @error = 3 Then MsgBox(48, "ERROR", "Error Mapping Drive H: - Already Mapped")
                If @error = 5 Then MsgBox(48, "ERROR", "Error Mapping Drive H: - Invalid Share / Share not found")              
            EndIf
            If GUICtrlRead($h_TOWN) = 1 Then
            ;Remove existing H: Map
                DriveMapDel("H:")
                Sleep(300)
            ;Map H: to TOWN Transfer Drive
                DriveMapAdd("H:", "\\Server2\Transfer")
            ;Error reporting
                If @error = 1 Then MsgBox(48, "ERROR", "Error Mapping Drive H: - Unknown Error")
                If @error = 2 Then MsgBox(48, "ERROR", "Error Mapping Drive H: - Access Denied")
                If @error = 3 Then MsgBox(48, "ERROR", "Error Mapping Drive H: - Already Mapped")
                If @error = 5 Then MsgBox(48, "ERROR", "Error Mapping Drive H: - Invalid Share / Share not found")              
            EndIf
            Sleep(500)
            If GUICtrlRead($z_SITE) = 1 Then
            ;Remove existing Z: Map
                DriveMapDel("Z:")
                Sleep(300)
            ;Map Z: to SITE Users Drive
                DriveMapAdd("Z:", "\\Server1\Users")
            ;Error reporting
                If @error = 1 Then MsgBox(48, "ERROR", "Error Mapping Drive Z: - Unknown Error")
                If @error = 2 Then MsgBox(48, "ERROR", "Error Mapping Drive Z: - Access Denied")
                If @error = 3 Then MsgBox(48, "ERROR", "Error Mapping Drive Z: - Already Mapped")
                If @error = 5 Then MsgBox(48, "ERROR", "Error Mapping Drive Z: - Invalid Share / Share not found")              
            EndIf
            If GUICtrlRead($z_TOWN) = 1 Then
            ;Remove existing Z: Map
                DriveMapDel("Z:")
                Sleep(300)
            ;Map Z: to TOWN Users Drive
                DriveMapAdd("Z:", "\\Server2\Users")
            ;Error reporting
                If @error = 1 Then MsgBox(48, "ERROR", "Error Mapping Drive Z: - Unknown Error")
                If @error = 2 Then MsgBox(48, "ERROR", "Error Mapping Drive Z: - Access Denied")
                If @error = 3 Then MsgBox(48, "ERROR", "Error Mapping Drive Z: - Already Mapped")
                If @error = 5 Then MsgBox(48, "ERROR", "Error Mapping Drive Z: - Invalid Share / Share not found")              
            EndIf
    ;Close GUI on hitting CANCEL
        Case $msg = $CANCEL
            Exit
    EndSelect
Wend
Edited by EndOfLine
Link to comment
Share on other sites

Well I don't have a solution to your drive removing dilemma, but this is a very much cleaned up version of your code... Anytime you repeat the same line of code over and over you can probably make a function out of it and just call that function over and over (using one line) instead of all the line of code that you can just put in a function once.

Does it work any better if you just do one of those DriveMapDel/DriveMapAdd commands at a time (instead of giving them the option to do up to 3 at a time?)

#include <GUIConstants.au3>

#Region ### START Koda GUI section ###
$DriveMapper = GUICreate("Drive Mapper v0.1", 442, 558, 285, 63)
GUISetFont(12, 800, 0, "MS Sans Serif")
GUISetBkColor(0xA6CAF0)
$Title = GUICtrlCreateLabel("Network Drive Mapping Tool", 14, 8, 409, 28)
GUICtrlSetFont(-1, 16, 800, 0, "Arial")
$CHOOSE = GUICtrlCreateLabel("Choose your drive mappings...", 96, 80, 244, 24)
$GENERAL = GUICtrlCreateGroup("G: (General/Common)", 102, 112, 233, 89)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$G_SITE = GUICtrlCreateRadio("SITE (Server1\Common)", 116, 144, 273, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$G_TOWN = GUICtrlCreateRadio("TOWN (Server2\General)", 116, 167, 273, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$TRANSFER = GUICtrlCreateGroup("H: (Transfer)", 104, 219, 233, 89)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$h_SITE = GUICtrlCreateRadio("SITE (Server1\Transfer)", 118, 251, 273, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$h_TOWN = GUICtrlCreateRadio("TOWN (Server2\Transfer)", 118, 274, 273, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$USERS = GUICtrlCreateGroup("Z: (Users)", 102, 326, 233, 89)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$z_SITE = GUICtrlCreateRadio("SITE (Server1\Users)", 116, 358, 273, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$z_TOWN = GUICtrlCreateRadio("TOWN (Server2\Users)", 116, 381, 273, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$OK = GUICtrlCreateButton("OK", 128, 496, 75, 25, 0)
$CANCEL = GUICtrlCreateButton("CANCEL", 216, 496, 83, 25, 0)
$VERSION = GUICtrlCreateLabel("v0.1", 312, 536, 119, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x808080)
$PERMASK = GUICtrlCreateCheckbox("If you wish these mappings to be permanent, tick this box", 82, 424, 273, 65, $BS_MULTILINE)
GUICtrlSetFont(-1, 9, 800, 0, "MS Sans Serif")
$WARNING = GUICtrlCreateLabel("WARNING: This will replace existing mappings", 68, 40, 300, 20)
GUICtrlSetFont(-1, 10, 800, 0, "Arial")
GUICtrlSetColor(-1, 0xFF0000)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

; Run the GUI
While 1
    $msg = GUIGetMsg()
    Select
    ;Close GUI on X
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
            
    ;Processing chosen mappings on hitting OK
        Case $msg = $OK
            If GUICtrlRead($G_SITE) = 1 Then
                ;Map G: to SITE General Drive
                _DriveRemoveAdd ("G", "\\Server1\Common")
           EndIf
           
            If GUICtrlRead($G_TOWN) = 1 Then
                ;Map G: to TOWN General Drive
                _DriveRemoveAdd ("G", "\\Server2\General")
            EndIf
           
            If GUICtrlRead($h_SITE) = 1 Then
                ;Map H: to SITE Transfer Drive
                _DriveRemoveAdd ("H", "\\Server1\Transfer")
            EndIf
               
            If GUICtrlRead($h_TOWN) = 1 Then
                ;Map H: to TOWN Transfer Drive
                _DriveRemoveAdd ("H", "\\Server2\Transfer")
            EndIf
            
            If GUICtrlRead($z_SITE) = 1 Then
                ;Map Z: to SITE Users Drive
                _DriveRemoveAdd ("Z", "\\Server1\Users")
               EndIf
               
            If GUICtrlRead($z_TOWN) = 1 Then
                ;Map Z: to TOWN Users Drive
                _DriveRemoveAdd ("Z", "\\Server2\Users")
            EndIf
               
    ;Close GUI on hitting CANCEL
        Case $msg = $CANCEL
            Exit
    EndSelect
Wend

Func _DriveRemoveAdd($DriveLetter, $DrivePath)
    ;Remove existing Drive Mapping
        DriveMapDel($DriveLetter & ":")
        Sleep(300)
    ;Map $DriveLetter: to specified location
        DriveMapAdd($DriveLetter & ":", $DrivePath)
        
    ;Error reporting
    If @error = 1 Then MsgBox(48, "ERROR", "Error Mapping Drive " & $DriveLetter & ": - Unknown Error")
    If @error = 2 Then MsgBox(48, "ERROR", "Error Mapping Drive " & $DriveLetter & ": - Access Denied")
    If @error = 3 Then MsgBox(48, "ERROR", "Error Mapping Drive " & $DriveLetter & ": - Already Mapped")
    If @error = 5 Then MsgBox(48, "ERROR", "Error Mapping Drive " & $DriveLetter & ": - Invalid Share / Share not found")
EndFunc
Edited by exodius
Link to comment
Share on other sites

Well I don't have a solution to your drive removing dilemma, but this is a very much cleaned up version of your code... Anytime you repeat the same line of code over and over you can probably make a function out of it and just call that function over and over (using one line) instead of all the line of code that you can just put in a function once.

Does it work any better if you just do one of those DriveMapDel/DriveMapAdd commands at a time (instead of giving them the option to do up to 3 at a time?)

Thanks for the code tidy. I was going to get into Functions at a later date to streamline it. Thanks :)

I did check, and if I select only 1 drive, then I can change between the servers at will, but as soon as more than one is selected, it will only do the first one. I'll try it with your adjustments to see if it makes a difference.

EDIT - Same thing happens. Works for the first only.

Edited by EndOfLine
Link to comment
Share on other sites

Thanks for the code tidy. I was going to get into Functions at a later date to streamline it. Thanks :)

I did check, and if I select only 1 drive, then I can change between the servers at will, but as soon as more than one is selected, it will only do the first one. I'll try it with your adjustments to see if it makes a difference.

Something I guess the documentation doesn't say, is if the command waits until it's done to continue... It could be an issue, especially if you're talking about a networked drive over some sort of vpn/remote connection, that it's taking so long to add the first network drive that the resources are still tied up when you go to go on to the next drive, maybe try doing an add, then doing a while 1...Wend loop until DriveMapGet returns the information that you want before continuing on?

Link to comment
Share on other sites

Something I guess the documentation doesn't say, is if the command waits until it's done to continue... It could be an issue, especially if you're talking about a networked drive over some sort of vpn/remote connection, that it's taking so long to add the first network drive that the resources are still tied up when you go to go on to the next drive, maybe try doing an add, then doing a while 1...Wend loop until DriveMapGet returns the information that you want before continuing on?

Well one of the servers is remote, and it does take a little while for the mappings to happen. To test it, I cleared all mappings and selected all 3 of the remote shares to map at once and it worked. How would I go about the wend loops? Nest each pair into its own loop? I gather from your suggestion that there isnt a 'Wait until done' call?

I'll have a fiddle with some loops and see if it helps.

UPDATE - I did a loop for each of the pairs, and it seemed to cause a neverending map/unmap/map loop for the first drive and didn't go any further.

Edited by EndOfLine
Link to comment
Share on other sites

Is there an edit timeout for this forum? I don't like double-posting.

Anyway, I tried to take it one further and loop the individual maps (6), but that ended up with a 99% cpu usage, and a 'Disconnected network drive' that I couldnt disconnect from :)

I must be doing something wrong somewhere, or misunderstood what you meant.

Link to comment
Share on other sites

Is there an edit timeout for this forum? I don't like double-posting.

Anyway, I tried to take it one further and loop the individual maps (6), but that ended up with a 99% cpu usage, and a 'Disconnected network drive' that I couldnt disconnect from :)

I must be doing something wrong somewhere, or misunderstood what you meant.

I'm thinking something like this but I don't have the best means to test it since I don't have remote servers that might time out...:

Func _DriveRemoveAdd($DriveLetter, $DrivePath)
    ;Remove existing Drive Mapping
        DriveMapDel($DriveLetter & ":")
        Do
            Sleep (100)
        Until DriveMapGet ($DriveLetter & ":") <> ""
        
    ;Map $DriveLetter: to specified location
        DriveMapAdd($DriveLetter & ":", $DrivePath)
        Do
            Sleep (100)
        Until DriveMapGet ($DriveLetter & ":") = $DrivePath
       
    ;Error reporting
    If @error = 1 Then MsgBox(48, "ERROR", "Error Mapping Drive " & $DriveLetter & ": - Unknown Error")
    If @error = 2 Then MsgBox(48, "ERROR", "Error Mapping Drive " & $DriveLetter & ": - Access Denied")
    If @error = 3 Then MsgBox(48, "ERROR", "Error Mapping Drive " & $DriveLetter & ": - Already Mapped")
    If @error = 5 Then MsgBox(48, "ERROR", "Error Mapping Drive " & $DriveLetter & ": - Invalid Share / Share not found")
EndFunc
Edited by exodius
Link to comment
Share on other sites

I realised after I got home last night that I misread what you meant, and that my looping was incorrect.

I made the changes you suggested, but it doesn't map anything at all now, just sits there. I think that its something to do with the line below.

Until DriveMapGet ($DriveLetter & ":") <> ""

I think that what it reports for a blank map is different, or I'll have to incorporate the @error flag. I'll see if I can get that to work. Thanks for pointing me in the right direction.

If it continues to be a pain, I think I might just incorporate a seperate 'map' button for each pair.

EDIT - Okay, if I change the above <> to = it functions, but I still get the same result, only 1 drive working at a time, or the first if multiple are selected. I think I'll start work on my 'multi-button' solution in the meantime, however I would be curious how to fix this problem.

EDIT2 - I have it working exactly how I want it to with multiple buttons. I guess this is how I'll have to do it for my v1 anyway :). Thanks again for your help exodius.

Edited by EndOfLine
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...