Sign in to follow this  
Followers 0
thexshadow

Link to a button

11 posts in this topic

I have been looking around for a file copy with progress bar script, and I found it Here. Now, I have been playing around with it, but I cannot think of a way to link a button to start the file copy, instead of it automatically starting.

Would anyone have any ideas?

Share this post


Link to post
Share on other sites



Be sure to check the example on the GUICtrlCreateButton reference page. If you use OnEvent mode (which I always prefer), you can use GUICtrlSetOnEvent.

This should get you going in the right - and easy - direction

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I already knew about GUICtrlCreateButto, GUICtrlSetOnEvent looks like it will be perfect.

Thanks!

One problem I am having is when I run my script, the file transfer starts automatically. Would there be any way to stop it from running auto?

I have tried a few things. My latest try.

#include <GuiConstantsEx.au3>

Opt("GuiOnEventMode", 1)

$hGUI = GUICreate("Test", 300, 300)
GUICtrlCreateButton("test", 100, 100, 100, 30)
GUICtrlSetOnEvent(-1, "_CopyWithProgress")
GUISetState()




Files
Files
Files


_CopyWithProgress()

If $FileCopy = 0 Then
    MsgBox(0,"Copy Complete", "File Copy Successful.")
Else
    MsgBox(16,"Copy Complete", "File Copy Failed.")
EndIf





Func _CopyWithProgress($inSource, $inDest, $ChunkSize = 2048)
    $SourceFile = FileOpen($inSource, 16)
    
; Check that file open was successful
    If $SourceFile = -1 Then
        MsgBox(16, "Error", "Unable to open source file: " & $inSource)
        Exit
    EndIf
    
; Open destination file for BINARY writing , overwriting existing file and creating directory structure if it doesn't exist
    $DestFile = FileOpen($inDest, 26)
    
; Check that file open was successful
    If $SourceFile = -1 Then
        MsgBox(16, "Error", "Unable to open destination file: " & $inDest)
        Exit
    EndIf
    
; Get the size of the file we are going to copy
    $SourceSize = FileGetSize($inSource)

; Calculate how many chunks we need to copy
    $Chunks = $SourceSize / $ChunkSize
    
; Show the progress bar
    ProgressOn("File Copy", "Copying...", "Source: " & $inSource & @LF & "Dest:  " & $inDest)
    
; Loop from 0 to number of chunks
    For $i = 0 to $Chunks
    
; Read the next chunk
        $Data = FileRead($SourceFile, $ChunkSize)
        
; Write the next chunk
        FileWrite($DestFile, $Data)
        
; Update the progress bar
        ProgressSet( (($i + 1) / $Chunks) * 100 )
    Next
    
; Hide the progress bar
    ProgressOff()
    
; Close the source file
    FileClose($SourceFile)
    
; Close the destination file
    FileClose($DestFile)
    
; Check the file copy was successful
    $DestSize = FileGetSize($inDest)
    
    If $SourceSize = $DestSize Then
        Return 0
    Else
        Return 1
    EndIf
EndFunc
It starts the transfer when the app starts. I have also tried it with While 1. Same thing. Any ideas? Edited by thexshadow

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

---

Edited by playlet

Share this post


Link to post
Share on other sites

Change

Files


_CopyWithProgress()

If $FileCopy = 0 Then

to

Files


;_CopyWithProgress()

If $FileCopy = 0 Then

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

@playlet

Not exactly what I wanted. But I think I can work with it. I wanted files from the autoit script to be copied.

$FileCopy = FileCopy("Extract\AudMgr_AM_ible.dll", "Test\AudioPluginMgr\AM_ible.dll")
$FileCopy = FileCopy("Extract\AudnMgr_AM_PP.dll", "Test\AudioPluginMgr\AM_PP.dll")
Now, I just need to figure out a way to do that. :mellow: Edited by thexshadow

Share this post


Link to post
Share on other sites

Well, I don't mean actually packed into the exe. I mean copy files from one folder to another (not the entire folder).

Share this post


Link to post
Share on other sites

Well, tried editing it. It kind of reverted back to its original workings.

Global $DestFile
Global $SourceFile



;Files To Copy
_CopyWithProgress("Extract\AudMgr_AM_ible.dll", "Test\AudioPluginMgr\AM_ible.dll")
_CopyWithProgress("Extract\AudnMgr_AM_PP.dll", "Test\AudioPluginMgr\AM_PP.dll")
;End


$hGUI = GUICreate("Test", 300, 300)
$button = GUICtrlCreateButton("test", 100, 100, 100, 30)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $button
            $FileCopy = _CopyWithProgress($SourceFile, $DestFile)
            If $FileCopy = 1 Then
                MsgBox(0,"Copy Complete", "File Copy Successful.")
            Else
                MsgBox(16,"Copy Complete", "File Copy Failed.")
            EndIf
        Case -3; This is actually $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    Sleep(10)
WEnd




Func _CopyWithProgress($SourceFile, $DestFile)
    If $SourceFile = -1 Then; Check that file open was successful
    MsgBox(16, "Error", "Unable to open source file: " & $copy_what)
    Return 0; THIS ENDS FUNCTION HERE AND SAYS THAT THERE WAS AN ERROR - YOU CAN TRY AGAIN WITH NEW SETTINGS
    EndIf
    $getfiletype = StringSplit ($SourceFile, ".", 1)
    $extension = $getfiletype[$getfiletype[0]]
    
    $DestFile = FileOpen($DestFile & "." & $extension, 26); Open destination file for BINARY writing , overwriting existing file and creating directory structure if it doesn't exist
    If $SourceFile = -1 Then; Check that file open was successful
    MsgBox(16, "Error", "Unable to open destination file: " & $DestFile)
    Return 0; THIS ENDS FUNCTION HERE AND SAYS THAT THERE WAS AN ERROR - YOU CAN TRY AGAIN WITH NEW SETTINGS
    EndIf
    $SourceSize = FileGetSize($SourceFile); Get the size of the file we are going to copy
    $Chunks = $SourceSize / 2048; Calculate how many chunks we need to copy
    ProgressOn("File Copy", "Copying...", "Source: " & $SourceFile & @LF & "Dest: " & $DestFile); Show the progress bar
    For $i = 0 to $Chunks; Loop from 0 to number of chunks
    $Data = FileRead($SourceFile, 2048); Read the next chunk
    FileWrite($DestFile, $Data); Write the next chunk
    ProgressSet(($i / $Chunks) * 100); Update the progress bar
    Next
    FileClose($DestFile); Close the destination file
    FileClose($SourceFile); Close the source file
    ProgressOff(); Hide the progress bar
    If FileGetSize($DestFile & "." & $extension) = $SourceSize Then Return 1; Check the file copy was successful
    Return 0
EndFunc

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

---

Edited by playlet

Share this post


Link to post
Share on other sites

Thanks playlet, looks like that is exactly what I want.

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