Jump to content

Graphical File Copy - how to improve it?


Recommended Posts

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
Link to post
Share on other sites
  • Moderators

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
Link to post
Share on other sites

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
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...