offline#1


avatar
Administradores
Administradores
em Sab 23 Jul 2011, 19:43
Eu acho que esta UDF ajudará muitas pessoas que usam a linha de comando (Subst.exe and diskpart.exe) para realizar os seguintes procedimentos:

1- Definir, redefinir ou remover nomes de dispositivos no MS-DOS;
2- Associar um volume com letra de unidade ou diretório em outro volume;
3- Editar associações de letras de Drives.


Funções:
_ChangeDriveLetter()
_VirtualDrive()


Salve como _Drivers.au3
Código:

#include-once
; #INDEX# =======================================================================================================================
; Title .........: _Drives
; AutoIt Version.: 3.2.12++
; Language.......: English
; Description ...: Defines, redefines, or deletes MS-DOS device names; Associates a volume with a drive letter or a directory
;            on another volume; Editing Drive Letter Assignments.
; Author ........: João Carlos (jscript)
; Support .......:
; ===============================================================================================================================

; #CURRENT# =====================================================================================================================
;_ChangeDriveLetter
;_VirtualDrive
; ===============================================================================================================================

; #INTERNAL_USE_ONLY# ===========================================================================================================
;__DefineDosDevice
;__GetVolumeName
; ===============================================================================================================================

; #VARIABLES# ===================================================================================================================

