Jump to content

Determining Progress Bar Increments


Recommended Posts

I have a program that copies pictures from a flash drive to the My Pictures directory.

My problems is that right now i do 2 search queries for each type of file, one to determine how many files of each type and one in the copy function.

Does anyone know a good way to determine how many increments my progress bar will need without having to do a query to determine the number of files?

I could easily remove the first query to speed up the program, but then it is impossible to determine the number of increments that will be needed. I could be copying 20 files or 400 files.

The intent of the program is to be used with an SD card, so if there is a way to determine number of files on disk, that may be close enough.

Any help is appreciated.

Link to comment
Share on other sites

I have a program that copies pictures from a flash drive to the My Pictures directory.

My problems is that right now i do 2 search queries for each type of file, one to determine how many files of each type and one in the copy function.

Does anyone know a good way to determine how many increments my progress bar will need without having to do a query to determine the number of files?

I could easily remove the first query to speed up the program, but then it is impossible to determine the number of increments that will be needed. I could be copying 20 files or 400 files.

The intent of the program is to be used with an SD card, so if there is a way to determine number of files on disk, that may be close enough.

Any help is appreciated.

For this kind of usage, I typically do a _FileListToArray() then use that as a dynamic feed into a calculation function to determine Progress Bar "stepping" through the percentages.

- MoChr(77)& Chr(97)& Chr(100)& Chr(101)& Chr(32)& Chr(121)& Chr(97)& Chr(32)& Chr(108)& Chr(111)& Chr(111)& Chr(107)-------I've told you 100,000 times not to exaggerate!-------Don't make me hit you with my cigarette hand...-------My scripts:Random Episode Selector, Keyboard MouseMover, CopyPath v2.1, SmartRename for XP,Window Tracer[sup]New![/sup]

Link to comment
Share on other sites

If i understand you correctly then the problem with that is that it takes far longer to do the _FileListToArray then it does to copy the files which leaves the program appearing as though it is doing nothing for a few seconds. Then it copies all the files so quickly that there is no need for a progress bar.

I want the progress bar to show the progress of the search and the copy.

BTW i am using slightly different method for the file search.

Here is my code.

Opt("GUIOnEventMode", 1)

;-------------------------Includes--------------------------------------------
#Include <GUIConstants.au3>
#Include <File.au3>
#Include <Array.au3>
#include <string.au3>


;-------------------------Variables--------------------------------------------
;Global Const $CB_SETCURSEL = 0x14E

Global $sAlbumName
Global $iProgress
Global $gcProgressBar
Global $gcProgressLabel
Global $increment
Global $cardDrive
Global $drives
Global $top = 50
Global $left = 40
Global $offset = 40

;-------------------------Gui Creation--------------------------------------------
$wMain = Guicreate("Picture Transfer", 300, 300)
GUISetOnEvent($GUI_EVENT_CLOSE, "bClose")

GUICtrlCreateLabel("Select the drive that has your pictures:", $left, $top, 200, 20)
$cDrive = GUICtrlCreateCombo("", $left, $top + ($offset/2), 150, 20)

$bTransfer = GUICtrlCreateButton("Transfer Pictures", $left, $top + ($offset*2), 100)
GUICtrlSetTip(-1, "This will ask for an album name then transfer all pics on the selected drive" & @CRLF & "to My Pictures\Album. (NOTE: Drive must be named ""PICTURES"")")
GUICtrlSetOnEvent(-1, "Transfer")

$bRefresh = GUICtrlCreateButton("Refresh", $left, $top + ($offset*3), 50)
GUICtrlSetTip(-1, "This will check for any newly added drives.")
GUICtrlSetOnEvent(-1, "Refresh")

$bRename = GUICtrlCreateButton("Rename Drive", $left, $top + ($offset*4), 100)
GUICtrlSetTip(-1, "This change the name of the selected drive to ""PICTURES"".")
GUICtrlSetOnEvent(-1, "Rename")
;~ $drives[0] = 3
;~ $drives[1] = "g:"
;~ $drives[2] = "a:"
;~ $drives[3] = "c:"

;ConsoleWrite("----1----" & @CRLF)

