Jump to content

Progress Bar and Loops (Solved)


 Share

Recommended Posts

Hi All,

I want to make my progress bar progress with the amount of File lines read. 
How would I do this correctly. So far the code below can sometimes run in the middle of the progress bar and state completed.. and other times it can run into the 200%. 
 

;========================================================================
ProgressOn("TITLE", "ACTION")
;========================================================================
For $count = 1 To _FileCountLines($FileRead) Step 1
    $string = FileReadLine($FileRead, $count)
    $value1 = $input[1]
    $value2 = $input[2]
    $value3 = $input[3]
    $value4 = $input[4]

    $TM = FileWrite("C:\temp\test.txt", $value1 & " " & $value2 & " " & $value3 & " " & $value4 & @CRLF)
    ProgressSet($count, $count & "%")
    
Next
;========================================================================
;                               PROGRESS BAR OFF
;========================================================================
ProgressSet(_FileCountLines($FileRead), "Completed!")
Sleep(750)
ProgressOff()
;========================================================================

 

Kind Regards
Skeletor

"Coffee: my defense against going postal."

Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI

Link to comment
Share on other sites

.. more or less something like this:

;========================================================================
ProgressOn("TITLE", "ACTION")
;========================================================================
$iRows = _FileCountLines($FileRead)
For $count = 1 To $iRows Step 1
    $string = FileReadLine($FileRead, $count)
    $value1 = $input[1]
    $value2 = $input[2]
    $value3 = $input[3]
    $value4 = $input[4]

    $TM = FileWrite("C:\temp\test.txt", $value1 & " " & $value2 & " " & $value3 & " " & $value4 & @CRLF)
    
     $iPercent = Int($count / $iRows * 100)
    
    ProgressSet($iPercent, $count  & "/" & $iRows & @TAB & "(" & $iPercent & "%)")
    
Next
;========================================================================
;                               PROGRESS BAR OFF
;========================================================================
ProgressSet($iPercent, "Completed!")
Sleep(750)
ProgressOff()
;========================================================================

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

  • Skeletor changed the title to Progress Bar and Loops (Solved)
  • 10 months later...

With Binary..

;File read progress bytes
#include <FileConstants.au3>
#include <WinAPIFiles.au3>
Local Const $KB = 1024
Local Const $CHUNKBYTES = 64 * $KB ;64 kb each read

Local $sFileName = SelectFile()
If Not FileExists($sFileName) Then Exit

Local $hFileRead = FileOpen($sFileName, $FO_BINARY) ;open in binary read mode

;file is already open get filesize this way instead of FileGetSize()
FileSetPos($hFileRead, 0, $FILE_END)
Local $iFileSizeBytes = FileGetPos($hFileRead)
Local $iFileSizeKB = Int($iFileSizeBytes / $KB)
Local $iRead, $sBinary, $iPercent
Local $FileData = ""

;========================================================================
ProgressOn("TITLE", "ACTION", "", Default, Default, $DLG_MOVEABLE)
;========================================================================

$iRead = 0
FileSetPos($hFileRead, $iRead, $FILE_BEGIN) ;Start reading from the pos $iRead in the file
ConsoleWrite("Reading " & $iFileSizeBytes - $iRead & " Bytes" & @CRLF)

;Note: Filesize limited by string size limit..
While $iRead < $iFileSizeBytes
    $iReadChunk = $CHUNKBYTES
    If $iReadChunk + $iRead > $iFileSizeBytes Then $iReadChunk = $iFileSizeBytes - $iRead
    $sBinary = FileRead($hFileRead, $iReadChunk)
    $iRead += @extended
    If @extended = 0 Or @error Then ExitLoop
    $FileData &= StringTrimLeft($sBinary, 2) ;remove 0x

    $iPercent = Int($iRead / $iFileSizeBytes * 100)

    If $iFileSizeKB > 10 Then
        ProgressSet($iPercent, Int($iRead / $KB) & "/" & $iFileSizeKB & "KB (" & $iPercent & "%)")
    Else
        ProgressSet($iPercent, $iRead & "/" & $iFileSizeBytes & "B (" & $iPercent & "%)")
    EndIf
WEnd
$FileData = "0x" & $FileData ;Need put 0x back
FileClose($hFileRead)

;Local $sConversionANSI = BinarytoString($FileData, $SB_ANSI)
;Local $sConversionUTF8= BinarytoString($FileData, $SB_UTF8)

ConsoleWrite("Writing data to " & @TempDir & "\test.txt" & @CRLF)
Local $hFileWrite = FileOpen(@TempDir & "\test.txt", BitOR($FO_OVERWRITE, $FO_BINARY))
FileWrite($hFileWrite, $FileData)
FileClose($hFileWrite)

;========================================================================
;                               PROGRESS BAR OFF
;========================================================================
Sleep(750)
ProgressSet($iPercent, "Completed!")
Sleep(750)
ProgressOff()
;========================================================================

Func SelectFile() ;From the helpfile
    ; Create a constant variable in Local scope of the message to display in FileOpenDialog.
    Local Const $sMessage = "Select a single file of any type."

    ; Display an open dialog to select a file.
    Local $sFileOpenDialog = FileOpenDialog($sMessage, @WindowsDir & "\", "All (*.*)", $FD_FILEMUSTEXIST) ;"

    If @error <> 0 Then
        ;Display the error message.
        MsgBox($MB_SYSTEMMODAL, "", "No file was selected.")

        ;Change the working directory (@WorkingDir) back to the location of the script directory as FileOpenDialog sets it to the last accessed folder.
        FileChangeDir(@ScriptDir)
    Else
        ;Change the working directory (@WorkingDir) back to the location of the script directory as FileOpenDialog sets it to the last accessed folder.
        FileChangeDir(@ScriptDir)
    EndIf
    ConsoleWrite($sFileOpenDialog & @CRLF)
    Return $sFileOpenDialog
EndFunc

 

Edited by Bilgus
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

×
×
  • Create New...