Jump to content

Script help with Copy UDF - (Moved)


Recommended Posts

I have been using a lot of scripting examples (new to Autoit)  from the site here to create a small app for folder copying to a selected USB drive.

Everything seems to be working fine with the exception of _copy_abort(). When copying the files, if i hit abort, it seems to reset the copy command.

i have tried to look at this from as many angles i can, but the answer is just out of my reach. Have i added too many IF statements, are they in the wrong order, am i doing the CASE wrong?

any help would be appreciated.

 

#include <guiconstants.au3>
#include <comboconstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include "Copy.au3"
#include <GuiListView.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>
#include <ButtonConstants.au3>

; Check to see if DLL is present in folder <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
If Not _Copy_OpenDll() Then
    MsgBox(16, '', 'DLL not found.')
    Exit
EndIf

; Create all global varibles <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Global $ESIS1 = "R432.2", $ESIS2 = "R500.2", $ESIS3 = "R510", $ESIS4 = "", $ESIS5 = "", $rESIS1 = $ESIS1, $rESIS2 = $ESIS2, $rESIS3 = $ESIS3, $rESIS4 = $ESIS4, $rESIS = $ESIS5, $array[5]
Global $hForm, $Input1, $Input2, $Button1, $Button2, $Button3, $Button4, $Label, $Data, $Msg, $Path, $Progress, $State, $Copy = False, $Pause = False
Global $Destination = '', $Source = '', $sDriveLabel1, $sDriveLabel2, $chkbox1

; Create array varibles <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$array[0] = $rESIS1
$array[1] = $rESIS2
$array[2] = $rESIS3
$array[3] = $rESIS4
$array[4] = $rESIS5

If _Singleton(@ScriptName, 1) = 0 Then Exit
AutoItSetOption("TrayIconHide", 1) ;0=show, 1=hide tray icon
AutoItSetOption("MustDeclareVars", 1)
AutoItSetOption("TrayMenuMode", 3)

Dim $aDriveName[1] ; declare array containing drive information

Local $bConfirm
; postion of main window at startup

Local $VirtualDesktopWidth = DllCall("user32.dll", "int", "GetSystemMetrics", "int", 78)
$VirtualDesktopWidth = $VirtualDesktopWidth[0]

;~ Dim $aExcludes

; First find all the USB drives on the system
Local $strComputer = "."
Local $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
Local $wmiDiskDrives = $objWMIService.ExecQuery _
        ("SELECT * FROM Win32_DiskDrive where InterfaceType = 'USB'")
If IsObj($wmiDiskDrives) Then
    For $wmiDiskDrive In $wmiDiskDrives
        If Not @Compiled Then ConsoleWrite("--------------------------------------" & @CRLF _
                 & "Caption: " & $wmiDiskDrive.Caption & @CRLF _
                 & "DeviceId: " & $wmiDiskDrive.DeviceID & @CRLF _
                 & "InterfaceType: " & $wmiDiskDrive.InterfaceType & @CRLF)
        ; Use the disk drive device id to find the associated partition(s)
        Local $wmiDiskPartitions = $objWMIService.ExecQuery _
                ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" & $wmiDiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition")
        If IsObj($wmiDiskPartitions) Then
            For $wmiDiskPartition In $wmiDiskPartitions
                ; Use partition device id to find logical disk
                Local $wmiLogicalDisks = $objWMIService.ExecQuery _
                        ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" & $wmiDiskPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition")
                If IsObj($wmiLogicalDisks) Then
                    For $wmiLogicalDisk In $wmiLogicalDisks
                        If Not @Compiled Then ConsoleWrite("Drive letter associated with disk Partition " _
                                 & $wmiDiskPartition.DeviceID & " ==>  " _
                                 & $wmiLogicalDisk.DeviceID & @CRLF)
                        ; add the drive info in the array
                        ReDim $aDriveName[UBound($aDriveName) + 1]
                        $aDriveName[UBound($aDriveName) - 1] = $wmiLogicalDisk.DeviceID & "     (" & DriveGetLabel($wmiLogicalDisk.DeviceID) & ")|" & $wmiDiskDrive.Caption
                    Next
                EndIf
            Next
        EndIf

    Next
EndIf
; delete the first array element.
_ArrayDelete($aDriveName, 0)

Local $Result
If UBound($aDriveName) = 0 Then
    ; no USB drives found
    Exit -1
