Jump to content

Display A % Of Progress Bar During Filecopy


Recommended Posts

I have searched for and answer unsuccessfully, so figured I would ask.

I am looking to display a Progress bar (while copying a file) that represents the Progress of the file being copied, like Windows does. The examples I have seen, all display progress based on time or a percentage of overall time. The roblem, depending on size or traffic conditions, we don't know how long the file will take to copy. Has anyone done this? Is there an example of this? Thank you. PeterF

Link to comment
Share on other sites

1

welcome to the forums

2

I would disagree, if you search ( from the top of this page ) in the Scripts and Scraps section, you will find many with "Copy with progress" that use file size to update the progress

8)

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

Here is a UDF done by Ezzetabi, that may do what you want. It would also be located in th Scripts'n'scraps forum for more info. :)

Func _CopyDirWithProgress($sOriginalDir, $sDestDir)
  ;$sOriginalDir and $sDestDir are quite selfexplanatory...
  ;This func returns:
  ; -1 in case of critical error, bad original or destination dir
  ;  0 if everything went all right
  ; >0 is the number of file not copied and it makes a log file
  ;  if in the log appear as error message '0 file copied' it is a bug of some windows' copy command that does not redirect output... 
   
   If StringRight($sOriginalDir, 1) <> '\' Then $sOriginalDir = $sOriginalDir & '\'
   If StringRight($sDestDir, 1) <> '\' Then $sDestDir = $sDestDir & '\'
   If $sOriginalDir = $sDestDir Then Return -1
   
   ProgressOn('Copying...', 'Making list of files...' & @LF & @LF, '', -1, -1, 18)
   Local $aFileList = _FileSearch($sOriginalDir)
   If $aFileList[0] = 0 Then
      ProgressOff()
      SetError(1)
      Return -1
   EndIf
   
   If FileExists($sDestDir) Then
      If Not StringInStr(FileGetAttrib($sDestDir), 'd') Then
         ProgressOff()
         SetError(2)
         Return -1
      EndIf
   Else
      DirCreate($sDestDir)
      If Not FileExists($sDestDir) Then
         ProgressOff()
         SetError(2)
         Return -1
      EndIf
   EndIf
   
   Local $iDirSize, $iCopiedSize = 0, $fProgress = 0
   Local $c, $FileName, $iOutPut = 0, $sLost = '', $sError
   Local $Sl = StringLen($sOriginalDir)
  
   _Quick_Sort($aFileList, 1, $aFileList[0])
   
   $iDirSize = Int(DirGetSize($sOriginalDir) / 1024)

   ProgressSet(Int($fProgress * 100), $aFileList[$c], 'Coping file:')
   For $c = 1 To $aFileList[0]
      $FileName = StringTrimLeft($aFileList[$c], $Sl)
      ProgressSet(Int($fProgress * 100), $aFileList[$c] & ' -> '& $sDestDir & $FileName & @LF & 'Total KiB: ' & $iDirSize & @LF & 'Done KiB: ' & $iCopiedSize, 'Coping file:  ' & Round($fProgress * 100, 2) & ' %   ' & $c & '/' &$aFileList[0])
      
      If StringInStr(FileGetAttrib($aFileList[$c]), 'd') Then
         DirCreate($sDestDir & $FileName)
      Else
         If Not FileCopy($aFileList[$c], $sDestDir & $FileName, 1) Then
            If Not FileCopy($aFileList[$c], $sDestDir & $FileName, 1) Then;Tries a second time
               If RunWait(@ComSpec & ' /c copy /y "' & $aFileList[$c] & '" "' & $sDestDir & $FileName & '">' & @TempDir & '\o.tmp', '', @SW_HIDE)=1 Then;and a third time, but this time it takes the error message
                  $sError = FileReadLine(@TempDir & '\o.tmp',1)
                  $iOutPut = $iOutPut + 1
                  $sLost = $sLost & $aFileList[$c] & '  ' & $sError & @CRLF
               EndIf
               FileDelete(@TempDir & '\o.tmp')
            EndIf
         EndIf
         
         FileSetAttrib($sDestDir & $FileName, "+A-RSH");<- Comment this line if you do not want attribs reset.
         
         $iCopiedSize = $iCopiedSize + Int(FileGetSize($aFileList[$c]) / 1024)
         $fProgress = $iCopiedSize / $iDirSize
      EndIf
   Next
  
   ProgressOff()
   
   If $sLost <> '' Then;tries to write the log somewhere.
      If FileWrite($sDestDir & 'notcopied.txt',$sLost) = 0 Then
         If FileWrite($sOriginalDir & 'notcopied.txt',$sLost) = 0 Then
            FileWrite(@WorkingDir & '\notcopied.txt',$sLost)
         EndIf
      EndIf
   EndIf
   
   Return $iOutPut
