Jump to content

Copy scirpt with progress/Multi progress


tlman12
 Share

Recommended Posts

I had a need a to write a script that could be called to copy files and folders, it needed to have and be able to collect more information then built in windows or xcopy could give me. so i started this script and what started out as a script for a singular purpose turned into a cool set of file functions. it now has the ability to copy files/folders with or without a built in gui or with a separate "kickoff" script (Thanks to Yashied and his Messages.au3 UDF)

this is only the third script that i felt would be useful enough to people (other then me) to release. I'm officially calling this version 1.0 and labeling it beta although it should be fully functional

any ideas for improvement in the code are welcomed and appreciated :D

Remember you must have the messages.au3 for this to work. Get it here http://www.autoitscript.com/forum/index.php?showtopic=91346

heres the function

#cs ----------------------------------------------------------------------------

 AutoIt Version: 1.0.0.0
 Author:         Travis

 Script Function:
    A small library of file folder functions.

#ce ----------------------------------------------------------------------------

#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Messages.au3>

Global $FileCount = 0
Global $CopiedCount = 0
Global $FolderCount = 0
Global $p = 0
Global $si = 0
Global $Status, $Progress, $SourceDirSize, $SourceSizeMB, $FinalDestSize, $gui, $Source


;========================================================================================
;Function _CopyGui($Source,$Dest,$GuiTitle)
;   This is a simple Gui for the _Copy($FileSource,$FileDest) Function
;
;   Example _CopyGui("C:\test", "C:\temp\test", "Copying Files...")
;   Status on the gui displays % completion Copied MB/ Total MB  and Current File
;
;   This script will even copy empty folders
;========================================================================================
Func _CopyGui($Source, $Dest, $GuiTitle)
    $gui = "yes"
    $SourceDirSize = DirGetSize($Source)
    $GIU = GUICreate($GuiTitle, 723, 80, -1, -1)
    $Progress = GUICtrlCreateProgress(8, 8, 705, 25)
    $Status = GUICtrlCreateLabel("", 8, 40, 708, 40)
    GUISetState(@SW_SHOW)
    _GetFileCount($Source)
    _Copy($Source, $Dest, "")
    $FinalDestSize = DirGetSize($Dest)
EndFunc   ;==>_CopyGui



;========================================================================================
;Function _CopyNoGui($Source,$Dest,$GuiTitle)
;   This is a simple Gui for the _Copy($FileSource,$FileDest) Function
;
;   Example _CopyGui("C:\test", "C:\temp\test", "Copying Files...")
;   Status on the gui displays % completion Copied MB/ Total MB  and Current File
;
;   This script will even copy empty folders
;========================================================================================
Func _CopyNoGui($Source, $Dest, $reciever)
    If $reciever = "" Then
        $r = "no"
    Else
        If Not _IsReceiver($reciever) Then
            MsgBox(0, "Error..", $reciever & " could not be found. Please check the name and try again.")
        EndIf
    EndIf
    $gui = "no"
    $SourceDirSize = DirGetSize($Source)
    _GetFileCount($Source)
    _Copy($Source, $Dest, $reciever)
    $FinalDestSize = DirGetSize($Dest)
EndFunc   ;==>_CopyNoGui