; #FUNCTION# ====================================================================================================================
; Name...........: _ChangeDriveLetter
; Description ...: This Function is a substitute for subst.exe
; Syntax.........: _ChangeDriveLetter( "OldDriveLetter" , "NewDriveLetter" )
; Parameters ....: OldDriveLetter   - Single character Drive Letter to be identified actual Drive.
;                  NewDriveLetter   - Single character Drive Letter to be set the new Drive.
; Return values .: Success    - Returns 1
;              Failure    - Returns 0 and set @error to:
;                   1    = Specified Drive Letter is wrong.
;                   2    = Specified Drive letter is not currently assigned.
;                   3    = Specified New Drive letter is alread in use.
;                   4   = Unable to get volume name for drive letter.
;                   5   = Unable to delete mount point.
;                   6   = Could not set volume mount point.
; Author ........: jscript
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........;
; Example .......; _ChangeDriveLetter( "D", "H" )
; ===============================================================================================================================
Func _ChangeDriveLetter($sOldLetter, $sNewLetter)
   Local $DDD_EXACT_MATCH_ON_REMOVE = 0x00000004
   Local $DDD_NO_BROADCAST_SYSTEM = 0x00000008
   Local $DDD_RAW_TARGET_PATH = 0x00000001
   Local $DDD_REMOVE_DEFINITION = 0x00000002
   Local $sVolumeName

   ; Remove any white spaces...
   $sOldLetter = StringStripWS($sOldLetter, 8)
   $sNewLetter = StringStripWS($sNewLetter, 8)

   ; It should be a single character and can't be null string
   If StringLen($sOldLetter) <> 1 Then Return SetError(1, 0, 0)
   If StringLen($sNewLetter) <> 1 Then Return SetError(1, 0, 0)

   ; Requires ':' at the end of drive letter...
   $sOldLetter = $sOldLetter & ":"
   $sNewLetter = $sNewLetter & ":"

   ; Check if specified Old drive letter is currently assigned.
   If DriveStatus($sOldLetter) = "INVALID" Then Return SetError(2, 0, 0)

   ; Check if specified New drive letter is alread in use.
   If FileExists($sNewLetter) Then Return SetError(3, 0, 0)

   ; Get volume name (requires trailing backslash)
   $sVolumeName = __GetVolumeName($sOldLetter & "\")
   If $sVolumeName = "" Then Return SetError(4, 0, 0)

   ; Remove current mount point (requires trailing backslash)
   $hKernel = DllOpen("Kernel32.dll")
   $aRet = DllCall("Kernel32.dll", "BOOL", "DeleteVolumeMountPoint", "str", $sOldLetter & "\")
   DllClose($hKernel)
   If Not IsArray($aRet) Or $aRet[0] = 0 Then Return SetError(5, 0, 0)

   ; Set new mount point (requires trailing backslash)
   $hKernel = DllOpen("Kernel32.dll")
   $aRet = DllCall("Kernel32.dll", "BOOL", "SetVolumeMountPoint", "str", $sNewLetter & "\", "str", $sVolumeName)
   DllClose($hKernel)
   If Not IsArray($aRet) Or $aRet[0] = 0 Then Return SetError(6, 0, 0)

   Return SetError(0, 0, 1)
EndFunc  ;==>_ChangeDriveLetter

; #FUNCTION# ====================================================================================================================
; Name...........: _VirtualDrive
; Description ...: This Function is a substitute for subst.exe
; Syntax.........: _VirtualDrive( "DriveLetter" , "TargetPath" [, Unmount ] )
; Parameters ....: DriveLetter   - Single character Drive Letter to be used for Virtual Drive.
;                  TargetPath   - Full Path of Folder which is to be mounted as Drive.
;                  Unmount      - [optional] As the name says, it will unmount specified Drive Letter if set as True.
; Return values .: Success    - Returns 1
;              Failure    - Returns 0 and set @error to:
;                   1    = Specified Drive Letter is wrong.
;                   2    = Specified mount path is wrong or does not point to a valid Windows Folder item.
;                   3    = Specified Drive letter is not available to mount virtual drive.
;                   4   = Try to Unmont an "Unknown" driver.
; Author ........: Ruturaj and translate to AutoIt by jscript
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: _VirtualDrive("x", @ProgramFilesDir & "\AutoIt3")
; ===============================================================================================================================
Func _VirtualDrive($sDriveLetter, $sTargetPath, $fUnmount = False)
   Local $DDD_REMOVE_DEFINITION = 0x00000002, $sDriveType, $aRet

   ; Remove any white spaces...
   $sDriveLetter = StringStripWS($sDriveLetter, 8)

   ; It should be a single character and can't be null string
   If StringLen($sDriveLetter) <> 1 Then Return SetError(1, 0, 0)

   ; Check if specified Target Path is correct and exists...
   If Not FileExists($sTargetPath) Then Return SetError(2, 0, 0)

   ; DefineDosDevice requires ':' at the end of drive letter...
   $sDriveLetter = $sDriveLetter & ":"

   ; Oops! For DriveGetType, we need to append a \ to Drive Letter. Let us check if specified Drive Letter is available...
   $sDriveType = DriveGetType($sDriveLetter & "\")
   If @error Then $sDriveType = "Unknown"

   ; Only Unknown type of Drive letters are allowed to use for Virtual Drive for obvious reasons...
   Switch $sDriveType
      ; Virtual Drive, when mounted successfully, is recognized as Fixed Drive. So, here we will implement the code for Unmount...
      Case "Fixed"
         If Not $fUnmount Then Return SetError(3, 0, 0)
         $aRet = __DefineDosDevice($DDD_REMOVE_DEFINITION, $sDriveLetter, $sTargetPath)

         ; Here it means that the Drive Letter is available for us to mount Virtual Drive...
      Case "Unknown"
         If $fUnmount Then Return SetError(4, 0, 0)
         $aRet = __DefineDosDevice(0, $sDriveLetter, $sTargetPath)

      Case Else
         Return SetError(3, 0, 0)
   EndSwitch

   If Not $aRet Then Return SetError(3, 0, 0)
   Return SetError(0, 0, 1)
EndFunc  ;==>_VirtualDrive

; #INTERNAL_USE_ONLY# ===========================================================================================================
; Name...........: __DefineDosDevice
; Description ...: Defines, redefines, or deletes MS-DOS device names.
; Syntax.........: __DefineDosDevice( dwFlags, "DeviceName", "TargetPath" )
; Parameters ....: dwFlags     - The controllable aspects of the DefineDosDevice function.
;                        Link for values: http://msdn.microsoft.com/en-us/library/aa363904%28VS.85%29.aspx
;                  DeviceName - A pointer to an MS-DOS device name string specifying the device the function is
;                        defining, redefining, or deleting.
;                  TargetPath - A pointer to a path string that will implement this device.
;                        The string is an MS-DOS path string unless the DDD_RAW_TARGET_PATH flag is specified,
;                        in which case this string is a path string.
; Return values .: Success    - Returns 1
;              Failure    - Returns 0
; Author ........: jscript
; Modified.......:
; Remarks .......: http://msdn.microsoft.com/en-us/library/aa363904%28VS.85%29.aspx
; Related .......:
; Link ..........:
; Example .......: __DefineDosDevice( $DDD_RAW_TARGET_PATH, $DeviceName, $TargetPath )
; ===============================================================================================================================
Func __DefineDosDevice($dwFlags, $sDeviceName, $sTargetPath)
   Local $hKernel, $aRet

   $hKernel = DllOpen("Kernel32.dll")
   $aRet = DllCall($hKernel, "BOOL", "DefineDosDevice", "DWORD", $dwFlags, "str", $sDeviceName, "str", $sTargetPath)
   DllClose($hKernel)

   If Not IsArray($aRet) Or $aRet[0] = 0 Then Return 0
   Return 1
EndFunc  ;==>__DefineDosDevice

; #INTERNAL_USE_ONLY# ===========================================================================================================
; Name...........: __GetVolumeName
; Description ...: Obtain a volume GUID path for use with functions such as SetVolumeMountPoint.
; Syntax.........: __GetVolumeName( "VolumeMountPoint" )
; Parameters ....: VolumeMountPoint   - A pointer to a string that contains the path of a mounted folder (for example, Y:\MountX\)
;                           or a drive letter (for example, X:\). The string must end with a trailing backslash ('\').
; Return values .: Success    - Returns VolumeMountPoint like: "\\?\Volume{GUID}\"
;              Failure    - Returns ""
; Author ........: jscript
; Modified.......:
; Remarks .......: http://msdn.microsoft.com/en-us/library/aa364994%28v=VS.85%29.aspx
; Related .......:
; Link ..........:
; Example .......: __GetVolumeName( $sVolumeMountPoint )
; ===============================================================================================================================
Func __GetVolumeName($sVolumeMountPoint)
   Local $sVolumeName, $pBuffer, $aRet

   $sVolumeName = DllStructCreate("char[1024]")
   $pBuffer = DllStructGetSize($sVolumeName)
   $aRet = DllCall("Kernel32.dll", "long", "GetVolumeNameForVolumeMountPoint", _
         "str", $sVolumeMountPoint, _
         "ptr", DllStructGetPtr($sVolumeName), _
         "ptr*", DllStructGetPtr($pBuffer))

   If Not IsArray($aRet) Or $aRet[0] = 0 Then Return ""
   Return DllStructGetData($sVolumeName, 1)
EndFunc  ;==>__GetVolumeName



Se seu tópico for solucionado, edite-o novamente e clique no botão [Resolvido]
Se gostou de um "comentário", agradeça clicando no botão [Obrigado], nosso fórum ficará bem mais organizado!
Olá Convidado, há uma mensagem para você, clique aqui: Link!

______________________________Links úteis______________________________
http://sourcecode.forumais.com (Novo fórum de Códigos Fontes)
http://www.bioscentral.com/# (Atualizações, códigos de beep...)
http://www.alldatasheet.com/ (Ótimo banco de dados.)
Ver perfil do usuário