Sign in to follow this  
Followers 0
Steph

Graphical File Copy - how to improve it?

6 posts in this topic

Hi All,

I've written a graphical file copy routine which gives user feedback on progress. This is for copying some large (video) files off a CD to the harddrive for a distribution I'm preparing. The progress bar advances as each file is copied, but because of the size of the files can stick there for a while. What I'm wondering is if there is any way to keep the progress bar advancing as the file is copied? Would it mean copying the file byte-by-byte as it were? Any ideas for the right direction to go in would be welcome.

My code is below, including a temporary entry point for testing which copies files from c:\Source to c:\Dest.

Thanks,

Steph

; Copy a number of files showing progress bar and keeping user
; advised of progress

Opt("MustDeclareVars", 1)     ; 0=no, 1=require pre-declare

#include <guiconstants.au3>

Const $WIDTH = 500
Const $HEIGHT = 90
Const $PROG_HEIGHT = 30
Const $CTRL_BORDER = 10
Const $MESS1 = "Copying file "
Const $MESS2 = " of "

; Temporary entry point for testing

MsgBox(0, "MyFileCopy...", "Returned : " & MyFileCopy("Test copy", "C:\Source", "C:\Dest", 1))
Exit

; MyFileCopy
; Copy all files from one directory while providing progress info.
; Returns number of files copied (which could be 0).
; Returns -1 if source directory doesn't exist

Func MyFileCopy($WinTitle, $SourceDir, $DestDir, $Flag)

    Local $NumFiles, $cnt, $Message, $Prog_Bar
    Local $DirInfo[3], $BytesDoneSoFar = 0
    Local $FileSearchHandle, $FileName
    
; Create GUI
    GUICreate($WinTitle,$WIDTH, $HEIGHT, -1, -1, $WS_CAPTION, BitOR($WS_EX_TOPMOST, $WS_EX_DLGMODALFRAME))
    $Message = GUICtrlCreateLabel("Initialising for file copy...", $CTRL_BORDER, $CTRL_BORDER, $WIDTH - (2*$CTRL_BORDER), 40, $SS_CENTER)
    $Prog_Bar = GUICtrlCreateProgress($CTRL_BORDER, $HEIGHT - $PROG_HEIGHT - $CTRL_BORDER, $WIDTH - (2*$CTRL_BORDER), $PROG_HEIGHT)
    GuiCtrlSetFont($Message, 14)
    GUISetState(@SW_SHOW)
    
; Find out how many files we have to copy
    $DirInfo = DirGetSize($SourceDir, 1)                    ; 1 means use extended mode so get extra information
    $FileSearchHandle = FileFindFirstFile($SourceDir & "\*.*"); Set up search handle to pick up files one by one
    
