Jump to content
Sign in to follow this  
llewxam

I'm having trouble pausing this script

Recommended Posts

llewxam

I know there are tons of good, free file syncing apps out there, but for fun made my own. I'd like to have a way to pause, then optionally quit, the sync but am not getting it right. What I have is:

#include <GUIConstantsEx.au3> ;needed for $GUI_EVENT_CLOSE, $GUI_DISABLE, $GUI_UNCHECKED
#include <String.au3> ;needed for _StringAddThousandsSep
#include <WindowsConstants.au3> ;needed for $DT_End_Ellipsis

global $sourcelist,$sync,$howmany,$howbig
$temp=""
$howmany=0
$sync=0

$main=GUICreate("File Syncer",400,300)
$sourceinfo=GUICtrlCreateLabel("",10,10,380,20)
$info=GUICtrlCreateLabel("",260,10,380,20)
$fileinfo=GUICtrlCreateLabel("",10,70,380,20,$DT_END_ELLIPSIS)
$percinfo=GUICtrlCreateLabel("",10,90,380,20)
$copyprogress=GUICtrlCreateProgress(10,130,380,20)
$progressbar=GUICtrlCreateProgress(10,200,380,20)
$copyinfo=GUICtrlCreateLabel("",10,170,380,20,$DT_END_ELLIPSIS)
$sourceinput=GUICtrlCreateInput("Type The Source Or Browse Above",10,260,180)
$targetinput=GUICtrlCreateInput("Type The Target Or Browse Above",210,260,180)
$sourcebutton=GUICtrlCreateButton("Source",60,230,80)
$destinationbutton=GUICtrlCreateButton("Destination",260,230,80)
$gobutton=GUICtrlCreateButton("GO",160,230,80)
GUISetState(@sw_show,$main)

Dim $AccelKeys[1][2]=[["{ENTER}", $gobutton]]
GUISetAccelerators($AccelKeys)

Do
    $msg=GUIGetMsg()    
    if $msg=$sourcebutton then guictrlsetdata ($sourceinput,FileSelectFolder("Select folder to sync",""))
    if $msg=$destinationbutton then GUICtrlSetData ($targetinput,FileSelectFolder("Select where to place the files","",1))
    if $msg=$gobutton and GUICtrlRead($sourceinput)<>"" and GUICtrlRead($sourceinput)<>"Type The Source Or Browse Above" and GUICtrlRead($targetinput)<>"" and GUICtrlRead($targetinput)<>"Type The Target Or Browse Above" then ExitLoop
    if $msg=$gui_event_close then Exit
until 1=2

ControlFocus("","",$gobutton)
GUICtrlSetState($sourcebutton,$gui_disable)
GUICtrlSetState($destinationbutton,$gui_disable)
GUICtrlSetState($gobutton,$gui_disable)
GUICtrlSetState($sourceinput,$gui_disable)
GUICtrlSetState($targetinput,$gui_disable)
$basedir=GUICtrlRead($sourceinput)
$target=GUICtrlRead($targetinput)
$trimamount=stringlen($basedir)+1
$begin=TimerInit()
theheart($basedir)
GUICtrlSetData($sourceinfo,"Finished Enumeration: "&_StringAddThousandsSep($howmany))
$sourcelist=StringSplit($temp,chr(1)) ;$temp is all file names & relative paths, see below; done to avoid _ArrayAdd
GUICtrlSetData($info,"Checking For Files To Sync")
;$stop=GUICtrlCreateCheckbox("Stop",180,160)