;========================================================================================
;Function _Copy($FileSource,$FileDest)
;   This function can be called by itself but I wouldn't reccomend it if you don't want
;   a GUI run _CopyNoGui($Source,$Dest)
;   If you don't want to run this alone you need to remember to put
;   $SourceDirSize = DirGetSize($Source) and
;   $FinalDestSize = DirGetSize($Dest) after you call the function for some parts to work
;
;   Example _Copy("C:\test","C:\temp\test")
;   $st     is the % complete
;   $p      is to set a progress bar
;   Round($si, 2)       will return the copied filesize in MB rounded to 2 decimal places
;   Round($SourceDirSize / 1000000, 2)      will return the origional source dir size in MB
;       rounded to 2 decimal places
;   $SourceFilePath     will return the full source path of the currently copying file
;   $DestFilePath       will return the full destination path of the currently copying file
;
;   This script will even copy empty folders
;=========================================================================================
Func _Copy($FileSource, $FileDest, $reciever)
    Local $Search
    Local $File
    Local $FileAttributes
    Local $SourceFilePath
    Local $DestFilePath
    $FirstFile = FileFindFirstFile($FileSource & "\*.*")
    While 1
        If $FirstFile = -1 Then
            ExitLoop
        EndIf
        $File = FileFindNextFile($FirstFile)
        If @error Then ExitLoop
        $SourceFilePath = $FileSource & "\" & $File
        $DestFilePath = $FileDest & "\" & $File
        $FileAttributes = FileGetAttrib($SourceFilePath)
        If StringInStr($FileAttributes, "D") Then
            DirCreate($DestFilePath)
            _Copy($SourceFilePath, $DestFilePath, $reciever)
        Else
            $CopyFileSize = FileGetSize($SourceFilePath)
            $Temp = ($CopyFileSize / $SourceDirSize) * 100
            $p = $p + $Temp
            $st = Round($p)
            $Tsi = $CopyFileSize / 1000000
            $si = $si + $Tsi
            $CopiedCount += 1
            $statusUP = $st & "%" & @TAB & "Copied: " & Round($si, 2) & "MB / " & Round($SourceDirSize / 1000000, 2) & "MB" & @TAB & "Current File: " & $SourceFilePath & @CRLF & "Copied: " & $CopiedCount & " / " & $FileCount & " Files"
            If $gui = "yes" Then
                GUICtrlSetData($Progress, $p)
                GUICtrlSetData($Status, $statusUP)
            ElseIf $gui = "no" Then
                If $reciever = "no" Then
                Else
                    If _IsReceiver($reciever) Then
                        $data = $p & "," & $statusUP
                        _MsgSend($reciever, String($data))
                    EndIf
                EndIf
            EndIf
            FileCopy($SourceFilePath, $DestFilePath, 9)
        EndIf
    WEnd
EndFunc   ;==>_Copy




;========================================================================================
;Function _GetFileCount($Folder)
;   This function gets the count of the files in a specified folder
;   Returns $FileCount
;=========================================================================================
Func _GetFileCount($Folder)
    Local $File
    Local $FileAttributes
    Local $FileSource
    Local $FirstFile
    $FirstFile = FileFindFirstFile($Folder & "\*.*")
    While 1
        If $FirstFile = -1 Then
            ExitLoop
        EndIf
        $File = FileFindNextFile($FirstFile)
        If @error Then ExitLoop
        $FilePath = $Folder & "\" & $File
        $FileAttributes = FileGetAttrib($FilePath)
        If StringInStr($FileAttributes, "D") Then
            _GetFileCount($FilePath)
        Else
            $FileCount += 1
            GUICtrlSetData($Status, "Counting files " & $FileCount & " files so far")
        EndIf
    WEnd
    Return $FileCount
EndFunc   ;==>_GetFileCount


;========================================================================================
;Function _GetFolderCount($Folder)
;   This function gets the count of the files in a specified folder
;   Returns $FolderCount
;=========================================================================================
Func _GetFolderCount($Folder)
    Local $File
    Local $FileAttributes
    Local $FileSource
    Local $FirstFile
    $FirstFile = FileFindFirstFile($Folder & "\*.*")
    While 1
        If $FirstFile = -1 Then
            ExitLoop
        EndIf
        $File = FileFindNextFile($FirstFile)
        If @error Then ExitLoop
        $FilePath = $Folder & "\" & $File
        $FileAttributes = FileGetAttrib($FilePath)
        If StringInStr($FileAttributes, "D") Then
            $FolderCount += 1
            GUICtrlSetData($Status, "Counting folders " & $FolderCount & " folders so far")
            _GetFolderCount($FilePath)
        EndIf
    WEnd
    Return $FileCount
EndFunc   ;==>_GetFolderCount

Heres an example on how to call it with the built in gui and with no gui. Also includes a simple statistic msgbox when finished.

#cs ----------------------------------------------------------------------------

 AutoIt Version: 1.0.0.0
 Author:         Travis

 Script Function:
    This is an example script to call the FileCopy Functions

#ce ----------------------------------------------------------------------------



#include<FileCopyFunc.au3>


$Source = "C:\test"
$Dest = "C:\temp\test"
$GuiTitle = "Copying Files..."


_CopyGui($Source,$Dest,$GuiTitle)
MsgBox(0, "Finished", "Copied " & $CopiedCount & " files total" & @CRLF _
        & "Source file count: " & $FileCount & @CRLF _
        & "Source Size: " & Round($SourceDirSize / 1000000, 2) & @CRLF _
        & "Destination Size: " & Round($FinalDestSize / 1000000, 2))

