Sign in to follow this  
Followers 0
surreal

Progress in exciting GUI

5 posts in this topic

#1 ·  Posted (edited)

progress bar code that i have found, if there is better code or a better way to do this please let me know:

;~ This script is demonstrate copy process with custom designed progress displayed, the progress display details about the copy process.
;~ Author: G.Sandler a.k.a CreatoR
;~ Functions _DirListToArray() and _FileListToArrayEx() is originaly writen by amel27.
#include <Array.au3>
#include <File.au3>

_CopyWithProgress("C:\users\surreal\documents", "C:\temp\migrate", 1)

Func _CopyWithProgress($SourcePath, $DestPath, $Replace=0)
    If Not FileExists($SourcePath) Then Return SetError(1, 0, -1)
    If Not StringInStr(FileGetAttrib($DestPath), "D") And Not DirCreate($DestPath) Then Return SetError(2, 0, "")
    If $Replace <> 0 And $Replace <> 1 Then SetError(3, 0, "")

    Local $PathName = StringRegExpReplace($SourcePath, "^.*\\", "")
    Local $Progress=0, $Counter, $ReadySize, $MidlePath, $Ready, $TimeRemained
    Local $CurrentFilePath, $CurrentFileName, $CurrentFilePathName, $CurrentParentDirName

    ProgressOn("Copy Files...", "Copy: " & $PathName, "Getting dir structure" & @LF & "Please wait...")

    Local $TotalDirSize = DirGetSize($SourcePath)
    Local $FilesArr = _FileListToArrayEx($SourcePath)
    Local $FilesCount = UBound($FilesArr)-1
    Local $ProgressStep = 100 / $FilesCount

    If IsArray($FilesArr) Then
        For $i = 1 To UBound($FilesArr)-1
            $CurrentFilePath = $FilesArr[$i]
            $CurrentFileName = StringRegExpReplace($CurrentFilePath, "^.*\\", "")
            $CurrentFilePathName = StringReplace($CurrentFilePath, $SourcePath & "\", "")

            $CurrentParentDirName = _GetParentDirName($CurrentFilePath)

            $Progress += $ProgressStep
            $Counter += 1

            $ReadySize = FileGetSize($CurrentFilePath)

            $MidlePath = _GetMidlePath($CurrentFilePath)
            $Ready = $Counter & "/" & $FilesCount
            $TimeRemained = _GetTimeRemained($TotalDirSize, $ReadySize, $FilesCount, $Counter)

            ProgressSet($Progress, 'Copy... from "' & $CurrentParentDirName & '" to "' & $CurrentParentDirName & '"' & @LF & _
                $MidlePath & @LF & "Approximately Remained Time: " & $TimeRemained, "Ready: " & $Ready)
            FileCopy($CurrentFilePath, $DestPath & "\" & $CurrentFilePathName, 8+$Replace)
        Next
    EndIf
    ProgressOff()
EndFunc

Func _FileListToArrayEx($sPath, $sMask='*')
    Local $i, $j, $blist, $rlist[1]=[0], $dlist = _DirListToArray($sPath)
    _ArrayAdd ($dlist, $sPath)
    For $i=1 To $dlist [0] +1
        $blist = _FileListToArray ($dlist [$i], $sMask, 1)
        If Not @error Then
            For $j=1 To $blist [0]
                _ArrayAdd ($rlist, $dlist[$i] & "\" & $blist [$j])
            Next
        EndIf
    Next
    $rlist [0] = UBound ($rlist) - 1
    Return $rlist
EndFunc

Func _DirListToArray($sPath)
    Local $rlist[2]=[1, $sPath], $blist, $alist=_FileListToArray ($sPath, '*', 2)
    If IsArray ($alist) Then
        For $i=1 To $alist [0]
            _ArrayAdd ($rlist, $sPath & "\" & $alist [$i])
            $blist = _DirListToArray ($sPath & "\" & $alist [$i])
            If $blist[0]>0 Then
                For $j=1 To $blist [0]
                    _ArrayAdd ($rlist, $blist [$j])
                Next
            EndIf
        Next
    EndIf
    $rlist[0] = UBound($rlist) - 1
    Return $rlist
EndFunc

Func _GetMidlePath($sPath)
    If StringLen($sPath) <= 50 Then Return $sPath
    Local $StartPath = StringLeft($sPath, 25)
    Local $EndPath = StringTrimLeft($sPath, StringInStr($sPath, "\", 0, -2)-1)
    Return $StartPath & "..." & $EndPath
EndFunc

Func _GetParentDirName($FullName)
    Local $LastSlashPos = StringInStr($FullName, "\", 0, -1)
    Local $SecondLastSlashPos = StringInStr($FullName, "\", 0, -2)
    Return StringMid($FullName, $SecondLastSlashPos+1, $LastSlashPos-$SecondLastSlashPos-1)
EndFunc

Func _GetTimeRemained($TotalSize, $CurrentSize, $FilesCount, $CurrentFilesCount)
    Local $NumLevl = 0.5

    If $TotalSize <= $CurrentSize Then Return _SecsToTime(0)

    Switch $FilesCount - $CurrentFilesCount
        Case 0 To 100
            $NumLevl = 0.1
        Case 100 To 1000
            $NumLevl = 0.5
        Case 1000 to 2000
            $NumLevl = 1
        Case Else
            $NumLevl = 2
    EndSwitch

    $Secs = ($TotalSize * $NumLevl) / (3600 * $CurrentFilesCount) - ($CurrentSize * $NumLevl) / (3600 * $CurrentFilesCount)
    Return _SecsToTime($Secs)
EndFunc

Func _SecsToTime($iTicks, $Delim=":")
    If Number($iTicks) >= 0 Then
        $iHours = Int($iTicks / 3600)
        $iTicks = Mod($iTicks, 3600)
        $iMins = Int($iTicks / 60)
        $iSecs = Round(Mod($iTicks, 60))
        If StringLen($iHours) = 1 Then $iHours = "0" & $iHours
        If StringLen($iMins) = 1 Then $iMins = "0" & $iMins
        If StringLen($iSecs) = 1 Then $iSecs = "0" & $iSecs
        Return $iHours & $Delim & $iMins & $Delim & $iSecs
    EndIf
    Return SetError(1, 0, 0)
EndFunc

below is my script

at this time if you enter your local computer name for testing it will copy your my docs folder to c:\temp\migrate. you will also have to load and select what profile you want to copy. i would like to get the progress bar and file name listed. thank you everyone for your help.

#AutoIt3Wrapper_Run_Debug_Mode=Y
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>

Opt("GUIOnEventMode", 1)
Local $sTitle = 'Migrate'
Local $LStrComputer = "localhost"
Local $DWbemFlagReturnImmediately = 0x10
Local $DWbemFlagForwardOnly = 0x20
Local $cnTarget, $OS, $prCombo, $SISB, $SIAN, $xpdocs, $vistadocs, $CBMD, $CBLN, $CBIE, $CBDT

;===Checks Local OSVersion Create Var User Friendly================================================
$OS = @OSVersion
$OS = StringReplace($OS, "WIN_VISTA", "Windows Vista")
$OS = StringReplace($OS, "WIN_XP", "Windows XP")
$OS = StringReplace($OS, "WIN_2000", "Windows 2000")

;===Determines Documents Location According To OS++================================================
$OSDOC = @OSVersion
If $OSDOC ="WIN_VISTA" then $DOC = "c$\Users\"
If $OSDOC ="WIN_XP" then
$DOC = "c$\Documents and Setting\"
EndIf

$OSDF = @OSVersion
If $OSDF ="WIN_VISTA" then  $DF = "Documents"
If $OSDF ="WIN_XP" then
$DF = "My Documents"
EndIf

;===GUI Start and Close============================================================================
$Form = GUICreate($sTitle, 460, 253, -1, -1)
GUISetOnEvent($GUI_EVENT_CLOSE, "FormClose")

;===GUI Local System Information===================================================================
GUICtrlCreateGroup("System Information", 8, 8, 223, 113)
GUICtrlCreateLabel("Operating System:", 23, 30, 90, 15)
$SIOS = GUICtrlCreateLabel($OS, 118, 30, 100, 15)
GUICtrlCreateLabel("Computer Name:", 23, 47, 90, 15)
$SICN = GUICtrlCreateLabel(@ComputerName, 118, 47, 100, 15)
GUICtrlCreateLabel("IP Address:", 23, 64, 90, 15)
$SIIP = GUICtrlCreateLabel(@IPAddress1, 118, 64, 100, 15)
GUICtrlCreateLabel("IP Subnet:", 23, 81, 90, 15)
$SISB = GUICtrlCreateLabel("", 118, 81, 100, 15)
GUICtrlCreateLabel("Asset Number:", 23, 98, 90, 15)
$SIAN = GUICtrlCreateLabel("", 118, 98, 100, 15)

;===GUI Network Computer To Tranfer From===========================================================
GUICtrlCreateGroup("Computer To Tranfer From", 240, 8, 210, 80)
$cnTarget = GUICtrlCreateInput("", 248, 32, 129, 21)                      ;<----Remote Compter Name
$cnButton = GUICtrlCreateButton("Connect", 385, 30, 57, 25, $WS_GROUP)
GUICtrlSetOnEvent(-1, "Connect")
GUICtrlCreateLabel("Enter Computer Name or IP Address", 256, 64, 174, 15)

;===GUI Network Profile(s)=========================================================================
GUICtrlCreateGroup("Profile(s)", 240, 96, 210, 65)
$prCombo = GUICtrlCreateCombo("", 248, 120, 145, 25)                      ;<---------Remote Profile
$prButton = GUICtrlCreateButton("Load", 400, 120, 40, 25, $WS_GROUP)
GUICtrlSetOnEvent(-1, "Profile")

;===GUI Group What To Migrate======================================================================
GUICtrlCreateGroup("What To Migrate", 8, 128, 225, 73)
GUICtrlCreateLabel("My Documents", 40, 155, 80, 15)
$CBMD = GUICtrlCreateCheckbox("", 20, 155, 15, 15)
GUICtrlSetState(-1, $GUI_CHECKED)
GUICtrlCreateLabel("Lotus Notes", 40, 175, 80, 15)
$CBLN = GUICtrlCreateCheckbox("", 20, 175, 15, 15)
GUICtrlCreateLabel("IE Favorites", 140, 155, 80, 15)
$CBIE = GUICtrlCreateCheckbox("", 120, 155, 15, 15)
GUICtrlCreateLabel("Desktop", 140, 175, 80, 15)
$CBDT = GUICtrlCreateCheckbox("", 120, 175, 15, 15)

;===GUI Start And Stop Functions===================================================================
$StopButton = GUICtrlCreateButton("Stop", 257, 173, 73, 25, $WS_GROUP)
GUICtrlSetOnEvent(-1, "StopButton")
$StartButton = GUICtrlCreateButton("Start", 357, 173, 73, 25, $WS_GROUP)
GUICtrlSetOnEvent(-1, "StartButton")

;===GUI ProgressBar and Details====================================================================
$ProgressBar = GUICtrlCreateProgress(8, 208, 441, 15, $PBS_SMOOTH)
GUICtrlCreateLabel("Details:", 8, 232, 39, 15)
$Details = GUICtrlCreateLabel("", 50, 232, 39, 15)

GUISetState(@SW_SHOW)

;===Gather Local Dell Service Asset Tag For System Information Group===============================
$DObjWMIService = ObjGet("winmgmts:\\" & $LStrComputer & "\root\CIMV2")
$DColItems = $DObjWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct", "WQL", $DWbemFlagReturnImmediately + $DWbemFlagForwardOnly)
If IsObj($DColItems) Then
    For $DObjInput In $DColItems
        GUICtrlSetData($SIAN, $DObjInput.IdentifyingNumber)
    Next
EndIf

;===Gather Local Subnet Address For System Information Group=======================================
$NObjWMIService = ObjGet("winmgmts:\\" & $LStrComputer & "\root\CIMV2")
$NColItems = $NObjWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True", "WQL", $DWbemFlagReturnImmediately + $DWbemFlagForwardOnly)
If IsObj($NColItems) Then
    For $NObjInput In $NColItems
        GUICtrlSetData($SISB, $NObjInput.IPSubnet(0))
    Next
EndIf

;===WEND===========================================================================================
While 1
    Sleep(100)
WEnd

;===Function For Remote System InfoBox=============================================================
Func Connect()
    If GUICtrlRead($cnTarget) = '' Then
    MsgBox(16, $sTitle, 'Target machine must be entered.')
    EndIf

    If Ping(GUICtrlRead($cnTarget)) Then
        $strComputer = GUICtrlRead($cnTarget)
        $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
        ;===WMI BIOS
        $colItems1 = $objWMIService.ExecQuery("SELECT * FROM Win32_Bios")
        $message = "System Info for " & $strComputer & @CRLF & "-------" & @CRLF & @CRLF
        For $objItem In $colItems1
            $message &= "Dell Asset Number: " & $objItem.SerialNumber & @CRLF & _
                    "Manufacturer: " & $objItem.Manufacturer & @CRLF & @CRLF
        Next
        ;===WMI NETWORK
        $colItems2 = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
        For $objItem In $colItems2
            $message &= "IP Address: " & $objItem.IPAddress(0) & @CRLF & _
                    "IP Subnet: " & $objItem.IPSubnet(0) & @CRLF & @CRLF
        Next
        ;===WMI SYSTEM
        $colItems3 = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
        For $objItem In $colItems3
            $message &= "Model: " & $objItem.Model & @CRLF & _
                    "Computer Name: " & $objItem.Name & @CRLF & _
                    "Last User: " & $objItem.UserName & @CRLF & @CRLF
        Next

        MsgBox(4096, "System Information", $message)
    Else
        MsgBox(16, $sTitle, 'Target system could not be contacted, and may not be valid.')
    EndIf
EndFunc   ;==>Connect

;===Function For Remote Profile Load===============================================================
Func Profile()

    Local $combo_string = ""
    $FolderList = _FileListToArray("\\" & GUICtrlRead($cnTarget) & "\" & $DOC, "*", 2)
    For $i = 1 To $FolderList[0]
        Switch $FolderList[$i]
            Case "All Users", "Default User", "LocalService", "NetworkService"
                ContinueLoop
            Case Else
                If $i = $FolderList[0] Then
                    $combo_string &= $FolderList[$i]
                Else
                    $combo_string &= $FolderList[$i] & "|"
                EndIf
        EndSwitch
    Next
    GUICtrlSetData($prCombo, $combo_string)
EndFunc   ;==>Profile

;===Function For XP or Vista Copy Per Checked Box with GUI=========================================
Func StartButton()
    Local $rmdocs = "\\" & GUICtrlRead($cnTarget) & "\" & $DOC & GUICtrlRead($prCombo) & "\" & $DF
    Local $rmfavs = "\\" & GUICtrlRead($cnTarget) & "\" & $DOC & GUICtrlRead($prCombo) & "\Favorites"
    Local $rmlns = "\\" & GUICtrlRead($cnTarget) & "\c$\Program Files\Lotus\Notes\Data"
    Local $rmdstp = "\\" & GUICtrlRead($cnTarget) & "\" & $DOC & GUICtrlRead($prCombo) & "\Desktop"

    If GUICtrlRead($CBMD) = $GUI_CHECKED Then
        DirCopy($rmdocs, "c:\Temp\Migrate\" & $DF, 1)
    EndIf
    If GUICtrlRead($CBIE) = $GUI_CHECKED Then
        DirCopy($rmfavs, "c:\Temp\Migrate\Favorites", 1)
    EndIf
    If GUICtrlRead($CBLN) = $GUI_CHECKED Then
        DirCopy($rmlns, "c:\Temp\Migrate\Notes", 1)
    EndIf
    If GUICtrlRead($CBDT) = $GUI_CHECKED Then
        DirCopy($rmdstp, "c:\Temp\Migrate\Desktop", 1)
    EndIf
    MsgBox(4, $sTitle, "Migration Was Successful. Do You Want To Exit?")
    Exit
EndFunc   ;==>StartButton

;===Function For Stop Button Message===============================================================
Func StopButton()
    MsgBox(4, $sTitle, "Are you really sure you want to Stop?")
    Exit
EndFunc   ;==>StopButton

;===Function For Gui Close Message=================================================================
Func FormClose()
    MsgBox(0, $sTitle, "Are you really sure you want to cancel?")
    Exit
EndFunc   ;==>FormClose

i also found this one, not sure witch one is better to use. it seems as if it may be easier to included into my script and i like the dual bars. however im not sure if its as clean of a copy, any help would be greatly appreciated.

#include <GUIConstants.au3>
ProgressCopy("c:\users\surreal\documents\", "c:\temp\migrate\",1)

Func ProgressCopy($current, $destination, $attrib = "-R", $overwrite = 1 ,$Run1 = 0 )

;FirstTimeRun Get original DirSize and set up Gui
    If $Run1 = 0 Then
        Global $OverallQty, $Overall, $source, $overallpercent, $Progress0Text, $progressbar1, $Progress1Text, $progressbar2, $Progress2Text,  $LocalPercent
        If not FileExists ($Destination) then DirCreate ($Destination)
        $source = $current
        If StringRight($current, 1) = '\' Then $current = StringTrimRight($current, 1)
        If StringRight($destination, 1) <> '\' Then $destination = $destination & "\"
        $tosearch = $current
        $Overall = DirGetSize($tosearch, 1)
        $OverallQty = $Overall[1]
        Global Const $PrCopyGui = GUICreate("Copying Files", 420, 100, -1, -1, -1)
        $Progress0Text = GUICtrlCreateLabel("Please Wait", 10, 5, 400, 20)
        $progressbar1 = GUICtrlCreateProgress(10, 20, 400, 20)
        GUICtrlSetColor(-1, 32250)
        $Progress1Text = GUICtrlCreateLabel("", 10, 44, 400, 20)
        $progressbar2 = GUICtrlCreateProgress(10, 60, 400, 20)
        $Progress2Text = GUICtrlCreateLabel("", 10, 82, 400, 20)
        GUISetFont(10, 600)
        GUISetState(@SW_SHOW)
        GUICtrlSetData($Progress1Text, "Working Directory " & $tosearch)
        $Run1 = 1
    EndIf

    $Size = DirGetSize($current, 3)
    $Qty = $Size[1]
    Local $search = FileFindFirstFile($current & "\*.*")
    While 1
        Dim $file = FileFindNextFile($search)
        If @error Or StringLen($file) < 1 Then ExitLoop
        If Not StringInStr(FileGetAttrib($current & "\" & $file), "D") And ($file <> "." Or $file <> "..") Then
            $Qty -= 1
            $LocalPercent = 100 - (($Qty / $Size[1]) * 100)
            $OverallQty -= 1
            $overallpercent = 100 - (($OverallQty / $Overall[1]) * 100)
            GUICtrlSetData($Progress0Text, "Total Progress " & Int($overallpercent) & "% completed")
            GUICtrlSetData($progressbar1, $overallpercent)
            GUICtrlSetData($progressbar2, $LocalPercent)
            GUICtrlSetData($Progress2Text, "Copying File " & $file)

            FileCopy($current & "\" & $file, $destination & StringTrimLeft($current, StringLen($source)) & "\" & $file,$overwrite)
            FileSetAttrib($destination & StringTrimLeft($current, StringLen($source)) & "\" & $file, $attrib)
        EndIf
        If StringInStr(FileGetAttrib($current & "\" & $file), "D") And ($file <> "." Or $file <> "..") Then
            DirCreate($destination & StringTrimLeft($current, StringLen($source)) & "\" & $file)
            FileSetAttrib($destination & StringTrimLeft($current, StringLen($source)) & "\" & $file, $attrib)
            GUICtrlSetData($Progress1Text, $current & "\" & $file)
            ProgressCopy($current & "\" & $file, $destination, $attrib, $overwrite,1)
        EndIf
    WEnd
    FileClose($search)
;when overall percent = 100 set end gui text, delete gui and reset run1 to 0
    If $overallpercent = 100 Then
        GUICtrlSetData($Progress0Text, "Total Progress 100% completed")
        GUICtrlSetData($progressbar1, 100)
        GUICtrlSetData($progressbar2, 100)
        GUICtrlSetData($Progress2Text, "Done!")
        Sleep(2000)
        GUIDelete($PRCopyGui)
        $Run1 = 0
    EndIf
EndFunc  ;==>ProgressCopy

d@ve

Edited by surreal

Share this post


Link to post
Share on other sites



Great! I had a couple questions about file because I want to do something very similar to this and your script has most of it already done!

Did you ever get any further in adding the progress bar?

I plan on adding moving drive mappings and printers and some other things as well but this is a great start.

Thanks to you (and Autoit) for saving me time!

Share this post


Link to post
Share on other sites

Welcome to the forum, ppie4me!

I'm sorry to say you might have to wait a while for an answer from surreal regarding this 2009 post, as he has not been online since last April. I'm sure that a forum search will turn up many other (and more recent) progress-bar-related posts for you to peruse..

Good luck :graduated:

Share this post


Link to post
Share on other sites

Thanks. I noticed there was a zip file with some includes but it won't unpack for me. I've seen several demos of progress bars, most of them involve counting loops. There's some that work when downloading from Internet explorer. I was looking for something when copying directories - possible hundreds of files and up to 15gig so a visual bar would be nice. I'll keep looking.

Autoit seems like a great program and I just updated scite so now I have the gui designer.

Thanks!

Share this post


Link to post
Share on other sites

There is a bunch of code like that in the Example Scripts forum.

Run some searches in there. I believe Yashied's Copy UDF has a progress bar, or RichE's XCopy GUI, or llewxam's Folder Sync Tool, and probably many others. If you put together a script that's giving you trouble, post it (in a new thread) and we'll be glad to help you with it.

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