Jump to content
Sign in to follow this  

File copy with progress bar

Recommended Posts


I needed to create a function like this for a specific requirement so I thought I would share it.

Uses FileRead and FileWrite to copy a file allowing for a progress bar showing amount of data copied. Also allows for slowing the copy down (copygap), to make it bandwidth friendly.

This has been done before using external copy commands (like xcopy) and constant file size checking, but this method fit my requirement better.


; -- ProgressCopy --
; Uses FileRead and FileWrite functions to do a binary copy of file data from
; one location to another with a progress bar. Note that time stamps will be
; different.
; Inputs
;  $src     : Full path to source file
;  $dst     : Full path to destination file (file name required)
;  $copygap : Time in ms to leave between each copy of data (default 20ms)
; Returns 1 if success, 0 if failed 
Func ProgressCopy($src, $dst, $copygap = 20)
   Dim $OptEnv, $OptVar, $size, $count, $timer, $progress, $in, $out
   ; Turn off expanded environment or variables, can cause problems for binary read
   $OptEnv = Opt("ExpandEnvStrings", 0)
   $OptVar = Opt("ExpandVarStrings", 0)
   ; Get size of file to determine remaining
   $size = FileGetSize($src)
   ; Set up initial variables
   $count = 0           ;keep count of bytes copied
   $progress = 0        ;progress bar variable (0 - 100)
   $chunk = 16384       ;amount of data to copy at a time (bytes)
   $updatefreq = 500    ;frequency of update to progress bar (ms)
   $progtitle = "File Copy Progress"   ;title of progress bar window
   $timer = TimerInit() ;initialize timer to keep track of update frequency
   ; Show progress bar
   ProgressOn($progtitle, "Copying " & $src & " ...", "0 of " & $size & " bytes copied. (0%)")

   ; Open input and output files (exit if error)
   $in = FileOpen($src, 0)
   If @error <> 0 Then Return 0
   $out = FileOpen($dst, 2)
   If @error <> 0 Then
      Return 0
   ; Start reading input
   $bin = FileRead($in, $chunk)
   While @error = 0
      ; Write chunk of data, keep trying for up to 10 seconds if failed
         FileWrite($out, $bin)
      Until (@error = 0) Or (TimerDiff($timer) > 10000)
      If @error <> 0 Then
         Return 0
      ; Update copy counter
      $count = $count + $chunk
      ; Update progress as specified
      If (TimerDiff($timer) / $updatefreq) > 1 Then
         $progress = Int(($count / $size) * 100)
         ProgressSet($progress, $count & " of " & $size & " bytes copied. (" & $progress & "%)")
         $timer = TimerInit()
      ; Wait as specified
      ; Get next chunk of data
      $bin = FileRead($in, $chunk)
   ; Show complete
   ProgressSet(100, $size & " of " & $size & " bytes copied. (100%)")
   ; Close files
   ; Hide progress bar
   ; Reset expanded environment or variables
   Opt("ExpandEnvStrings", $OptEnv)
   Opt("ExpandVarStrings", $OptVar)
   ; Return success
   Return 1

Share this post

Link to post
Share on other sites

This is an awesome script, thanks for taking the time to write and share it. I do have a feature request. Can you add the ability to Create the source destination if it doesn't already exist? Much like the FileCopy with a flag of 8

Share this post

Link to post
Share on other sites

I don't think that's a reasonable expectation, the OP hasn't been on the forum in 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

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