Refresh()
GUICtrlSetState($bTransfer, $GUI_FOCUS)
GUISetState()
;Refresh()


While 1
    Sleep(100)
WEnd



;-------------------------Functions--------------------------------------------
Func bClose()
    Exit
EndFunc

Func Rename()
    ConsoleWrite("GetCardDrive() = " & GetCardDrive() & @CRLF)
    DriveSetLabel(GetCardDrive(), "PICTURES")
    Refresh()
EndFunc

Func Refresh()
    ResetCombo($cDrive)
    $drives = GetDrives()
    If $drives[0] <> 0 Then
        GUICtrlSetState($bTransfer, $GUI_ENABLE)
        For $i = 1 to $drives[0]
            GUICtrlSetData($cDrive, StringUpper($drives[$i]) & " """ & DriveGetLabel($drives[$i])& """")
        Next
        SetComboSelection($cDrive, 0)
        ;GUICtrlSetData(-1, "", $drives[1] & "\" & DriveGetLabel($drives[1]))
    Else
        GUICtrlSetData($cDrive, "No Cards Detected", "No Cards Detected")
        GUICtrlSetState($bTransfer, $GUI_DISABLE)
    EndIf
EndFunc

Func GetDriveLabel()        
    Return StringUpper(DriveGetLabel($drives[GetComboSelection($cDrive)+1]))
EndFunc

Func Transfer()
    $driveLabel = GetDriveLabel()
    If $driveLabel = "PICTURES" Then
    
        $sAlbumName = InputBox("Picture Transfer", "Please Enter Album Name", Default, Default, 300, 120)
        
        If Not @error Then
            
            $increment = 100/GetFileTotal()
        
            $wProgress = GUICreate("Copying...", 500, 70, Default, Default, $WS_CAPTION)
            $gcProgressLabel = GUICtrlCreateLabel("", 10, 10, 480, 40)
            $gcProgressBar = GUICtrlCreateProgress(10, 40, 480, 20, $PBS_SMOOTH)
            GUISetState(@SW_SHOW, $wProgress)
            
            If DirCreate(@MyDocumentsDir & "\My Pictures\" & $sAlbumName) Then
                TransferFiles("avi")
                TransferFiles("jpg")
                TransferFiles("bmp")
                TransferFiles("wav")
                TransferFiles("mov")            
            Else
                MsgBox(48, "Error!", "Could not create album.")
            EndIf
            GuiDelete($wProgress)
        EndIf
        $iProgress = 0
    Else
        MsgBox(48, "No Memory Card", "This memory card is not labeled as PICTURES.")
    EndIf
EndFunc

Func TransferFiles($sFileType)
    Local $aFileList
    $aFileList = _FileSearch(GetCardDrive() & "*." & $sFileType, 1) 
    ;_ArrayDisplay($aFileList)

    ConsoleWrite("UBound($aFileList) = " & UBound($aFileList) & @CRLF)
    
    For $i = 1 to UBound($aFileList)-1
        $iProgress = $iProgress + $increment
        GUICtrlSetData($gcProgressLabel, "Copying " & @CRLF & $aFileList[$i])
        FileCopy($aFileList[$i],@MyDocumentsDir & "\My Pictures\" & $sAlbumName & "\" & $sAlbumName & $i & "." & $sFileType)
        ConsoleWrite("$aFileList[$i] = " & $aFileList[$i] & @CRLF)
        GUICtrlSetData ($gcProgressBar,$iProgress)
        ConsoleWrite("$iProgress = " & $iProgress & @CRLF)
        ConsoleWrite("Dir = " & @MyDocumentsDir & "\My Pictures\" & $sAlbumName & "\" & $sAlbumName & $i & "." & $sFileType & @CRLF)
    Next
EndFunc
    
Func GetFileTotal()
;~  Local $total
;~  $array = _FileSearch(GetCardDrive() & "*." & "avi", 1)
;~  $total = $total + $array[0]
;~  $array = _FileSearch(GetCardDrive() & "*." & "jpg", 1)
;~  If $array[0] = 0 Then
;~      ConsoleWrite("No jpgs!!!!!!!!!!!" & @CRLF)
;~  EndIf
;~  $total = $total + $array[0]
;~  $array = _FileSearch(GetCardDrive() & "*." & "bmp", 1)
;~  $total = $total + $array[0]
;~  $array = _FileSearch(GetCardDrive() & "*." & "wav", 1)
;~  $total = $total + $array[0]
;~  $array = _FileSearch(GetCardDrive() & "*." & "mov", 1)
;~  $total = $total + $array[0]
    Return 300 ;$total
EndFunc

;~ Func GetCardDrive()
;~  Local $aDrives
;~  Local $sCardDrive = "None"
;~  
;~  
;~  $aDrives  = DriveGetDrive("removable")
;~  ;ConsoleWrite("$aDrives[1] = " & $aDrives[2] & @CRLF)
;~  
;~  If Not @error then
;~      For $drive = 1 to $aDrives[0]
;~          If StringUpper(DriveGetLabel($aDrives[$drive])) = "PICTURES" Then
;~              $sCardDrive = $aDrives[$drive]
;~          EndIf
;~      Next
;~  EndIf   
;~  
;~  If $sCardDrive = "None" Then
;~      MsgBox(48, "No Memory Card", "There is no memory card labeled PICTURES currently inserted.")
;~  EndIf
;~  
;~  
;~  ConsoleWrite("$sCardDrive = " & $sCardDrive & @CRLF)
;~  
;~  Return $sCardDrive
;~ EndFunc

Func GetCardDrive()
    ;Uncomment when done testing with USB drive!!
    Return $drives[GetComboSelection($cDrive)+1]
    ;Return "G:\Documents\Pictures\"
EndFunc

Func _FileSearch($sQuery, $iSubdir=0)
    ConsoleWrite("query = " & $sQuery & @CRLF)
    $iLine = 0
    $sLine = ""
    $aLine = ""
    Dim $aFiles[100000]
    $aFiles[0] = 0
    $sArguments = "/a-d /b /on"
    If $iSubDir Then $sArguments = $sArguments & " /s"
    $aRaw = Run(@ComSpec & ' /c dir "' & $sQuery & '" ' & $sArguments, @SystemDir, @SW_HIDE, $STDOUT_CHILD)
    While 1
        $sLine = StdoutRead($aRaw)
        If @error Then ExitLoop
        $aLine = StringSplit($SLine, @CRLF)
        If $aLine[0] > 1 Then
            For $i = 1 To $aLine[0] - 1
                If $aLine[$i] = "" Then Continueloop
                $iLine = $iLine + 1
                $aFiles[$iLine] = $aLine[$i]
            Next
        ElseIf $aLine[0] = 1 Then
            If $aLine[1] = "" Then Continueloop
            If $aLine[1] = "File Not Found" Then Exitloop
            $iLine = $iLine + 1
            $aFiles[$iLine] = $aLine[1]
        Else
            ExitLoop
        EndIf
    Wend
    $aFiles[0] = $iLine
    ReDim $aFiles[$iLine + 1]
    Return $aFiles
EndFunc

Func GetDrives()
    Local $nonFloppydrives[1]
    Local $removableDrives
    $removableDrives = DriveGetDrive( "removable" )
    If NOT @error Then
        For $i = 1 to $removableDrives[0]
            $key = Regread("HKLM\System\MountedDevices","\DosDevices\" &  $removableDrives[$i])
            If not StringInStr(_HexToString(StringReplace ($key, "00", "")),"Floppy") Then
                _ArrayAdd($nonFloppydrives, $removableDrives[$i])
            EndIf
        Next
        $nonFloppydrives[0] = UBound($nonFloppydrives) - 1
    EndIf
    return $nonFloppydrives
EndFunc

Func SetComboSelection($hWnd, $iIndex = -1)
    If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
    
    Return _SendMessage($hWnd, $CB_SETCURSEL, $iIndex)
EndFunc 

Func GetComboSelection($hWnd)
    If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
    
    Return _SendMessage($hWnd, $CB_GETCURSEL)
EndFunc

Func ResetCombo($hWnd)
    If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
    
    _SendMessage($hWnd, $CB_RESETCONTENT)
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...