_CopyNoGui($Source,$Dest,"")
MsgBox(0, "Finished", "Copied " & $CopiedCount & " files total" & @CRLF _
        & "Source file count: " & $FileCount & @CRLF _
        & "Source Size: " & Round($SourceDirSize / 1000000, 2) & @CRLF _
        & "Destination Size: " & Round($FinalDestSize / 1000000, 2))

Heres the command line version, also has a finished statistics message box at the end.

#cs ----------------------------------------------------------------------------

 AutoIt Version: 1.0.0.0
 Author:         Travis

 Script Function:
    This is a command line version of my script, this is not an example and can
    be used.

    [Options] /n = No GUI  Default: Builtin GUI
    "Source Path"       Required
    "Destination Path"  Required
    "GUI Title"         Required only if /n is not used
    "Reciever Name"     Required only if /n is used (Advanced)
    Example: FileCopyCmdLine.exe "C:\test" "C:\temp\test" "Gui Title"
                    If no GUI Title is specified no GUI will be used
    Example: FileCopyCmdLine.exe /n "C:\test" "C:\temp\test" "Progress6"


#ce ----------------------------------------------------------------------------


#include<FileCopyFunc.au3>

If StringLower(StringRight(@ScriptName, 3)) = 'au3' Then
    MsgBox(64, @ScriptName, 'To run this script, you must first compile it and then run the (.exe) file.')
    Exit
EndIf

If $CmdLine[0] = "" Or $CmdLine[1] = "" Or StringInStr($CmdLine[1], "?") Or StringInStr($CmdLine[1], "help") Then
    MsgBox(0, "Help?", "To use the command line version of this scirpt you must specify" & @CRLF _
             & '[Options] /n = No GUI  Default: Builtin GUI' & @CRLF _
             & '"Source Path"' & @TAB & 'Required' & @CRLF _
             & '"Destination Path"' & @TAB & 'Required' & @CRLF _
             & '"GUI Title" Required if /n is not used' & @CRLF _
             & '"Reciever Name" Required if /n is used (Advanced)' & @CRLF _
             & 'Example: ' & @ScriptName & ' "C:\test" "C:\temp\test" "Gui Title"' & @CRLF _
             & 'Example2: ' & @ScriptName & ' /n "C:\test" "C:\temp\test" "Progress6"' & @CRLF _
             & "If no GUI Title is specified no GUI will be used")
EndIf
If $CmdLine[0] = "4" Then
    If $CmdLine[1] = "/n" Then
        _CopyNoGui($CmdLine[2], $CmdLine[3], $CmdLine[4])
    EndIf
EndIf

If $CmdLine[0] = "2" Then
    _CopyNoGui($CmdLine[1], $CmdLine[2], "")
    MsgBox(0, "Finished", "Copied " & $CopiedCount & " files total" & @CRLF _
             & "Source file count: " & $FileCount & @CRLF _
             & "Source Size: " & Round($SourceDirSize / 1000000, 2) & @CRLF _
             & "Destination Size: " & Round($FinalDestSize / 1000000, 2))
EndIf
If $CmdLine[0] = "3" Then
    _CopyGui($CmdLine[1], $CmdLine[2], $CmdLine[3])
    MsgBox(0, "Finished", "Copied " & $CopiedCount & " files total" & @CRLF _
             & "Source file count: " & $FileCount & @CRLF _
             & "Source Size: " & Round($SourceDirSize / 1000000, 2) & @CRLF _
             & "Destination Size: " & Round($FinalDestSize / 1000000, 2))
EndIf

and the "Kickoff" script, the example copies a folder C:\test to c:\temp\test1,2,3,...6 to show you the remote progress feature. so to use it make sure you have a folder called C:\test or change the script to a source and destination location of your choice

#cs ----------------------------------------------------------------------------

 AutoIt Version: 1.0.0.0
 Author:         Travis

 Script Function:
    This is an example script to call the FileCopyCmdLine.exe so it reports back
    to a seperate progress bar.

#ce ----------------------------------------------------------------------------
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <messages.au3>

