Jump to content

DirCopy w/progress alternate


Recommended Posts

Hello,

I've seen dozens of examples on how to copy folders with a progress bar. But find that I don't want to incorporate the code in my very simple/tiny scripts.

Anyhow, I have come up with this bit of code.

$sSourceFolder = "D:\!Drivers"
$iSourceSize = DirGetSize($sSourceFolder)
$sDestFolder = @DesktopDir & "\" & "TestDownloadFolder"
Run(@AutoItExe & ' /AutoIt3ExecuteLine "DirCopy(''' & $sSourceFolder & ''', ''' & $sDestFolder & ''')"')
ProgressOn("Progress Meter", "Increments every second", "0 percent")
Do
$iDestSize = DirGetSize($sDestFolder)
$iPercent = Int($iDestSize / ($iSourceSize / 100))
ProgressSet($iPercent, $iPercent & " percent")
Until $iPercent = 100
ProgressSet(100 , "Done", "Complete")
Sleep(500)
ProgressOff()

I would like to know what the risks/danger of using such code. I can only come up with is there is no error checking if the folder copied or not.

But I would image there or more risks I can't possible come up with.

I welcome any advice/help.

Thanks.

Link to comment
Share on other sites

I like this code but need a sleep to not to stress the processor and not blink percentage

$sSourceFolder = "D:\!Drivers"
$iSourceSize = DirGetSize($sSourceFolder)
$sDestFolder = @DesktopDir & "\" & "TestDownloadFolder"
Run(@AutoItExe & ' /AutoIt3ExecuteLine "DirCopy(''' & $sSourceFolder & ''', ''' & $sDestFolder & ''')"')
ProgressOn("Progress Meter", "Increments every second", "0 percent")
Do
$iDestSize = DirGetSize($sDestFolder)
$iPercent = Int($iDestSize / ($iSourceSize / 100))
ProgressSet($iPercent, $iPercent & " percent")
Sleep(200)
Until $iPercent = 100
ProgressSet(100 , "Done", "Complete")
Sleep(500)
ProgressOff()

Not to understand the part of the risks. Im a brazilian (PT-BR) xD

Edited by GordonFreeman
Link to comment
Share on other sites

In SMF I switched to Yashied's excellent " which utilizes a copy.dll file to perform the operations. The script stays responsive, the copy process can be interrupted and you can measure the progress too.

Link to comment
Share on other sites

In SMF I switched to Yashied's excellent " which utilizes a copy.dll file to perform the operations. The script stays responsive, the copy process can be interrupted and you can measure the progress too.

I second that suggestion too.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Not to mention that you can interrogate the return from the copy process in Yashied's code whereas in the technique that you use has no visibility to the spawned process, as you metioned in the OP.

However, if you elect to stay with your code the following may help:

- changed the code to a function to make it easier to add to code/maintain

- added delete for target dir as code will not work if target is not empty

- changed % calculation - I believe the general formula is int((part/whole)*100)

- changed to loop on the pid of the spawned process

;
; copy folder and subfolders with progress
;

_CopyFolder(@scriptdir, @scriptdir & '\Temp out folder')

func _CopyFolder($sSourceFolder,$sDestFolder)

    dirremove($sDestFolder,1)
    local $iSourceSize = DirGetSize($sSourceFolder), $iDestSize
    local $pid = Run(@AutoItExe & ' /AutoIt3ExecuteLine "DirCopy(''' & $sSourceFolder & ''', ''' & $sDestFolder & ''')"')
    ProgressOn("Copy Progress", "Please Wait...")
    Do
        $iDestSize = dirgetsize($sDestFolder)
        local $ipct = int(($iDestSize/$iSourceSize)*100)
        ProgressSet($ipct,$ipct & ' percent complete')
        sleep(20)
    Until not ProcessExists($pid)
    ProgressOff()

endfunc

Incidentally, your code shows that you are thinking and grasping the language, Good Job!

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

For efficency you should declare $ipct outside the loop.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

  • 4 months later...
  • 1 year later...

Hi, everyone.

First thanks for the script. but i have a problem when i compile the script. if i run the script pressing f5 it´s all right but when i compile the script the line:

Run(@AutoItExe & ' /AutoIt3ExecuteLine "DirCopy(''' & $sSourceFolder & ''', ''' & $sDestFolder & ''')"')

don´t do the spected, the progress bar stay freezed and don´t continue, in any line with the run command i need to say it the WorkingDir, and in this example i dónt see it, maybe i was wrong but i don´t understand my mistake in this line, i only copy and paste and later i modify the source and destination, nothing more.

why when i press f5 all it´s fine but when i compile all it´s wrong?

thanks.

Christian

Link to comment
Share on other sites

  • Developers

Do you mean that the line isn't executed so no files are copied?

What version are you running?

Have you added this at the top in case you run the latest version of AutoIt3?:

#pragma compile(AutoItExecuteAllowed, true)

Jos

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

Do you mean that the line isn't executed so no files are copied?

What version are you running?

Have you added this at the top in case you run the latest version of AutoIt3?:

#pragma compile(AutoItExecuteAllowed, true)

Jos

Exactly, the script pause the execution in the window of progress and don´t continue, it´s not copying anything.

but you are the men.

this line was the solution of my problems:

#pragma compile(AutoItExecuteAllowed, true)

now it´s working perfect.

my autoit version is

SciTE

Version 3.4.1

 

I don´t know how this line work, i need to read about it.

Thanks a Lot Jos for your contribution to my work.

Christian

Link to comment
Share on other sites

  • 2 years later...

Hi

I found the code from kylomas for DirCopy with progress (above) works great for me. Many thanks ! ! !

However, I have one small problem . . .

I'm using the code to copy Audacity recording files from C:\...\My Documents\Audacity\ to a USB (E:\ drive).

When I use the command: DirCopy($FilePath, $USBdrive, 1) normally (without progress), I can set $USBdrive to "E:\" and it all works fine.

HOWEVER, when I call the _CopyFolder fuction above from within my script, I have found the destination drive MUST have a drive letter AND a folder name specified.  Otherwise the script continues without copying anything!

ie  $sDestFolder MUST be set to something like: "E:\Audacity" - not just "E:\".

Any ideas ? ? ?

I can work around it but it would be nice to just use "E:\".

Link to comment
Share on other sites

I agree that it would be nice to use just "E:\" but you can't rely on the USB flash drive always being E:  To get around this, use the "DriveGetDrive" function and check to see if it is of type "REMOVEABLE"; flash drives (as well as SD cards) show up as REMOVEABLE.

Who lied and told you life would EVER be fair?

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