Else
    ; Define heigth and width according to number of (drive) buttons to show
    If UBound($aDriveName) = 1 Then
        Local $_Width = 200
        Local $_Height = 90
    Else
        Local $_Width = 400
        Local $_Height = 20 + (Int((UBound($aDriveName) / 2) + 0.5) * 70)
    EndIf

    ; set the menu in the center of the mouse position
    Local $aPos = MouseGetPos()
    ; correct horizontal position of GUI
    $aPos[0] -= $_Width / 2
    If $aPos[0] < 0 Then
        $aPos[0] = 10
    ElseIf $aPos[0] > $VirtualDesktopWidth - $_Width Then
        $aPos[0] = $VirtualDesktopWidth - $_Width - 10
    EndIf

    ; correct vertical position of GUI
    $aPos[1] -= $_Height / 2
    If $aPos[1] < 0 Then
        $aPos[1] = 10
    ElseIf $aPos[1] > @DesktopHeight - $_Height Then
        $aPos[1] = @DesktopHeight - $_Height - 10
    EndIf

    Local $Choice = _ArrayMenu($aDriveName, "Click to Choose USB Drive letter or Press 'Escape'", $_Width, $_Height, $aPos[0], $aPos[1])

    If $Choice >= 0 Then
        ; get the drive VolumeName
        Local $sVolName = DllCall("Kernel32.dll", "long", "GetVolumeNameForVolumeMountPoint", "str", StringLeft($aDriveName[$Choice], 2) & "\", "str", "", "dword", 100)
        If Not @error And $sVolName[0] <> 0 Then
            If Not @Compiled Then ConsoleWrite(@ScriptLineNumber & " Volumename: " & StringLeft($aDriveName[$Choice], 2) & " " & $sVolName[2] & @CR)
            ; create the drive availability list
            ; empty CD drive to be resolved
            Local $aCDROM = DriveGetDrive("CDROM")
            Local $sDrive, $FirstDrive = "", $sAvailableDrives = "", $sNewDriveLetter
            For $i = 67 To 90
                $sDrive = Chr($i) & ":"
                If _ArraySearch($aCDROM, $sDrive, 1, 0, 0, 0, 1, 1) = -1 Then  ; exclude CDROM drives
                    If Not FileExists($sDrive) Then
                        If StringLen($FirstDrive) = 0 Then
                            $FirstDrive = $sDrive
                        Else
                            $sAvailableDrives &= $sDrive & "|"
                        EndIf
                    EndIf
                EndIf
            Next
            If Not @Compiled Then ConsoleWrite(@ScriptLineNumber & " " & StringLeft($aDriveName[$Choice], 2) & " - " & $sVolName[2] & " - " & $sNewDriveLetter & @CR)
            Global $Destination = StringLeft($aDriveName[$Choice], 2)
        EndIf
    EndIf
EndIf

; Create a single string to fill the combo box <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Global $sFullList = ""
For $i = 0 To UBound($array) - 1
    $sFullList &= "|" & $array[$i]
Next

; Create Gui <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$hForm = GUICreate('MyGUI', 360, 175)
$chkbox1 = GUICtrlCreateCheckbox("Format Drive?", 10, 20)
GUICtrlCreateLabel("Select ESIS", 10, 60)
Global $useESIS = GUICtrlCreateCombo("", 130, 60, 210, 10, $CBS_DROPDOWN)
GUICtrlSetData(-1, $sFullList)
$Label = GUICtrlCreateLabel('Preparing', 10, 90, 332, 14)
$Progress = GUICtrlCreateProgress(10, 110, 332, 16)
$Button3 = GUICtrlCreateButton('Copy', 135, 135, 80, 21)
$Button4 = GUICtrlCreateButton(';', 326, 138, 21, 21)
GUICtrlSetFont(-1, 10, 400, 0, 'Webdings')
GUICtrlSetState(-1, $GUI_DISABLE)
GUISetState()

While 1
    If GUICtrlRead($chkbox1) = $GUI_CHECKED Then
        $sDriveLabel1 = GUICtrlCreateInput("Drive Name", 130, 20, 210)
    EndIf
    If GUICtrlRead($useESIS) = "R432.2" Then
        $Source = "D:\Hotswap\R432\R432.2"
    EndIf
    If GUICtrlRead($useESIS) = "R500.2" Then
        $Source = "D:\Hotswap\R500\R500.2 ESIS"
    EndIf
    If GUICtrlRead($useESIS) = "R510" Then
        $Source = "D:\Hotswap\R510\R510 ESIS"
    EndIf
    If $Copy Then
        $State = _Copy_GetState()
        If $State[0] Then
            If $State[0] = -1 Then
                ; Preparing
            Else
                $Data = Round($State[1] / $State[2] * 100)
                If GUICtrlRead($Progress) <> $Data Then
                    GUICtrlSetData($Progress, $Data)
                EndIf
                $Data = StringRegExpReplace($State[6], '^.*\\', '')
                If GUICtrlRead($Label) <> $Data Then
                    GUICtrlSetData($Label, $Data)
                EndIf
            EndIf
        Else
            Switch $State[5]
                Case 0
                    GUICtrlSetData($Progress, 100)
                    MsgBox(64, '', 'Folder was successfully copied.', 0, $hForm)
                Case 1235
                    MsgBox(16, '', 'Folder copying was aborted.', 0, $hForm)
                Case Else
                    MsgBox(16, '', 'Folder was not copied.' & @CR & @CR & $State[5], 0, $hForm)
            EndSwitch
            GUICtrlSetState($Button1, $GUI_ENABLE)
            GUICtrlSetState($Button2, $GUI_ENABLE)
            GUICtrlSetState($Button4, $GUI_DISABLE)
            GUICtrlSetData($Progress, 0)
            GUICtrlSetData($Label, '')
            GUICtrlSetData($Button3, 'Copy')
            GUICtrlSetData($Button4, ';')
            $Copy = 0
        EndIf
    EndIf
    $Msg = GUIGetMsg()
    Switch $Msg
        Case $GUI_EVENT_CLOSE
            ExitLoop

        Case $Button3
            If $Copy Then
                _Copy_Abort()
            Else
                If (Not $Source) Or (Not $Destination) Then
                    MsgBox(16, '', 'The source and destination folders must be specified.', 0, $hForm)
                    ContinueLoop
                EndIf
                If GUICtrlRead($chkbox1) = $GUI_CHECKED Then
                    $sDriveLabel2 = GUICtrlRead($sDriveLabel1)
                    GUICtrlSetData($Label, 'Formatting...')
                    Sleep(500)
                    RunWait(@ComSpec & ' /c ' & 'format ' & StringLeft($aDriveName[$Choice], 2) & ' /FS:NTFS /q /v:"' & $sDriveLabel2 & '" /y', @WindowsDir, @SW_HIDE)
                    MsgBox(0, "Test", 'format ' & StringLeft($aDriveName[$Choice], 2) & ' /FS:NTFS /q /v:"' & $sDriveLabel2 & '" /y')
                EndIf
                $Path = $Destination & '\' & StringRegExpReplace($Source, '^.*\\', '')
                If FileExists($Path) Then
                    If MsgBox(51, 'Copy', $Path & ' already exists.' & @CR & @CR & 'Do you want to merge folders?', 0, $hForm) <> 6 Then
                        ContinueLoop
                    EndIf
                EndIf
            EndIf
            GUICtrlSetState($Button1, $GUI_DISABLE)
            GUICtrlSetState($Button2, $GUI_DISABLE)
            GUICtrlSetState($Button4, $GUI_ENABLE)
            GUICtrlSetData($Label, 'Preparing...')
            GUICtrlSetData($Button3, 'Abort')
            _Copy_CopyDir($Source, $Path, 0, 0, 0, '_Copy_CallbackDlg', $hForm)
            $Copy = 1

        Case $Button4
            $Pause = Not $Pause
            If $Pause Then
                GUICtrlSetData($Button4, '4')
            Else
                GUICtrlSetData($Button4, ';')
            EndIf
            _Copy_Pause($Pause)
    EndSwitch
WEnd
GUIDelete()

#FUNCTION# ==============================================================
Func _ArrayMenu($Menu_array, $WinTitle = "", $_Width = Default, $_Height = Default, $_left = Default, $_top = Default)
    Local $aButton[UBound($Menu_array)]

    Local $mainmsg
    Local $Menu_Window = GUICreate($WinTitle, $_Width, $_Height, $_left, $_top, $WS_DLGFRAME, BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST))
    Local $size = WinGetClientSize($Menu_Window)

    For $i = 0 To UBound($Menu_array) - 1
        Local $Label = StringLeft($Menu_array[$i], StringInStr($Menu_array[$i], "|") - 1) & @CR & StringTrimLeft($Menu_array[$i], StringInStr($Menu_array[$i], "|"))
        If $i / 2 = Int($i / 2) Then
            ; right button
            $aButton[$i] = GUICtrlCreateButton($Label, 1, (Int($i / 2) * 70) + 1, 200, 70, BitOR($BS_MULTILINE, $BS_CENTER, $BS_VCENTER))
        Else
            ; left button
            $aButton[$i] = GUICtrlCreateButton($Label, 201, (Int($i / 2) * 70) + 1, 200, 70, BitOR($BS_MULTILINE, $BS_CENTER, $BS_VCENTER))
        EndIf
    Next

    GUISetState(@SW_SHOW, $Menu_Window)

    While 1
        $mainmsg = GUIGetMsg()

        For $i = 0 To UBound($Menu_array) - 1
            If $mainmsg = $aButton[$i] Then
                GUIDelete($Menu_Window)
                Return $i
            EndIf
        Next

        If $mainmsg = $GUI_EVENT_CLOSE Then
            GUIDelete($Menu_Window)
            Return -1
        EndIf
    WEnd

EndFunc

 

Link to comment
Share on other sites

  • Moderators

Moved to the appropriate forum.

Moderation Team

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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...