EndFunc  ;==>_CopyDirWithProgress

Func _FileSearch($sIstr, $bSF = 1)
  ; $bSF = 1 means looking in subfolders
  ; $sSF = 0 means looking only in the current folder.
  ; An array is returned with the full path of all files found. The pos [0] keeps the number of elements.
   Local $sIstr, $bSF, $sCriteria, $sBuffer, $iH, $iH2, $sCS, $sCF, $sCF2, $sCP, $sFP, $sOutPut = '', $aNull[1]
   $sCP = StringLeft($sIstr, StringInStr($sIstr, '\', 0, -1))
   If $sCP = '' Then $sCP = @WorkingDir & '\'
   $sCriteria = StringTrimLeft($sIstr, StringInStr($sIstr, '\', 0, -1))
   If $sCriteria = '' Then $sCriteria = '*.*'
   
  ;To begin we seek in the starting path.
   $sCS = FileFindFirstFile($sCP & $sCriteria)
   If $sCS <> - 1 Then
      Do
         $sCF = FileFindNextFile($sCS)
         If @error Then
            FileClose($sCS)
            ExitLoop
         EndIf
         If $sCF = '.' Or $sCF = '..' Then ContinueLoop
         $sOutPut = $sOutPut & $sCP & $sCF & @LF
      Until 0
   EndIf
   
  ;And after, if needed, in the rest of the folders.
   If $bSF = 1 Then
      $sBuffer = @CR & $sCP & '*' & @LF;The buffer is set for keeping the given path plus a *.
      Do
         $sCS = StringTrimLeft(StringLeft($sBuffer, StringInStr($sBuffer, @LF, 0, 1) - 1), 1);current search.
         $sCP = StringLeft($sCS, StringInStr($sCS, '\', 0, -1));Current search path.
         $iH = FileFindFirstFile($sCS)
         If $iH <> - 1 Then
            Do
               $sCF = FileFindNextFile($iH)
               If @error Then
                  FileClose($iH)
                  ExitLoop
               EndIf
               If $sCF = '.' Or $sCF = '..' Then ContinueLoop
               If StringInStr(FileGetAttrib($sCP & $sCF), 'd') Then
                  $sBuffer = @CR & $sCP & $sCF & '\*' & @LF & $sBuffer;Every folder found is added in the begin of buffer
                  $sFP = $sCP & $sCF & '\';                               for future searches
                  $iH2 = FileFindFirstFile($sFP & $sCriteria);         and checked with the criteria.
                  If $iH2 <> - 1 Then
                     Do
                        $sCF2 = FileFindNextFile($iH2)
                        If @error Then
                           FileClose($iH2)
                           ExitLoop
                        EndIf
                        If $sCF2 = '.' Or $sCF2 = '..' Then ContinueLoop
                        $sOutPut = $sOutPut & $sFP & $sCF2 & @LF;Found items are put in the Output.
                     Until 0
                  EndIf
               EndIf
            Until 0
         EndIf
         $sBuffer = StringReplace($sBuffer, @CR & $sCS & @LF, '')
      Until $sBuffer = ''
   EndIf
   
   If $sOutPut = '' Then
      $aNull[0] = 0
      Return $aNull
   Else
      Return StringSplit(StringTrimRight($sOutPut, 1), @LF)
   EndIf
EndFunc  ;==>_FileSearch

Func _Quick_Sort(ByRef $SortArray, $First, $Last);Larry's code
   Dim $Low, $High
   Dim $Temp, $List_Separator
   
   $Low = $First
   $High = $Last
   $List_Separator = StringLen($SortArray[ ($First + $Last) / 2])
   Do
      While (StringLen($SortArray[$Low]) < $List_Separator)
         $Low = $Low + 1
      WEnd
      While (StringLen($SortArray[$High]) > $List_Separator)
         $High = $High - 1
      WEnd
      If ($Low <= $High) Then
         $Temp = $SortArray[$Low]
         $SortArray[$Low] = $SortArray[$High]
         $SortArray[$High] = $Temp
         $Low = $Low + 1
         $High = $High - 1
      EndIf
   Until $Low > $High
   If ($First < $High) Then _Quick_Sort($SortArray, $First, $High)
   If ($Low < $Last) Then _Quick_Sort($SortArray, $Low, $Last)
EndFunc  ;==>_Quick_Sort
Link to comment
Share on other sites

  • Developers

here's what windows uses:

;~ 4 Do not display a progress dialog box.  
;~ 8 Give the file being operated on a new name in a move, copy, or rename operation if a file with the target name already exists.  
;~ 16 Respond with "Yes to All" for any dialog box that is displayed.  
;~ 64 Preserve undo information, if possible. 
;~ 128 Perform the operation on files only if a wildcard file name (*.*) is specified.  
;~ 256 Display a progress dialog box but do not show the file names.  
;~ 512 Do not confirm the creation of a new directory if the operation requires one to be created.  
;~ 1024 Do not display a user interface if an error occurs.  
;~ 2048 Version 4.71. Do not copy the security attributes of the file. 
;~ 4096 Only operate in the local directory. Don't operate recursively into subdirectories. 
;~ 9182 Version 5.0. Do not copy connected files as a group. Only copy the specified files.  

_FileCopy("C:\Installed Apps\Patches\WindowsXP-KB835935-SP2-ENU.exe","C:\temp")

Func _FileCopy($fromFile,$tofile) 
    Local $FOF_RESPOND_YES = 16
    Local $FOF_SIMPLEPROGRESS = 256
    $winShell = ObjCreate("shell.application")
    $winShell.namespace($tofile).CopyHere($fromFile,$FOF_RESPOND_YES)
EndFunc
Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

here's what windows uses:

CODE: AutoIt;~ 4 Do not display a progress dialog box.

;~ 8 Give the file being operated on a new name in a move, copy, or rename operation if a file with the target name already exists.

;~ 16 Respond with "Yes to All" for any dialog box that is displayed.

;~ 64 Preserve undo information, if possible.

;~ 128 Perform the operation on files only if a wildcard file name (*.*) is specified.

;~ 256 Display a progress dialog box but do not show the file names.

;~ 512 Do not confirm the creation of a new directory if the operation requires one to be created.

;~ 1024 Do not display a user interface if an error occurs.

;~ 2048 Version 4.71. Do not copy the security attributes of the file.

;~ 4096 Only operate in the local directory. Don't operate recursively into subdirectories.

;~ 9182 Version 5.0. Do not copy connected files as a group. Only copy the specified files.

_FileCopy("C:Installed AppsPatchesWindowsXP-KB835935-SP2-ENU.exe","C: emp")

Func _FileCopy($fromFile,$tofile)

Local $FOF_RESPOND_YES = 16

Local $FOF_SIMPLEPROGRESS = 256

$winShell = ObjCreate("shell.application")

$winShell.namespace($tofile).CopyHere($fromFile,$FOF_RESPOND_YES)

EndFunc

i used this on upto 5 meg... and did not see a progress-bar

???

8)

NEWHeader1.png

Link to comment
Share on other sites

  • Developers

i used this on upto 5 meg... and did not see a progress-bar

???

8)

I get:

Maybe your PC is to fast...

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

i continue to get this error.

$winShell = ObjCreate("shell.application")

$winShell = ^ERROR

Error: Unknown Function Name.

I installed:

autoit-v3-setup.exe - then

autoit-v3.1.1.123-beta-Setup.exe - then

autoit-v3.1.1.124-beta-Setup.exe

Is that correct?

I only changed the file source.

this is what i am trying to run:

_FileCopy("C:\Windows\imsins.BAK","C:\temp")

Func _FileCopy($fromFile,$tofile)

Local $FOF_RESPOND_YES = 16

Local $FOF_SIMPLEPROGRESS = 256

$winShell = ObjCreate("shell.application")

$winShell.namespace($tofile).CopyHere($fromFile,$FOF_RESPOND_YES)

EndFunc

Any suggestions?

PeterF

Link to comment
Share on other sites

I don't know a lot about SciTE, but it simply looks like *whatever* run command you are using is pointing to the wrong AutoIt binary.

I gather from Valuater's post that SciTE has a menu allowing you to choose *which* binary you wish to run the script with (in the "tools" menu, there are options to "run" and "run with beta", same for "compile", so you can use either). Although the SciTE menus and options are a tad confusing, if you are going to use it, spend some time running through them all, there's lots of useful stuff in there.

If it's still not happening, I would uninstall everything (including SciTE, if you use that). Then install *only* the beta version of AutoIt (which is miles better than the release, anyway, and so far in my experience, totally stable), and then install SciTE (the "for AutoIt" version) afterwards; all your commands should be correctly setup, including explorer concept menus.

At least that's how I remember it; I've only ever done that one time, since then I just throw binaries around and keep the whole lot in my EditPlus tools menu for that one-touch-run-or-compile-with-whatever-options feeling.

While I'm here, here's a good tip for whatever IDE you use:

I use EditPlus (for pretty much everything), but very probably you can do this in SciTE, and all good text editors (maybe we should start a thread for "IDE Tips", maybe there already is one. oops!) ...

When you create your AutoIt tools menu (in whatever editor you use) there will likely be a space to add arguments to the command (that is, switches, etc. some will have a separate input, some allow you to just tag them on the end of the command itself, whatever). All good editors have "macros" (much like AutoIt @macros) that allow you to insert special strings into the command, like the file path, folder, basename of file, time, etc.

So far so good. Now, if you are creating an AutoIt application that accepts command-line parameters, you don't want to have to keep compiling the thing all the time just to throw some file onto the binary to test some tweak, so...

Create a run command for "Run with selection", and use whatever macro your editor uses for the current text selection, and you can just select strings right there in your script, probably in a comments section ...

; damn! I've been gagging to try out this new syntax highlighting
; but all I have to put here is comments! 
; not just any comments, though, but command-line test strings...
;
;   zxvf C:\temp\testfile.dat
;   cf C:\temp\testfile.dat
;
; oh wait! I've thought of something I could do...
;
; example command-line intereceptor:
$file_path = ""
$optional_switches = ""
if $CmdLine[0] then
    if $CmdLine[0] = 2 then
        $optional_switches = $CmdLine[1]
    endif
    $file_path = $CmdLine[$CmdLine[0]]
endif
if $file_path = "" then DoCommandLineInfo() 
;
; hey! looks good!

Then you simply select the text, hit your hot-key, and Bang! The script runs with the selected string as its command-line arguments. Recently saved me hours! By the way, in EditPlus, that would be (in the "Argument:" input) ...

/ErrorStdOut "$(FilePath)" $(CurSel)

The "/ErrorStdOut " is optional, but highly recommended; you can write to the console at any time in your script (ConsoleWrite()), and it will appear right there in your console output window, handy.

There are loads of tricks and time-savers like this, but you need to get a solid, 100% reliable setup to begin with, something you can trust (because when things break in your development cycle, you don't want it crossing your mind, ever, that the trouble may be outside your code), and that possibly means installing only one version of AutoIt, at least until you are more familiar with all this stuff.

After a short learning curve you can have a killer menu with options to run and compile your script with all sorts of different parameters and crazy criteria, but to start with...

KISS!

-mu

ps. in the mood for writing tonight. :)

Link to comment
Share on other sites

  • Developers

i continue to get this error.

$winShell = ObjCreate("shell.application")

$winShell = ^ERROR

Error: Unknown Function Name.

I installed:

autoit-v3-setup.exe - then

autoit-v3.1.1.123-beta-Setup.exe - then

autoit-v3.1.1.124-beta-Setup.exe

Is that correct?

Any suggestions?

PeterF

@Peter, pls make sure you install the programs in the sequence as defined in this link:

Installation instructions for SciTE4AutoIt3 and AutoIt3:Production and Beta.

Then run the script from Within SciTE by doing ALt+F5.

When this doesn't work, pls post the whole output from SciTE's outputpane so I can see whats wrong.

Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

Does that short and sweet progress bar code work with dircopy also?

Buck

Sure, just make sure the target directory exists:

_FileCopy("C:\Installed Apps\Patches\*.*","C:\temp\test")

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

OK, I figured out a few things. That although I installed the Beta version, I was running uner 3.1.1.0. I found a toggle script to activate 3.1.1.124 and a few things became clearer. I also found out what SciTE is and I installed it. I now better understand the earlier posts.

While running under the Beta I do not get the earlier error. However, the file does not copy. Not that the progress bar does not appear, the file itself does not copy. The icon flashes on the taskbar and that's it. I tried a file \windows\FILE to a \windows\temp\FILE then a \windows\temp\FILE to a renamed \windows\temp\FILE2. The file just does not copy.

Then, I run this line alone, FileCopy("C:\Windows\temp\test3.tmp","C:\windows\temp\peter1.pjf")

the file does copy. But, as soon as I include the rest of the script, it just flashes and nothing gets done.

Any idea what is going wrong?

Sorry for all the trouble.

PeterF

Link to comment
Share on other sites

  • Developers

OK, I figured out a few things. That although I installed the Beta version, I was running uner 3.1.1.0. I found a toggle script to activate 3.1.1.124 and a few things became clearer. I also found out what SciTE is and I installed it. I now better understand the earlier posts.

While running under the Beta I do not get the earlier error. However, the file does not copy. Not that the progress bar does not appear, the file itself does not copy. The icon flashes on the taskbar and that's it. I tried a file \windows\FILE to a \windows\temp\FILE then a \windows\temp\FILE to a renamed \windows\temp\FILE2. The file just does not copy.

Then, I run this line alone, FileCopy("C:\Windows\temp\test3.tmp","C:\windows\temp\peter1.pjf")

the file does copy. But, as soon as I include the rest of the script, it just flashes and nothing gets done.

Any idea what is going wrong?

Sorry for all the trouble.

PeterF

np... can you show the script you have thats failing so i can have a look and test ?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

hey JdeB,

i am only trying to see it work right now. Other than file location, I think it is a copy of your post.

Is It?

Peter

;~ 4 Do not display a progress dialog box.

;~ 8 Give the file being operated on a new name in a move, copy, or rename operation if a file with the target name already exists.

;~ 16 Respond with "Yes to All" for any dialog box that is displayed.

;~ 64 Preserve undo information, if possible.

;~ 128 Perform the operation on files only if a wildcard file name (*.*) is specified.

;~ 256 Display a progress dialog box but do not show the file names.

;~ 512 Do not confirm the creation of a new directory if the operation requires one to be created.

;~ 1024 Do not display a user interface if an error occurs.

;~ 2048 Version 4.71. Do not copy the security attributes of the file.

;~ 4096 Only operate in the local directory. Don't operate recursively into subdirectories.

;~ 9182 Version 5.0. Do not copy connected files as a group. Only copy the specified files.

FileCopy("C:\Windows\temp\test3.tmp","C:\windows\temp\peter1.pjf")

Func _FileCopy($fromFile,$tofile)

Local $FOF_RESPOND_YES = 16

Local $FOF_SIMPLEPROGRESS = 256

$winShell = ObjCreate("shell.application")

$winShell.namespace($tofile).CopyHere($fromFile,$FOF_RESPOND_YES)

EndFunc

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