$total=$sourcelist[0]-1
for $lookatsource=1 to $total
;   if GUICtrlRead($stop)==1 then 
;       $quit=msgbox(36,"Paused","Quit Now?")
;       if $quit=6 then exit
;       if $quit=7 then GUICtrlSetState($Stop,$gui_unchecked)   
;   EndIf   
    if FileExists($target&"\"&$sourcelist[$lookatsource]) then  
        GUICtrlSetData($fileinfo,$sourcelist[$lookatsource])
        GUICtrlSetData($percinfo,"("&_StringAddThousandsSep($lookatsource)&"/"&_StringAddThousandsSep($total)&")")
        GUICtrlSetData($progressbar,$lookatsource/$total*99)
    Else
        GUICtrlSetData($fileinfo,$sourcelist[$lookatsource])
        GUICtrlSetData($percinfo,"("&_StringAddThousandsSep($lookatsource)&"/"&_StringAddThousandsSep($total)&")")
        GUICtrlSetData($progressbar,$lookatsource/$total*99)
        _CopyWithProgress($basedir&"\"&$sourcelist[$lookatsource], $target&"\"&$sourcelist[$lookatsource])
        $sync+=1
    EndIf
Next    

MsgBox(0,"Finished in "&_StringAddThousandsSep(round((TimerDiff($begin)/1000),2))&" Seconds","Synced "&_StringAddThousandsSep($sync)&" Files")
Exit



func theheart($basedir)
    
    $basesearch = fileFindFirstFile($basedir & "\*.*")
    if $basesearch==-1 then return 0 ; specified folder is empty!
    while @error<>1
        $basefile = fileFindNextFile($basesearch)
        ; skip these
        if $basefile=="." or $basefile==".." or $basefile=="" then
            continueLoop
        endIf
        ; if it's a dir then call this function again (nesting the function is clever ;)
        $formattedfile=$basedir&"\"&$basefile
        if stringInStr(fileGetAttrib($formattedfile), "D") > 0 then
            
            theheart($formattedfile)
            
        else
            
            ; Files we need to deal with

            $filename=StringTrimLeft($formattedfile,$trimamount)
            $temp=$temp&$filename&chr(1) ;makes a string with all results mashed together
            $howmany+=1
        EndIf
    wEnd
    fileClose($basesearch)
    return 1
endFunc





Func _CopyWithProgress($inSource, $inDest)
    
; Function to copy a file showing a realtime progress bar
;
; By Paul Niven (NiVZ)
;
; 05/11/2008
;
; Parameters:
;
; $inSource  -   Full path to source file
; $inDest   -    Full path to destination file (structure will be created if it does not exist
;
; Returns 0 if copy is successful and 1 if it fails
;
;
; Updates:
;
; 06/11/2008 - Now shows SOURCE and DEST on seperate lines while copying
;           - Now uses Kernal32 and DllCallBackRegister to provide faster copying
;             Kernal32 and DllCallBackRegister taken from _MultiFileCopy.au3
;             See: http://www.autoit.de/index.php?page=Thread&postID=58875
      

    If Not IsDeclared("callback") Then Local $callback = DllCallbackRegister('__Progress', 'int', 'uint64;uint64;uint64;uint64;dword;dword;ptr;ptr;str')
    If Not IsDeclared("ptr") Then Local $ptr = DllCallbackGetPtr($callback)

    $DestDir = StringLeft($inDest, StringInStr($inDest, "\", "", -1))

    If Not FileExists($DestDir) Then DirCreate($DestDir)
    
    $ret = DllCall('kernel32.dll', 'int', 'CopyFileExA', 'str', $inSource, 'str', $inDest, 'ptr', $ptr, 'str', '', 'int', 0, 'int', 0)

    If $ret[0] <> 0 Then
    ; Success
        Return 1
    Else
    ; Fail
        Return 0
    EndIf
    
EndFunc


Func __Progress($FileSize, $BytesTransferred, $StreamSize, $StreamBytesTransferred, $dwStreamNumber, $dwCallbackReason, $hSourceFile, $hDestinationFile, $lpData)
    
    GUICtrlSetData($copyprogress,Round($BytesTransferred / $FileSize * 100, 0))
    
EndFunc

The commented code is what I'd like, though better yet a button, I just thought (for some reason) that a checkbox would catch a mouseclick and check the box, and that would get picked up on the next file. Any thoughts?

Thanks

Ian


My projects:

  • IP Scanner - Multi-threaded ping tool to scan your available networks for used and available IP addresses, shows ping times, resolves IPs in to host names, and allows individual IPs to be pinged.
  • INFSniff - Great technicians tool - a tool which scans DriverPacks archives for INF files and parses out the HWIDs to a database file, and rapidly scans the local machine's HWIDs, searches the database for matches, and installs them.
  • PPK3 (Persistent Process Killer V3) - Another for the techs - suppress running processes that you need to keep away, helpful when fighting spyware/viruses.
  • Sync Tool - Folder sync tool with lots of real time information and several checking methods.
  • USMT Front End - Front End for Microsoft's User State Migration Tool, including all files needed for USMT 3.01 and 4.01, 32 bit and 64 bit versions.
  • Audit Tool - Computer audit tool to gather vital hardware, Windows, and Office information for IT managers and field techs. Capabilities include creating a customized site agent.
  • CSV Viewer - Displays CSV files with automatic column sizing and font selection. Lines can also be copied to the clipboard for data extraction.
  • MyDirStat - Lists number and size of files on a drive or specified path, allows for deletion within the app.
  • 2048 Game - My version of 2048, fun tile game.
  • Juice Lab - Ecigarette liquid making calculator.
  • Data Protector - Secure notes to save sensitive information.
  • VHD Footer - Add a footer to a forensic hard drive image to allow it to be mounted or used as a virtual machine hard drive.
  • Find in File - Searches files containing a specified phrase.

Share this post


Link to post
Share on other sites
WolfWorld

Hi, I look into it and found a bug.

First of all I fixed line 34

From

if $msg=$gobutton and GUICtrlRead($sourceinput)<>"" and GUICtrlRead($sourceinput)<>"Type The Source Or Browse Above" and GUICtrlRead($targetinput)<>"" and GUICtrlRead($targetinput)<>"Type The Target Or Browse Above" then ExitLoop     if $msg=$gui_event_close then Exit

To

if $msg=$gobutton and FileExists(GUICtrlRead($sourceinput)) and FileExists(GUICtrlRead($targetinput)) then ExitLoop ;//Change to File Exists

This would make sure the file exists

Next is the GUI overlap between the $copyinfo button and the checkbox

From

$stop=GUICtrlCreateCheckbox("Stop",180,160)

To

$stop=GUICtrlCreateCheckBox("Stop",135,150);//GUI button overlap with $copyinfo

And Change the code

From

if GUICtrlRead($stop)==1 then 
       $quit=msgbox(36,"Paused","Quit Now?")
       if $quit=6 then exit
       if $quit=7 then GUICtrlSetState($Stop,$gui_unchecked)   
   EndIf

To

While GUICtrlRead($stop) == 1
;Will add quit later on
        Sleep(1000)
    WEnd

And added the quit button on line 53

$quit = GUICtrlCreateCheckbox("Quit", 180, 150);//Added quit button

And the script below the Stop script

If GUICtrlRead($quit) == 1 Then Exit

This is added to two places to make sure when you want to quit. It will quit.

Done.

#include <GUIConstantsEx.au3> ;needed for $GUI_EVENT_CLOSE, $GUI_DISABLE, $GUI_UNCHECKED
#include <String.au3> ;needed for _StringAddThousandsSep
#include <WindowsConstants.au3> ;needed for $DT_End_Ellipsis

Global $sourcelist, $sync, $howmany, $howbig
$temp = ""
$howmany = 0
$sync = 0

$main = GUICreate("File Syncer", 400, 300)
$sourceinfo = GUICtrlCreateLabel("", 10, 10, 380, 20)
$info = GUICtrlCreateLabel("", 260, 10, 380, 20)
$fileinfo = GUICtrlCreateLabel("", 10, 70, 380, 20, $DT_END_ELLIPSIS)
$percinfo = GUICtrlCreateLabel("", 10, 90, 380, 20)
$copyprogress = GUICtrlCreateProgress(10, 130, 380, 20)
$progressbar = GUICtrlCreateProgress(10, 200, 380, 20)
$copyinfo = GUICtrlCreateLabel("", 10, 170, 380, 20, $DT_END_ELLIPSIS)
$sourceinput = GUICtrlCreateInput("Type The Source Or Browse Above", 10, 260, 180)
$targetinput = GUICtrlCreateInput("Type The Target Or Browse Above", 210, 260, 180)
$sourcebutton = GUICtrlCreateButton("Source", 60, 230, 80)
$destinationbutton = GUICtrlCreateButton("Destination", 260, 230, 80)
$gobutton = GUICtrlCreateButton("GO", 160, 230, 80)

GUISetBkColor(0xFFFFF)
GUISetState(@SW_SHOW, $main)

Dim $AccelKeys[1][2] = [["{ENTER}", $gobutton]]
GUISetAccelerators($AccelKeys)

Do
    $msg = GUIGetMsg()
    If $msg = $sourcebutton Then GUICtrlSetData($sourceinput, FileSelectFolder("Select folder to sync", ""))
    If $msg = $destinationbutton Then GUICtrlSetData($targetinput, FileSelectFolder("Select where to place the files", "", 1))
    If $msg = $gobutton And FileExists(GUICtrlRead($sourceinput)) And FileExists(GUICtrlRead($targetinput)) Then ExitLoop ;//Change to File Exists
    If $msg = $gui_event_close Then Exit
Until 1 = 2

ControlFocus("", "", $gobutton)
GUICtrlSetState($sourcebutton, $gui_disable)
GUICtrlSetState($destinationbutton, $gui_disable)
GUICtrlSetState($gobutton, $gui_disable)
GUICtrlSetState($sourceinput, $gui_disable)
GUICtrlSetState($targetinput, $gui_disable)
$basedir = GUICtrlRead($sourceinput)
$target = GUICtrlRead($targetinput)
$trimamount = StringLen($basedir) + 1
$begin = TimerInit()
theheart($basedir)
GUICtrlSetData($sourceinfo, "Finished Enumeration: " & _StringAddThousandsSep($howmany))
$sourcelist = StringSplit($temp, Chr(1)) ;$temp is all file names & relative paths, see below; done to avoid _ArrayAdd
GUICtrlSetData($info, "Checking For Files To Sync")
$stop = GUICtrlCreateCheckbox("Stop", 135, 150);//GUI button overlap with $copyinfo
$quit = GUICtrlCreateCheckbox("Quit", 180, 150);//Added quit button

$total = $sourcelist[0] - 1
For $lookatsource = 1 To $total
    While GUICtrlRead($stop) == 1
        Sleep(1000)
        If GUICtrlRead($quit) == 1 Then Exit
    WEnd
    If GUICtrlRead($quit) == 1 Then Exit
    If FileExists($target & "\" & $sourcelist[$lookatsource]) Then
        GUICtrlSetData($fileinfo, $sourcelist[$lookatsource])
        GUICtrlSetData($percinfo, "(" & _StringAddThousandsSep($lookatsource) & "/" & _StringAddThousandsSep($total) & ")")
        GUICtrlSetData($progressbar, $lookatsource / $total * 99)
    Else
        GUICtrlSetData($fileinfo, $sourcelist[$lookatsource])
        GUICtrlSetData($percinfo, "(" & _StringAddThousandsSep($lookatsource) & "/" & _StringAddThousandsSep($total) & ")")
        GUICtrlSetData($progressbar, $lookatsource / $total * 99)
        _CopyWithProgress($basedir & "\" & $sourcelist[$lookatsource], $target & "\" & $sourcelist[$lookatsource])
        $sync += 1
    EndIf
Next

MsgBox(0, "Finished in " & _StringAddThousandsSep(Round((TimerDiff($begin) / 1000), 2)) & " Seconds", "Synced " & _StringAddThousandsSep($sync) & " Files")
Exit



Func theheart($basedir)

    $basesearch = FileFindFirstFile($basedir & "\*.*")
    If $basesearch == -1 Then Return 0 ; specified folder is empty!
    While @error <> 1
        $basefile = FileFindNextFile($basesearch)
        ; skip these
        If $basefile == "." Or $basefile == ".." Or $basefile == "" Then
            ContinueLoop
        EndIf
        ; if it's a dir then call this function again (nesting the function is clever ;)
        $formattedfile = $basedir & "\" & $basefile
        If StringInStr(FileGetAttrib($formattedfile), "D") > 0 Then

            theheart($formattedfile)

        Else

            ; Files we need to deal with

            $filename = StringTrimLeft($formattedfile, $trimamount)
            $temp = $temp & $filename & Chr(1) ;makes a string with all results mashed together
            $howmany += 1
        EndIf
    WEnd
    FileClose($basesearch)
    Return 1
EndFunc   ;==>theheart





Func _CopyWithProgress($inSource, $inDest)

    ; Function to copy a file showing a realtime progress bar
    ;
    ; By Paul Niven (NiVZ)
    ;
    ; 05/11/2008
    ;
    ; Parameters:
    ;
    ; $inSource  -   Full path to source file
    ; $inDest   -    Full path to destination file (structure will be created if it does not exist
    ;
    ; Returns 0 if copy is successful and 1 if it fails
    ;
    ;
    ; Updates:
    ;
    ; 06/11/2008 - Now shows SOURCE and DEST on seperate lines while copying
    ;           - Now uses Kernal32 and DllCallBackRegister to provide faster copying
    ;             Kernal32 and DllCallBackRegister taken from _MultiFileCopy.au3
    ;             See: http://www.autoit.de/index.php?page=Thread&postID=58875


    If Not IsDeclared("callback") Then Local $callback = DllCallbackRegister('__Progress', 'int', 'uint64;uint64;uint64;uint64;dword;dword;ptr;ptr;str')
    If Not IsDeclared("ptr") Then Local $ptr = DllCallbackGetPtr($callback)

    $DestDir = StringLeft($inDest, StringInStr($inDest, "\", "", -1))

    If Not FileExists($DestDir) Then DirCreate($DestDir)

    $ret = DllCall('kernel32.dll', 'int', 'CopyFileExA', 'str', $inSource, 'str', $inDest, 'ptr', $ptr, 'str', '', 'int', 0, 'int', 0)

    If $ret[0] <> 0 Then
        ; Success
        Return 1
    Else
        ; Fail
        Return 0
    EndIf

EndFunc   ;==>_CopyWithProgress


Func __Progress($FileSize, $BytesTransferred, $StreamSize, $StreamBytesTransferred, $dwStreamNumber, $dwCallbackReason, $hSourceFile, $hDestinationFile, $lpData)

    GUICtrlSetData($copyprogress, Round($BytesTransferred / $FileSize * 100, 0))

EndFunc   ;==>__Progress
Edited by athiwatc

Share this post


Link to post
Share on other sites
llewxam

Hmm, thanks for the help, and it does work like you said, but I'm confused... What do you mean by "Next is the GUI overlap between the $copyinfo button and the checkbox"? The checkbox is nowhere near $copyinfo, but it is next to $copyprogress, and when the checkbox is moved a bit lower it doesn't work. Could you explain what you meant? I've looked at it several times and just kinda don't get why it works now... Is it strictly the position of the box overlapping with another element?

I can't believe I left out the file enumeration from the code I posted earlier, so this includes the help you gave on getting the button to be recognized mid-copy, but takes it back to what I had in mind for it.

#include <GUIConstantsEx.au3> ;needed for $GUI_EVENT_CLOSE, $GUI_DISABLE, $GUI_UNCHECKED
#include <String.au3> ;needed for _StringAddThousandsSep
#include <WindowsConstants.au3> ;needed for $DT_End_Ellipsis

Global $sourcelist, $sync, $howmany, $howbig
$temp = ""
$howmany = 0
$sync = 0

$main = GUICreate("File Syncer", 400, 300)
$sourceinfo = GUICtrlCreateLabel("", 10, 10, 380, 20)
$info = GUICtrlCreateLabel("", 260, 10, 380, 20)
$fileinfo = GUICtrlCreateLabel("", 10, 70, 380, 20, $DT_END_ELLIPSIS)
$percinfo = GUICtrlCreateLabel("", 10, 90, 380, 20)
$copyprogress = GUICtrlCreateProgress(10, 130, 380, 20)
$progressbar = GUICtrlCreateProgress(10, 200, 380, 20)
$copyinfo = GUICtrlCreateLabel("", 10, 170, 380, 20, $DT_END_ELLIPSIS)
$sourceinput = GUICtrlCreateInput("Type The Source Or Browse Above", 10, 260, 180)
$targetinput = GUICtrlCreateInput("Type The Target Or Browse Above", 210, 260, 180)
$sourcebutton = GUICtrlCreateButton("Source", 60, 230, 80)
$destinationbutton = GUICtrlCreateButton("Destination", 260, 230, 80)
$gobutton = GUICtrlCreateButton("GO", 160, 230, 80)

GUISetState(@SW_SHOW, $main)

Dim $AccelKeys[1][2] = [["{ENTER}", $gobutton]]
GUISetAccelerators($AccelKeys)

Do
    $msg = GUIGetMsg()
    If $msg = $sourcebutton Then GUICtrlSetData($sourceinput, FileSelectFolder("Select folder to sync", ""))
    If $msg = $destinationbutton Then GUICtrlSetData($targetinput, FileSelectFolder("Select where to place the files", "", 1))
    if $msg = $gobutton and FileExists(GUICtrlRead($sourceinput)) and FileExists(GUICtrlRead($targetinput)) then ExitLoop ;//Change to File Exists
    If $msg = $gui_event_close Then Exit
Until 1 = 2

ControlFocus("", "", $gobutton)
GUICtrlSetState($sourcebutton, $gui_disable)
GUICtrlSetState($destinationbutton, $gui_disable)
GUICtrlSetState($gobutton, $gui_disable)
GUICtrlSetState($sourceinput, $gui_disable)
GUICtrlSetState($targetinput, $gui_disable)
$basedir = GUICtrlRead($sourceinput)
$target = GUICtrlRead($targetinput)
$trimamount = StringLen($basedir) + 1
$begin = TimerInit()
theheart($basedir)
GUICtrlSetData($sourceinfo, "Finished Enumeration: " & _StringAddThousandsSep($howmany))
$sourcelist = StringSplit($temp, Chr(1)) ;$temp is all file names & relative paths, see below; done to avoid _ArrayAdd
GUICtrlSetData($info, "Checking For Files To Sync")
$pause = GUICtrlCreateCheckbox("Pause", 175, 150);//GUI button overlap with $copyinfo
$total = $sourcelist[0] - 1
For $lookatsource = 1 To $total
    While GUICtrlRead($pause) == 1
        $quit=msgbox(36,"Paused","Quit Now?")
        if $quit=6 then exit
        if $quit=7 then GUICtrlSetState($pause,$gui_unchecked)  
    WEnd
    If FileExists($target & "\" & $sourcelist[$lookatsource]) Then
        GUICtrlSetData($fileinfo, $sourcelist[$lookatsource])
        GUICtrlSetData($percinfo, "(" & _StringAddThousandsSep($lookatsource) & "/" & _StringAddThousandsSep($total) & ")")
        GUICtrlSetData($progressbar, $lookatsource / $total * 99)
    Else
        GUICtrlSetData($fileinfo, $sourcelist[$lookatsource])
        GUICtrlSetData($percinfo, "(" & _StringAddThousandsSep($lookatsource) & "/" & _StringAddThousandsSep($total) & ")")
        GUICtrlSetData($progressbar, $lookatsource / $total * 99)
        _CopyWithProgress($basedir & "\" & $sourcelist[$lookatsource], $target & "\" & $sourcelist[$lookatsource])
        $sync += 1
    EndIf
Next

MsgBox(0, "Finished in " & _StringAddThousandsSep(Round((TimerDiff($begin) / 1000), 2)) & " Seconds", "Synced " & _StringAddThousandsSep($sync) & " Files")
Exit



Func theheart($basedir)

    $basesearch = FileFindFirstFile($basedir & "\*.*")
    If $basesearch == -1 Then Return 0 ; specified folder is empty!
    While @error <> 1
        $basefile = FileFindNextFile($basesearch)
        ; skip these
        If $basefile == "." Or $basefile == ".." Or $basefile == "" Then
            ContinueLoop
        EndIf
        ; if it's a dir then call this function again (nesting the function is clever ;)
        $formattedfile = $basedir & "\" & $basefile
        If StringInStr(FileGetAttrib($formattedfile), "D") > 0 Then

            theheart($formattedfile)

        Else

            ; Files we need to deal with

            $filename = StringTrimLeft($formattedfile, $trimamount)
            $temp = $temp & $filename & Chr(1) ;makes a string with all results mashed together
            $howmany += 1
            GUICtrlSetData($sourceinfo,"Enumerating Source Files: "&_StringAddThousandsSep($howmany))
        EndIf
    WEnd
    FileClose($basesearch)
    Return 1
EndFunc   ;==>theheart





Func _CopyWithProgress($inSource, $inDest)

    ; Function to copy a file showing a realtime progress bar
    ;
    ; By Paul Niven (NiVZ)
    ;
    ; 05/11/2008
    ;
    ; Parameters:
    ;
    ; $inSource  -   Full path to source file
    ; $inDest   -    Full path to destination file (structure will be created if it does not exist
    ;
    ; Returns 0 if copy is successful and 1 if it fails
    ;
    ;
    ; Updates:
    ;
    ; 06/11/2008 - Now shows SOURCE and DEST on seperate lines while copying
    ;           - Now uses Kernal32 and DllCallBackRegister to provide faster copying
    ;             Kernal32 and DllCallBackRegister taken from _MultiFileCopy.au3
    ;             See: http://www.autoit.de/index.php?page=Thread&postID=58875


    If Not IsDeclared("callback") Then Local $callback = DllCallbackRegister('__Progress', 'int', 'uint64;uint64;uint64;uint64;dword;dword;ptr;ptr;str')
    If Not IsDeclared("ptr") Then Local $ptr = DllCallbackGetPtr($callback)

    $DestDir = StringLeft($inDest, StringInStr($inDest, "\", "", -1))

    If Not FileExists($DestDir) Then DirCreate($DestDir)

    $ret = DllCall('kernel32.dll', 'int', 'CopyFileExA', 'str', $inSource, 'str', $inDest, 'ptr', $ptr, 'str', '', 'int', 0, 'int', 0)

    If $ret[0] <> 0 Then
        ; Success
        Return 1
    Else
        ; Fail
        Return 0
    EndIf

EndFunc   ;==>_CopyWithProgress


Func __Progress($FileSize, $BytesTransferred, $StreamSize, $StreamBytesTransferred, $dwStreamNumber, $dwCallbackReason, $hSourceFile, $hDestinationFile, $lpData)

    GUICtrlSetData($copyprogress, Round($BytesTransferred / $FileSize * 100, 0))

EndFunc   ;==>__Progress

Thanks!

Ian


My projects:

  • IP Scanner - Multi-threaded ping tool to scan your available networks for used and available IP addresses, shows ping times, resolves IPs in to host names, and allows individual IPs to be pinged.
  • INFSniff - Great technicians tool - a tool which scans DriverPacks archives for INF files and parses out the HWIDs to a database file, and rapidly scans the local machine's HWIDs, searches the database for matches, and installs them.
  • PPK3 (Persistent Process Killer V3) - Another for the techs - suppress running processes that you need to keep away, helpful when fighting spyware/viruses.
  • Sync Tool - Folder sync tool with lots of real time information and several checking methods.
  • USMT Front End - Front End for Microsoft's User State Migration Tool, including all files needed for USMT 3.01 and 4.01, 32 bit and 64 bit versions.
  • Audit Tool - Computer audit tool to gather vital hardware, Windows, and Office information for IT managers and field techs. Capabilities include creating a customized site agent.
  • CSV Viewer - Displays CSV files with automatic column sizing and font selection. Lines can also be copied to the clipboard for data extraction.
  • MyDirStat - Lists number and size of files on a drive or specified path, allows for deletion within the app.
  • 2048 Game - My version of 2048, fun tile game.
  • Juice Lab - Ecigarette liquid making calculator.
  • Data Protector - Secure notes to save sensitive information.
  • VHD Footer - Add a footer to a forensic hard drive image to allow it to be mounted or used as a virtual machine hard drive.
  • Find in File - Searches files containing a specified phrase.

Share this post


Link to post
Share on other sites
WolfWorld

Hmm, thanks for the help, and it does work like you said, but I'm confused... What do you mean by "Next is the GUI overlap between the $copyinfo button and the checkbox"? The checkbox is nowhere near $copyinfo, but it is next to $copyprogress, and when the checkbox is moved a bit lower it doesn't work. Could you explain what you meant? I've looked at it several times and just kinda don't get why it works now... Is it strictly the position of the box overlapping with another element?

I can't believe I left out the file enumeration from the code I posted earlier, so this includes the help you gave on getting the button to be recognized mid-copy, but takes it back to what I had in mind for it.

#include <GUIConstantsEx.au3> ;needed for $GUI_EVENT_CLOSE, $GUI_DISABLE, $GUI_UNCHECKED
#include <String.au3> ;needed for _StringAddThousandsSep
#include <WindowsConstants.au3> ;needed for $DT_End_Ellipsis

Global $sourcelist, $sync, $howmany, $howbig
$temp = ""
$howmany = 0
$sync = 0

$main = GUICreate("File Syncer", 400, 300)
$sourceinfo = GUICtrlCreateLabel("", 10, 10, 380, 20)
$info = GUICtrlCreateLabel("", 260, 10, 380, 20)
$fileinfo = GUICtrlCreateLabel("", 10, 70, 380, 20, $DT_END_ELLIPSIS)
$percinfo = GUICtrlCreateLabel("", 10, 90, 380, 20)
$copyprogress = GUICtrlCreateProgress(10, 130, 380, 20)
$progressbar = GUICtrlCreateProgress(10, 200, 380, 20)
$copyinfo = GUICtrlCreateLabel("", 10, 170, 380, 20, $DT_END_ELLIPSIS)
$sourceinput = GUICtrlCreateInput("Type The Source Or Browse Above", 10, 260, 180)
$targetinput = GUICtrlCreateInput("Type The Target Or Browse Above", 210, 260, 180)
$sourcebutton = GUICtrlCreateButton("Source", 60, 230, 80)
$destinationbutton = GUICtrlCreateButton("Destination", 260, 230, 80)
$gobutton = GUICtrlCreateButton("GO", 160, 230, 80)

GUISetState(@SW_SHOW, $main)

Dim $AccelKeys[1][2] = [["{ENTER}", $gobutton]]
GUISetAccelerators($AccelKeys)

Do
    $msg = GUIGetMsg()
    If $msg = $sourcebutton Then GUICtrlSetData($sourceinput, FileSelectFolder("Select folder to sync", ""))
    If $msg = $destinationbutton Then GUICtrlSetData($targetinput, FileSelectFolder("Select where to place the files", "", 1))
    if $msg = $gobutton and FileExists(GUICtrlRead($sourceinput)) and FileExists(GUICtrlRead($targetinput)) then ExitLoop ;//Change to File Exists
    If $msg = $gui_event_close Then Exit
Until 1 = 2

ControlFocus("", "", $gobutton)
GUICtrlSetState($sourcebutton, $gui_disable)
GUICtrlSetState($destinationbutton, $gui_disable)
GUICtrlSetState($gobutton, $gui_disable)
GUICtrlSetState($sourceinput, $gui_disable)
GUICtrlSetState($targetinput, $gui_disable)
$basedir = GUICtrlRead($sourceinput)
$target = GUICtrlRead($targetinput)
$trimamount = StringLen($basedir) + 1
$begin = TimerInit()
theheart($basedir)
GUICtrlSetData($sourceinfo, "Finished Enumeration: " & _StringAddThousandsSep($howmany))
$sourcelist = StringSplit($temp, Chr(1)) ;$temp is all file names & relative paths, see below; done to avoid _ArrayAdd
GUICtrlSetData($info, "Checking For Files To Sync")
$pause = GUICtrlCreateCheckbox("Pause", 175, 150);//GUI button overlap with $copyinfo
$total = $sourcelist[0] - 1
For $lookatsource = 1 To $total
    While GUICtrlRead($pause) == 1
        $quit=msgbox(36,"Paused","Quit Now?")
        if $quit=6 then exit
        if $quit=7 then GUICtrlSetState($pause,$gui_unchecked)  
    WEnd
    If FileExists($target & "\" & $sourcelist[$lookatsource]) Then
        GUICtrlSetData($fileinfo, $sourcelist[$lookatsource])
        GUICtrlSetData($percinfo, "(" & _StringAddThousandsSep($lookatsource) & "/" & _StringAddThousandsSep($total) & ")")
        GUICtrlSetData($progressbar, $lookatsource / $total * 99)
    Else
        GUICtrlSetData($fileinfo, $sourcelist[$lookatsource])
        GUICtrlSetData($percinfo, "(" & _StringAddThousandsSep($lookatsource) & "/" & _StringAddThousandsSep($total) & ")")
        GUICtrlSetData($progressbar, $lookatsource / $total * 99)
        _CopyWithProgress($basedir & "\" & $sourcelist[$lookatsource], $target & "\" & $sourcelist[$lookatsource])
        $sync += 1
    EndIf
Next

MsgBox(0, "Finished in " & _StringAddThousandsSep(Round((TimerDiff($begin) / 1000), 2)) & " Seconds", "Synced " & _StringAddThousandsSep($sync) & " Files")
Exit



Func theheart($basedir)

    $basesearch = FileFindFirstFile($basedir & "\*.*")
    If $basesearch == -1 Then Return 0 ; specified folder is empty!
    While @error <> 1
        $basefile = FileFindNextFile($basesearch)
        ; skip these
        If $basefile == "." Or $basefile == ".." Or $basefile == "" Then
            ContinueLoop
        EndIf
        ; if it's a dir then call this function again (nesting the function is clever ;)
        $formattedfile = $basedir & "\" & $basefile
        If StringInStr(FileGetAttrib($formattedfile), "D") > 0 Then

            theheart($formattedfile)

        Else

            ; Files we need to deal with

            $filename = StringTrimLeft($formattedfile, $trimamount)
            $temp = $temp & $filename & Chr(1) ;makes a string with all results mashed together
            $howmany += 1
            GUICtrlSetData($sourceinfo,"Enumerating Source Files: "&_StringAddThousandsSep($howmany))
        EndIf
    WEnd
    FileClose($basesearch)
    Return 1
EndFunc   ;==>theheart





Func _CopyWithProgress($inSource, $inDest)

    ; Function to copy a file showing a realtime progress bar
    ;
    ; By Paul Niven (NiVZ)
    ;
    ; 05/11/2008
    ;
    ; Parameters:
    ;
    ; $inSource  -   Full path to source file
    ; $inDest   -    Full path to destination file (structure will be created if it does not exist
    ;
    ; Returns 0 if copy is successful and 1 if it fails
    ;
    ;
    ; Updates:
    ;
    ; 06/11/2008 - Now shows SOURCE and DEST on seperate lines while copying
    ;           - Now uses Kernal32 and DllCallBackRegister to provide faster copying
    ;             Kernal32 and DllCallBackRegister taken from _MultiFileCopy.au3
    ;             See: http://www.autoit.de/index.php?page=Thread&postID=58875


    If Not IsDeclared("callback") Then Local $callback = DllCallbackRegister('__Progress', 'int', 'uint64;uint64;uint64;uint64;dword;dword;ptr;ptr;str')
    If Not IsDeclared("ptr") Then Local $ptr = DllCallbackGetPtr($callback)

    $DestDir = StringLeft($inDest, StringInStr($inDest, "\", "", -1))

    If Not FileExists($DestDir) Then DirCreate($DestDir)

    $ret = DllCall('kernel32.dll', 'int', 'CopyFileExA', 'str', $inSource, 'str', $inDest, 'ptr', $ptr, 'str', '', 'int', 0, 'int', 0)

    If $ret[0] <> 0 Then
        ; Success
        Return 1
    Else
        ; Fail
        Return 0
    EndIf

EndFunc   ;==>_CopyWithProgress


Func __Progress($FileSize, $BytesTransferred, $StreamSize, $StreamBytesTransferred, $dwStreamNumber, $dwCallbackReason, $hSourceFile, $hDestinationFile, $lpData)

    GUICtrlSetData($copyprogress, Round($BytesTransferred / $FileSize * 100, 0))

EndFunc   ;==>__Progress

Thanks!

Ian

Okay in you original script try this

From

$copyinfo = GUICtrlCreateLabel("", 10, 170, 380, 20, $DT_END_ELLIPSIS)

To

$copyinfo = GUICtrlCreateLabel("Test overlap zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", 10, 170, 380, 20, $DT_END_ELLIPSIS)

And you will see that the zzzzzz goes over the checkbox

Remember even the text is not there, there is some kind of background there.

Edited by athiwatc

Share this post


Link to post
Share on other sites
llewxam

Thanks again for your help, I guess I'll just need to play with it more to really understand how/why this works, but it is great that it does!! >_< For now, that's good enough!

Here is another update, I put in some error checking.

#include <GUIConstantsEx.au3> ;needed for $GUI_EVENT_CLOSE, $GUI_DISABLE, $GUI_UNCHECKED
#include <String.au3> ;needed for _StringAddThousandsSep
#include <WindowsConstants.au3> ;needed for $DT_End_Ellipsis

Global $sourcelist, $sync, $howmany, $howbig
$temp = ""
$failed = ""
$howmany = 0
$sync = 0
$failedcount = 0

$main = GUICreate("File Syncer", 400, 300)
$sourceinfo = GUICtrlCreateLabel("", 10, 10, 380, 20)
$info = GUICtrlCreateLabel("", 260, 10, 380, 20)
$fileinfo = GUICtrlCreateLabel("", 10, 70, 380, 20, $DT_END_ELLIPSIS)
$percinfo = GUICtrlCreateLabel("", 10, 90, 380, 20)
$copyprogress = GUICtrlCreateProgress(10, 130, 380, 20)
$progressbar = GUICtrlCreateProgress(10, 200, 380, 20)
$copyinfo = GUICtrlCreateLabel("", 10, 170, 380, 20, $DT_END_ELLIPSIS)
$sourceinput = GUICtrlCreateInput("Type The Source Or Browse Above", 10, 260, 180)
$targetinput = GUICtrlCreateInput("Type The Target Or Browse Above", 210, 260, 180)
$sourcebutton = GUICtrlCreateButton("Source", 60, 230, 80)
$destinationbutton = GUICtrlCreateButton("Destination", 260, 230, 80)
$gobutton = GUICtrlCreateButton("GO", 160, 230, 80)
GUISetState(@SW_SHOW, $main)

Dim $AccelKeys[1][2] = [["{ENTER}", $gobutton]]
GUISetAccelerators($AccelKeys)

Do
    $msg = GUIGetMsg()
    If $msg = $sourcebutton Then GUICtrlSetData($sourceinput, FileSelectFolder("Select folder to sync", ""))
    If $msg = $destinationbutton Then GUICtrlSetData($targetinput, FileSelectFolder("Select where to place the files", "", 1))
    if $msg = $gobutton and FileExists(GUICtrlRead($sourceinput)) and FileExists(GUICtrlRead($targetinput)) then ExitLoop ;//Change to File Exists
    If $msg = $gui_event_close Then Exit
Until 1 = 2

ControlFocus("", "", $gobutton)
GUICtrlSetState($sourcebutton, $gui_disable)
GUICtrlSetState($destinationbutton, $gui_disable)
GUICtrlSetState($gobutton, $gui_disable)
GUICtrlSetState($sourceinput, $gui_disable)
GUICtrlSetState($targetinput, $gui_disable)
$basedir = GUICtrlRead($sourceinput)
$target = GUICtrlRead($targetinput)
$trimamount = StringLen($basedir) + 1
$begin = TimerInit()
theheart($basedir)
GUICtrlSetData($sourceinfo, "Finished Enumeration: " & _StringAddThousandsSep($howmany))
$sourcelist = StringSplit($temp, Chr(1)) ;$temp is all file names & relative paths, see below; done to avoid _ArrayAdd
GUICtrlSetData($info, "Checking For Files To Sync")
$pause = GUICtrlCreateCheckbox("Pause", 175, 150);//GUI button overlap with $copyinfo
$total = $sourcelist[0] - 1
For $lookatsource = 1 To $total
    While GUICtrlRead($pause) == 1
        $quit=msgbox(36,"Paused","Quit Now?")
        if $quit=6 then exit
        if $quit=7 then GUICtrlSetState($pause,$gui_unchecked)  
    WEnd
    If FileExists($target & "\" & $sourcelist[$lookatsource]) Then
        GUICtrlSetData($fileinfo, $sourcelist[$lookatsource])
        GUICtrlSetData($percinfo, "(" & _StringAddThousandsSep($lookatsource) & "/" & _StringAddThousandsSep($total) & ")")
        GUICtrlSetData($progressbar, $lookatsource / $total * 99)
    Else
        GUICtrlSetData($fileinfo, $sourcelist[$lookatsource])
        GUICtrlSetData($percinfo, "(" & _StringAddThousandsSep($lookatsource) & "/" & _StringAddThousandsSep($total) & ")")
        GUICtrlSetData($progressbar, $lookatsource / $total * 99)
        $copied=_CopyWithProgress($basedir & "\" & $sourcelist[$lookatsource], $target & "\" & $sourcelist[$lookatsource])
        if $copied=0 Then 
            $failed=$failed&$basedir & "\" & $sourcelist[$lookatsource]&chr(1)
            $failedcount+=1
        EndIf
        if $copied=1 then $sync += 1
    EndIf
Next

MsgBox(0, "Finished in " & _StringAddThousandsSep(Round((TimerDiff($begin) / 1000), 2)) & " Seconds", "Synced " & _StringAddThousandsSep($sync) & " Files With "&$failedcount&" Errors")
if $failedcount>0 then 
    $yesorno=msgbox(4,"See Failed?","Would you like to see the failed files?")
    if $yesorno=6 then
        $errors=GUICreate("Failed Files",@DesktopWidth,200)
        $showfailed=GUICtrlCreateList("",10,10,@DesktopWidth-20,180)
        GUISetState(@sw_show,$errors)
        $errorlist = StringSplit($failed, Chr(1))
        $total = $errorlist[0] - 1
        For $show = 1 To $total
            GUICtrlSetData($showfailed,$errorlist[$show])
        next
        Do
            $msg=GUIGetMsg()
            if $msg=$gui_event_close then Exit
        until 1=2
    EndIf
EndIf
Exit



Func theheart($basedir)

    $basesearch = FileFindFirstFile($basedir & "\*.*")
    If $basesearch == -1 Then Return 0 ; specified folder is empty!
    While @error <> 1
        $basefile = FileFindNextFile($basesearch)
        ; skip these
        If $basefile == "." Or $basefile == ".." Or $basefile == "" Then
            ContinueLoop
        EndIf
        ; if it's a dir then call this function again (nesting the function is clever ;)
        $formattedfile = $basedir & "\" & $basefile
        If StringInStr(FileGetAttrib($formattedfile), "D") > 0 Then

            theheart($formattedfile)

        Else

            ; Files we need to deal with

            $filename = StringTrimLeft($formattedfile, $trimamount)
            $temp = $temp & $filename & Chr(1) ;makes a string with all results mashed together
            $howmany += 1
            GUICtrlSetData($sourceinfo,"Enumerating Source Files: "&_StringAddThousandsSep($howmany))
        EndIf
    WEnd
    FileClose($basesearch)
    Return 1
EndFunc   ;==>theheart





Func _CopyWithProgress($inSource, $inDest)

    ; Function to copy a file showing a realtime progress bar
    ;
    ; By Paul Niven (NiVZ)
    ;
    ; 05/11/2008
    ;
    ; Parameters:
    ;
    ; $inSource  -   Full path to source file
    ; $inDest   -    Full path to destination file (structure will be created if it does not exist
    ;
    ; Returns 0 if copy is successful and 1 if it fails
    ;
    ;
    ; Updates:
    ;
    ; 06/11/2008 - Now shows SOURCE and DEST on seperate lines while copying
    ;           - Now uses Kernal32 and DllCallBackRegister to provide faster copying
    ;             Kernal32 and DllCallBackRegister taken from _MultiFileCopy.au3
    ;             See: http://www.autoit.de/index.php?page=Thread&postID=58875


    If Not IsDeclared("callback") Then Local $callback = DllCallbackRegister('__Progress', 'int', 'uint64;uint64;uint64;uint64;dword;dword;ptr;ptr;str')
    If Not IsDeclared("ptr") Then Local $ptr = DllCallbackGetPtr($callback)

    $DestDir = StringLeft($inDest, StringInStr($inDest, "\", "", -1))

    If Not FileExists($DestDir) Then DirCreate($DestDir)

    $ret = DllCall('kernel32.dll', 'int', 'CopyFileExA', 'str', $inSource, 'str', $inDest, 'ptr', $ptr, 'str', '', 'int', 0, 'int', 0)

    If $ret[0] <> 0 Then
        ; Success
        Return 1
    Else
        ; Fail
        Return 0
    EndIf

EndFunc   ;==>_CopyWithProgress


Func __Progress($FileSize, $BytesTransferred, $StreamSize, $StreamBytesTransferred, $dwStreamNumber, $dwCallbackReason, $hSourceFile, $hDestinationFile, $lpData)

    GUICtrlSetData($copyprogress, Round($BytesTransferred / $FileSize * 100, 0))

EndFunc   ;==>__Progress

Ian

EDIT: Fixed a slight "whoopsie" with the ending "if $failedcount>0 then" that caused an ugly termination if no errors were found...

Edited by llewxam

My projects:

  • IP Scanner - Multi-threaded ping tool to scan your available networks for used and available IP addresses, shows ping times, resolves IPs in to host names, and allows individual IPs to be pinged.
  • INFSniff - Great technicians tool - a tool which scans DriverPacks archives for INF files and parses out the HWIDs to a database file, and rapidly scans the local machine's HWIDs, searches the database for matches, and installs them.
  • PPK3 (Persistent Process Killer V3) - Another for the techs - suppress running processes that you need to keep away, helpful when fighting spyware/viruses.
  • Sync Tool - Folder sync tool with lots of real time information and several checking methods.
  • USMT Front End - Front End for Microsoft's User State Migration Tool, including all files needed for USMT 3.01 and 4.01, 32 bit and 64 bit versions.
  • Audit Tool - Computer audit tool to gather vital hardware, Windows, and Office information for IT managers and field techs. Capabilities include creating a customized site agent.
  • CSV Viewer - Displays CSV files with automatic column sizing and font selection. Lines can also be copied to the clipboard for data extraction.
  • MyDirStat - Lists number and size of files on a drive or specified path, allows for deletion within the app.
  • 2048 Game - My version of 2048, fun tile game.
  • Juice Lab - Ecigarette liquid making calculator.
  • Data Protector - Secure notes to save sensitive information.
  • VHD Footer - Add a footer to a forensic hard drive image to allow it to be mounted or used as a virtual machine hard drive.
  • Find in File - Searches files containing a specified phrase.

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.