#Region ### START Koda GUI section ### Form=
$Form1_1 = GUICreate("Form1", 625, 443, 297, 155)
$Progress1 = GUICtrlCreateProgress(8, 8, 609, 17)
$Progress2 = GUICtrlCreateProgress(8, 64, 609, 17)
$Progress3 = GUICtrlCreateProgress(8, 128, 609, 17)
$Progress4 = GUICtrlCreateProgress(8, 192, 609, 17)
$Progress5 = GUICtrlCreateProgress(8, 256, 609, 17)
$Progress6 = GUICtrlCreateProgress(8, 320, 609, 17)
$Status1 = GUICtrlCreateLabel("", 8, 32, 604, 33)
$Status2 = GUICtrlCreateLabel("", 8, 88, 612, 33)
$Status3 = GUICtrlCreateLabel("", 8, 152, 612, 33)
$Status4 = GUICtrlCreateLabel("", 8, 216, 612, 33)
$Status5 = GUICtrlCreateLabel("", 8, 280, 612, 33)
$Status6 = GUICtrlCreateLabel("", 8, 344, 612, 33)
$Button1 = GUICtrlCreateButton("Start Demo", 8, 384, 169, 49, $WS_GROUP)
$stop = GUICtrlCreateButton("Stop Demo", 180, 384, 169, 49)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
_MsgRegister('Progress1', '_Progress1')
_MsgRegister('Progress2', '_Progress2')
_MsgRegister('Progress3', '_Progress3')
_MsgRegister('Progress4', '_Progress4')
_MsgRegister('Progress5', '_Progress5')
_MsgRegister('Progress6', '_Progress6')

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        case $Button1
            $pid1 = Run('FileCopyCmdLine.exe /n "C:\test" "C:\temp\test1" "Progress1"')
            $pid2 = Run('FileCopyCmdLine.exe /n "C:\test" "C:\temp\test2" "Progress2"')
            $pid3 = Run('FileCopyCmdLine.exe /n "C:\test" "C:\temp\test3" "Progress3"')
            $pid4 = Run('FileCopyCmdLine.exe /n "C:\test" "C:\temp\test4" "Progress4"')
            $pid5 = Run('FileCopyCmdLine.exe /n "C:\test" "C:\temp\test5" "Progress5"')
            $pid6 = Run('FileCopyCmdLine.exe /n "C:\test" "C:\temp\test6" "Progress6"')
        Case $stop
            ProcessClose($pid1)
            ProcessClose($pid2)
            ProcessClose($pid3)
            ProcessClose($pid4)
            ProcessClose($pid5)
            ProcessClose($pid6)


    EndSwitch
WEnd



Func _Progress1($p)
    $p = StringSplit($p, ",")
    GUICtrlSetData($Progress1, $p[1])
    GUICtrlSetData($status1, $p[2])
    Return 0
EndFunc   ;==>_Progress1

Func _Progress2($p)
    $p = StringSplit($p, ",")
    GUICtrlSetData($Progress2, $p[1])
    GUICtrlSetData($status2, $p[2])
    Return 0
EndFunc   ;==>_Progress2

Func _Progress3($p)
        ;MsgBox(0,"","Recieving" & $p)
    $p = StringSplit($p, ",")
    ;MsgBox(0,"","Recieving" & $p)
    GUICtrlSetData($Progress3, $p[1])
    GUICtrlSetData($status3, $p[2])
    Return 0
EndFunc   ;==>_Progres3

Func _Progress4($p)
    $p = StringSplit($p, ",")
    GUICtrlSetData($Progress4, $p[1])
    GUICtrlSetData($status4, $p[2])
    Return 0
EndFunc   ;==>_Progress4

Func _Progress5($p)
    $p = StringSplit($p, ",")
    GUICtrlSetData($Progress5, $p[1])
    GUICtrlSetData($status5, $p[2])
    Return 0
EndFunc   ;==>_Progress5
Func _Progress6($p)
    $p = StringSplit($p, ",")
    GUICtrlSetData($Progress6, $p[1])
    GUICtrlSetData($status6, $p[2])
    Return 0
EndFunc   ;==>_Progress6

all scripts written in AutoIT Version 3.3.0.0

Features

Copies all Files/Folders in the specified source path.

Copies empty directories

Includes a simple GUI for a quick and easy use

includes a no GUI for background use

Includes file and folder count (Folder count not implemented in examples)

Includes the ability to run multiple copies at once and have them all report back to a single GUI with progress and status

Edited by tlman12
Link to comment
Share on other sites

  • 2 weeks later...
  • 3 weeks later...
  • 5 years later...

How do you get the copy dialog to go away once the copy is complete?

                           local $GIU = _CopyGui($Source,$Dest,$GuiTitle)

                           GUIDelete($GIU)

 

Edited by caramen

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to comment
Share on other sites

I'd be willing to bet big money that jp10558 doesn't need that answer after nearly 5 1/2 years.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

i was needing it so i think about other's ppl that may will need it. sry for bump old post

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

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