; Copy files, advance progress bar and update message
    If $DirInfo[0] = -1 Then
        GUIDelete
        Return(-1)
    Else
        For $cnt = 1 To $DirInfo[1]
            $FileName = FileFindNextFile($FileSearchHandle)
            $BytesDoneSoFar =$BytesDoneSoFar + FileGetSize( $SourceDir & "\" & $FileName )
            GUICtrlSetData($Prog_Bar,(100 / $DirInfo[0]) * $BytesDoneSoFar)
            GuiCtrlSetData($Message, $MESS1 & String($cnt) & $MESS2 & String($DirInfo[1]) & " (" & $FileName & ")")
            FileCopy($SourceDir & "\" & $FileName, $DestDir & "\" & $FileName, $Flag)
        Next
        FileClose($FileSearchHandle)
        GUIDelete()
        Return ($DirInfo[1])
    EndIf
    
EndFunc

Share this post


Link to post
Share on other sites



I finally found a post here, that talks about the wonders of microsoft and it's "copy" command. I just edited PsaltyDS code to fit your script and it seems to work great.

; Copy a number of files showing progress bar and keeping user
; advised of progress

;Opt("MustDeclareVars", 1)   ; 0=no, 1=require pre-declare

#include <guiconstants.au3>

Const $WIDTH = 500
Const $HEIGHT = 90
Const $PROG_HEIGHT = 30
Const $CTRL_BORDER = 10
Const $MESS1 = "Copying file "
Const $MESS2 = " of "

; Temporary entry point for testing

MsgBox(0, "MyFileCopy...", "Returned : " & MyFileCopy("Test copy", "C:\Source", "C:\Dest", 1))
Exit

; MyFileCopy
; Copy all files from one directory while providing progress info.
; Returns number of files copied (which could be 0).
; Returns -1 if source directory doesn't exist

Func MyFileCopy($WinTitle, $SourceDir, $DestDir, $Flag)
    
    Local $NumFiles, $cnt, $Message, $Prog_Bar
    Local $DirInfo[3], $BytesDoneSoFar = 0
    Local $FileSearchHandle, $FileName
    
; Create GUI
    GUICreate($WinTitle, $WIDTH, $HEIGHT, -1, -1, $WS_CAPTION, BitOR($WS_EX_TOPMOST, $WS_EX_DLGMODALFRAME))
    $Message = GUICtrlCreateLabel("Initialising for file copy...", $CTRL_BORDER, $CTRL_BORDER, $WIDTH - (2 * $CTRL_BORDER), 40, $SS_CENTER)
    $Prog_Bar = GUICtrlCreateProgress($CTRL_BORDER, $HEIGHT - $PROG_HEIGHT - $CTRL_BORDER, $WIDTH - (2 * $CTRL_BORDER), $PROG_HEIGHT)
    GUICtrlSetFont($Message, 14)
    GUISetState(@SW_SHOW)
    
; Find out how many files we have to copy
    $DirInfo = DirGetSize($SourceDir, 1)                   ; 1 means use extended mode so get extra information
    $FileSearchHandle = FileFindFirstFile($SourceDir & "\*.*"); Set up search handle to pick up files one by one
    
; Copy files, advance progress bar and update message
    If $DirInfo[0] == -1 Then
        GUIDelete
        Return (-1)
    Else
        For $cnt = 1 To $DirInfo[1]
            $FileName = FileFindNextFile($FileSearchHandle)
            GUICtrlSetData($Message, $MESS1 & String($cnt) & $MESS2 & String($DirInfo[1]) & " (" & $FileName & ")")
            $TypeCommand = 'type "' & $SourceDir & '\' & $FileName & '" > "' & $DestDir & '\' & $FileName & '"'
            Run(@ComSpec & ' /c ' & $TypeCommand, $DestDir, @SW_SHOW)
            Do
                $Bytes = FileGetSize($SourceDir & "\" & $FileName)
                $BytesDoneSoFar = FileGetSize($DestDir & "\" & $FileName)
                $Percent = ($BytesDoneSoFar / $Bytes) * 100
                GUICtrlSetData($Prog_Bar, $Percent)
            Until $Bytes == $BytesDoneSoFar
        Next
        FileClose($FileSearchHandle)
        GUIDelete()
        Return ($DirInfo[1])
    EndIf
EndFunc  ;==>MyFileCopy

Share this post


Link to post
Share on other sites

Great - just what I needed, I've edited a bit to hide the cmd windows and to now have two progress bars, one for the individual file and one for overall progress. I've attached it below.

Thanks for your help big_daddy.

File_Copy_v2.au3

Share this post


Link to post
Share on other sites

Glad I could help. Your finished script looks good, I may use this myself.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

The Gui look good, I try your script and

1) It said that copy 813 file but, actually only copy one file of 0 byte.

2) I enter a wrong source path and the script error out, did not report -1

at these lines of code remove the red and your script will report -1 when worg source path

; Copy files, advance progress bar and update message

If $DirInfo[0] == -1 Then

GUIDelete()

Return(-1)

3) When the Gui came out the progress bars did not show progress but I can see the message copying file # of 813.

When I have an spare time I try to find out why did not copy the files.

Edited by Danny35d

AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

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  
Followers 0