Page 1 of 1

Connect,disconnect,track network drives

Posted: Fri Nov 23, 2007 4:13 am
by Fangbeast

Code: Select all

;============================================================================================================================
; Authors:    Rings, Hi-Toro and others
; Cop[yright: Free
; Rebashed:   Fangbeast. Done to suit what I needed.
;
; Notes:      I found this set of routines in the forums done as far back as 2003 (I think) done by a variety of people
;             much smarter than me and only just started playing with it in response to an idea given to me by my somebody
;             for a project he wanted. Attach network drives to the system, detach network drives. Keep track of network
;             drives in a list. Use an interactive dialogue to connect network drives. Copy files to a network drive.
;
;             That's all the explanation I feel like doing right now, too tired. Probably has bugs, don't care.
;
;============================================================================================================================
; A structure to keep track of drive letters and net paths
;============================================================================================================================

Structure DriveList                                          ; Keep track of drive letters and net paths
  DriveLetter.s                                               ; 
  DrivePath.s                                                 ; 
EndStructure                                                 ; 

;============================================================================================================================
; The global tracking list for the network drives on your system
;============================================================================================================================

Global NewList NetDrives.DriveList()                         ; Tracking list for the network drives on your system

;============================================================================================================================
; Find all network drives attached to the system and store them in a linked list
;============================================================================================================================

Procedure GetNetworkDrivesInSystem()                         ; 
  #dllMPR = 1                                                 ; Use a constant for the dll
  If OpenLibrary(#dllMPR, "MPR.DLL")                          ; Open the necessary windows dll
    For DriveCount.b = 4 To 26                                ; Count through drives, exclude floppies
      DriveLetter.s = Chr(DriveCount.b + 64) + ":"            ; Get current drive letter
      DriveType.l   = GetDriveType_(DriveLetter)              ; What sort of drive are we looking for
      If DriveType = #DRIVE_REMOTE                            ; Did we find a remote drive?
        UNCPath.s     = Space(128)                            ; Allocate space for the path length
        LenUNCPath.l  = 127                                   ; Allocate the length for this space
        ErrorNum.l    = CallFunction(#dllMPR, "WNetGetConnectionA", @DriveLetter, @UNCPath, @LenUNCPath)
        If ErrorNum = #NO_ERROR                               ; We didn't get an error
          UNCPath = Left(UNCPath, LenUNCPath)                 ; Get the current drive found
          ForEach NetDrives()                                ; Iterate through our tracking list
            If NetDrives()\DrivePath = UNCPath                ; If the drive already exists in the list
              ExistsFlag = 1                                  ; Set the flag on
            Else                                              ; Otherwise
              ExistsFlag = 0                                  ; Set the flag off
            EndIf                                             ; No more tests
          Next                                                ; Next drive in the tracking list
          If ExistsFlag = 0                                   ; If the flag was set off
            AddElement(NetDrives())                           ; Add a blank element to the tracking list
            NetDrives()\DriveLetter = DriveLetter             ; Add the found drive to it
            NetDrives()\DrivePath   = UNCPath                 ; Add the found drive to it
          EndIf                                               ; No more tests
          ForEach NetDrives()                                ; Iterate through the tracking list
            ; Debug NetDrives()                               ; Show the found network drives or other things you need to do with them
          Next                                                ; Next drive in the list
        Else                                                  ; Otherwise, we have an error
          Select ErrorNum                                     ; Test the error number
            Case #ERROR_BAD_DEVICE         : UNCPath = "ERROR_BAD_DEVICE"
            Case #ERROR_NOT_CONNECTED      : UNCPath = "ERROR_NOT_CONNECTED"
            Case #ERROR_MORE_DATA          : UNCPath = "ERROR_MORE_DATA"
            Case #ERROR_CONNECTION_UNAVAIL : UNCPath = "ERROR_CONNECTION_UNAVAIL"
            Case #ERROR_NO_NETWORK         : UNCPath = "ERROR_NO_NETWORK"
            Case #ERROR_EXTENDED_ERROR     : UNCPath = "ERROR_EXTENDED_ERROR"
            Case #ERROR_NO_NET_OR_BAD_PATH : UNCPath = "ERROR_NO_NET_OR_BAD_PATH"
            Case #ERROR_INVALID_ADDRESS    : UNCPath = "ERROR_INVALID_ADDRESS"
            Default                        : UNCPath = "UNKNOWN ERROR " + Str(ErrorNum)
          EndSelect                                           ; No more tests to do
        EndIf                                                 ; No more conditional tests
        ; AllDrives.s = AllDrives.s + UNCPath + CHR(10) + CHR(13); Usually add up all the network drives found here
      EndIf                                                   ; No more conditional tests
    Next                                                      ; Look for the next possible drive letter
    ;------------------------------------------------------------------------------------------------  
  ;   If Len(AllDrives.s)    ; If we were just reporting ALL the drives found, this is a good place to put it
  ;     Debug AllDrives.s
  ;   EndIf
    ;------------------------------------------------------------------------------------------------  
  Else                                                        ; Otherwise, we cannot find the right dll for these functions
   MessageRequester("Error", "MPR.DLL not found", 0)           ; Give the user an error message
  EndIf                                                       ; No more tests
EndProcedure                                                 ; 

;============================================================================================================================
; Connect a remote network drive to your system. Must already be shared
;============================================================================================================================

Procedure ConnectNetworkDrive(LocalDriveLetter.s, RemotePath.s)
  ; RemotePath.s        = "\\FurBox\Roo's incoming"           ; Remote path
  ; LocalDriveLetter.s  = "Z:"                                ; Local drive to be 'created'
  Define.NETRESOURCE res                                     ; res points to structure that specifies connection details
  res\dwType        = #RESOURCETYPE_DISK                      ; 
  res\lpLocalName   = @LocalDriveLetter.s                     ; 
  res\lpRemoteName  = @RemotePath.s                           ; 
  res\lpProvider    = #Null                                   ; 
  If WNetAddConnection2_ (res, #Null, #Null, 0) = #NO_ERROR   ; 
    GetNetworkDrivesInSystem()                                ; Rescan system and add the new drive not in the list
    MessageRequester("Information:", "Connected the network drive successfully", #MB_ICONINFORMATION)
  Else                                                        ; 
    MessageRequester("Error:", "Couldn't connect the network drive, check paths, network and share status", #MB_ICONWARNING)
  EndIf                                                       ; 
EndProcedure                                                 ; 

;============================================================================================================================
; Disconnect a network drive attached to the system. Check the linked list to see if it had already been added before and
; optionally delete the item from the list
;============================================================================================================================

Procedure DisconnectNetworkDrive(LocalDriveLetter.s, Remove.l)
  ; RemotePath.s        = "\\FurBox\Roo's incoming"           ; Remote path
  ; LocalDriveLetter.s  = "Z:"                                ; Local drive to be 'created'
  ; Remove.l                                                  ; Remove the disconnected drive from the tracking linked list
  Define.NETRESOURCE res                                     ; res points to structure that specifies connection details
  ForEach NetDrives()                                        ; Iterate through our tracking list
    If NetDrives()\DriveLetter = LocalDriveLetter.s           ; If the found drive is in the tracking list
      res\dwType        = #RESOURCETYPE_DISK                  ; 
      res\lpLocalName   = @LocalDriveLetter.s                 ; 
      res\lpRemoteName  = @NetDrives()\\DrivePath             ; 
      res\lpProvider    = #Null                               ; 
      WNetCancelConnection2_(LocalDriveLetter.s, #CONNECT_UPDATE_PROFILE, 0) ; Disconnect the connection if it exists
    EndIf                                                     ; 
    If Remove.l                                               ; Do we want to keep this path in the list or not?
      DeleteElement(NetDrives())                              ; 
    EndIf                                                     ; 
  Next                                                        ; 
EndProcedure                                                 ; 

;============================================================================================================================
; Assign remote path to drive (eg. "Z:") using an interactive dialog box
;============================================================================================================================

Procedure ConnectNetworkDriveD()                            ; 
  If WNetConnectionDialog_ (0, #RESOURCETYPE_DISK) = #NO_ERROR; 
    MessageRequester("Information:", "Connected the network drive successfully", #MB_ICONINFORMATION)
    GetNetworkDrivesInSystem()                                ; Rescan system and add the new drive not in the list
  Else                                                        ; 
    MessageRequester("Error:", "Couldn't connect the network drive, check paths, network and share status", #MB_ICONWARNING)
  EndIf                                                       ; 
EndProcedure                                                 ; 

;============================================================================================================================
; Copy a local file to a network drive. Check if it is in the list first. Check if the dir exists
;============================================================================================================================

Procedure CopyFileToNetworkDrice(DriveLetter.s, FileName.s)   ; 
  ForEach NetDrives()                                         ; Iterate through our tracking list
    If NetDrives()\DriveLetter = DriveLetter.s                ; If the found drive is in the tracking list
      If FileSize(NetDrives()\DrivePath) = -2                 ;
        NewFile.s = NetDrives()\DrivePath + "\" + GetFilePart(FileName.s)   ;
        If CopyFile(FileName.s, NewFile.s)                    ; 
          MessageRequester("Information:", "Copied " + FileName.s + " to " + NewFile.s, #MB_ICONWARNING)
        Else                                                  ; 
          MessageRequester("Error:", "Cannot copy the file to the destination", #MB_ICONWARNING)
        EndIf                                                 ; 
      Else                                                    ; 
        MessageRequester("Error:", "This drive does not exist, cannot copy the file to destination", #MB_ICONWARNING)
      EndIf                                                   ; 
    Else                                                      ; 
      MessageRequester("Error:", "This drive does not exist in the list, cannot copy the file to destination", #MB_ICONWARNING)
    EndIf                                                     ;
  Next                                                        ; 
EndProcedure                                                 ;

Posted: Fri Nov 23, 2007 4:27 am
by netmaestro
Procedure CopyFileToNetworkDrice
Is that intentional? or a slip?

Posted: Fri Nov 23, 2007 4:30 am
by Fangbeast
netmaestro wrote:
Procedure CopyFileToNetworkDrice
Is that intentional? or a slip?
My quote above says "Probably has bugs, don't care"

Doesn't that include procedure names?? (VERY EVIL GRIN)

You know how badly I spell when I am tired don't you? (grin). (Or, it could be the Hungarian spelling of Drive)


/me falls off the chair laughing

Posted: Mon Nov 26, 2007 9:48 am
by Dare
Heya Fangs,

Thanks for this! I will have a play with it over next few days.

Posted: Mon Nov 26, 2007 11:16 am
by Fangbeast
Hope it's useful to somebody. Probably could have done more but depression has me lately so maybe when this lot fades I can do better.