Jump to content

Progress Bar


wu36
 Share

Recommended Posts

I'm trying to figure out how to put in a progress/status bar that counts the number of files it finds/edits. Or even a status bar that slowly increases as it nears finish. Any help would be appreciated. Thanks

#include <File.au3>

dim $ServerName = "@oldserver.net"

dim $NewServerName = "@newserver.net"

dim $Repo = "oldrepo"

dim $NewRepo = "newrepo"

dim $IP = "@192.168.1.1"

$RegKey = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"

If RegRead($RegKey, "Hidden") = 2 Then

RegWrite($RegKey, "Hidden", "REG_DWORD", 1)

Else

EndIf

$find = _findfile('root')

Func _findfile($file)

$path = InputBox("Test Tool", "Enter the pathname", "C:\Temp")

If $path <> '' And StringRight($path, 1) <> '\' Then $path = $path & '\'

If $path = '' Then $path = @HomeDrive & '\'

Local $output = Run(@ComSpec & " /c " & 'dir ' & '"' & $path & '' & '*.*" /b /s', '', @SW_HIDE, 2)

While 1

$stdout = StdoutRead($output)

If @error Then ExitLoop

$found = StringSplit(StringStripCR($stdout), @LF)

For $i = 1 To UBound($found) - 1

If StringRight($found[$i], StringLen($file) + 1) = '\' & $file Then myfunc($found[$i])

Next

WEnd

EndFunc

Func myfunc($var)

If $var Then

_ReplaceStringInFile( $var, $ServerName, $NewServerName)

_ReplaceStringInFile( $var, $Repo, $NewRepo)

_ReplaceStringInFile( $var, $IP, $NewServerName)

EndIf

EndFunc

$RegKey = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"

If RegRead($RegKey, "Hidden") = 1 Then

RegWrite($RegKey, "Hidden", "REG_DWORD", 2)

Else

EndIf

MsgBox(4096, "Update", "Update Complete", 5)

Link to comment
Share on other sites

I'm trying to figure out how to put in a progress/status bar that counts the number of files it finds/edits. Or even a status bar that slowly increases as it nears finish. Any help would be appreciated. Thanks

Try the ProgressOn() function and related functions. See the help file for an example. If you can't figure that out, please post your attempt at the progress bar so people can figure out what went wrong. (Note that you will need to calculate somehow on which percentage you are, you will need to send that to any progress bar you might want to create anyway.)

(And as a side note, please put your posted code in autoit tags, makes your post much more readable.)

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

As soon as I posted I noticed that you replied. Before I initially posted I was searching under 'status bar' and 'counter' which were not turning up good results. As soon as I searched for 'progress' I found what I needed.

Thanks

Link to comment
Share on other sites

As soon as I posted I noticed that you replied. Before I initially posted I was searching under 'status bar' and 'counter' which were not turning up good results. As soon as I searched for 'progress' I found what I needed.

Thanks

And thank you for the feedback! Saves other people's time...

And yes we posted at exactly the same minute so we couldn't have known. :whistle:

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Try the ProgressOn() function and related functions. See the help file for an example. If you can't figure that out, please post your attempt at the progress bar so people can figure out what went wrong. (Note that you will need to calculate somehow on which percentage you are, you will need to send that to any progress bar you might want to create anyway.)

(And as a side note, please put your posted code in autoit tags, makes your post much more readable.)

Working out the percentage is the problem; youcan't know how many files there are until there is no more output from the dir function. The best might be to just have a small form with the number of files counting up by one every time myfunc is called say.
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Working out the percentage is the problem; youcan't know how many files there are until there is no more output from the dir function. The best might be to just have a small form with the number of files counting up by one every time myfunc is called say.

Now you read each output line and parse it, that's why you don't know beforehand. But you could probably easily change that to be able to use percentage counting. Look at how I changed your main function. (Note that this is ofcourse completely untested since I don't have your test data, but this should probably work, barring some typo's and/or small logic errors like looping once too many or something.)

Func _findfile($file)
    $path = InputBox("Test Tool", "Enter the pathname", "C:\Temp")
    If $path <> '' And StringRight($path, 1) <> '\' Then $path = $path & '\'
    If $path = '' Then $path = @HomeDrive & '\'
    Local $output = Run(@ComSpec & " /c " & 'dir ' & '"' & $path & '' & '*.*" /b /s', '', @SW_HIDE, 2)

    Dim $a_Stdout[] ; create an array to store all Stdout lines in

    While 1 ; first read the whole output into an array
        $stdout = StdoutRead($output)
        If @error Then ExitLoop
        $counter += 1
        ReDim $a_Stdout[$counter] ; increase the array size by 1 for the new line
        $a_Stdout[$counter-1] = $stdout ; put line 1 in element 0, line 2 in element 1, etc.
    WEnd

; now parse the whole array, and make a percentage counter
    
    $onePercent = UBound($a_Stdout)/100 ; this is 1% of total, so you can calculate percentage in loop below
    $pctCntr = 0
    $oldPctCntr = 0
    
    ProgressOn("Percent counter","","") 
    For $looper = 0 To UBound($a_Stdout)-1 ; loop through array
        
        $pctCntr = Int($looper/$onePercent) ; calculate current percentage
        If $pctCntr > $oldPctCntr Then ; percentage has increased by at least 1, so increase progress bar
            ProgressSet($pctCntr)
            $oldPctCntr = $pctCntr
        EndIf
        
        $found = StringSplit(StringStripCR($a_Stdout[$looper]), @LF)
        For $i = 1 To UBound($found) - 1
            If StringRight($found[$i], StringLen($file) + 1) = '\' & $file Then myfunc($found[$i])
        Next
        
    Next
    ProgressOff
EndFunc   ;==>_findfile

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Just tried to read in a directory of my own, seems that stdoutread does not read per line :whistle: Meaning that you will probably have to for instance stringsplit the complete output that you read first, to put it in lines, right before the onepercent counter is made... Oh well, the general idea stays the same.

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Martin - How would you go about creating that form?

Well don't I feel useless now :whistle:

(Note to self: stop writing useful examples... :P)

If you just want a counter so you still don't know what percentage but can read out what line you are on, just use GUICtrlCreateLabel to create a label in your main GUI, then use GuiCtrlSetData to write another text to the label, like GUICtrlSetData($counterLabel,$lineCounter). Be sure to have the counter increase with every line (meaning every iteration of the loop).

Hope this is more useful than example code that just about shows exactly what you asked in the first post :D

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Hah. Dont feel useless. I was just curious about how to create martins idea. I appreciate the help you have given me Sadbunny =)

Never mind, just kidding :whistle: It is my own choice to invest time in anything, you're not to blame for that :P Happy scripting!

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Sadbuddy - What would $counterLabel,$lineCounter look like within the script that I posted in the beginning? Would the line counter be something like $lineCounter = $var + 1?

WTF! I have been so stupid. With some extra changes, my suggestions should work, but I see now it all can be done with way less hassle and less changes. You see, $i is actually already your lineCounter variable! Only you start $i over when new output is found.

But, since an array containing all lines has already been created after stringSplit, the only thing you still need to do is first read the whole output and then make a progressbox. But you don't need all that redim stuff.

You want to try it or can I make up for the mess and give it another try BUT first I'll test it on a sample file before I barge in? :">

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Sadbuddy - What would $counterLabel,$lineCounter look like within the script that I posted in the beginning? Would the line counter be something like $lineCounter = $var + 1?

But, to answer your question, in your For $i loop, you would need a counter like $lineCounter += 1, and in your main GUI, a label created by $counterLabel = GUICtrlCreateLabel(...preferences...)

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

bump

Well this shows both a counterlabel and a progressbar :whistle:

(But if all you wanted to do in the first place was a findfile function, there are nice recursive functions available in this forum (I like the one Smoke made) which you could copy and edit to include a counterlabel... So this is just for exampling the two things discussed earlier :P

#include <File.au3>

$mainGui = GUICreate("Reading output from DIR command",400,100)
$counterLabel = GUICtrlCreateLabel("Amount of stdoutReadings: ",10,10,380,30)
GUISetState()

$stdout = ""

$find = _findfile('niu.exe')

; if this point is reached, file has not been found
MsgBox (16,"File not found","File not found")
Exit

Func _findfile($file)
    $path = InputBox("Test Tool", "Enter the pathname", "C:\")
    If $path <> '' And StringRight($path, 1) <> '\' Then $path = $path & '\'
    If $path = '' Then $path = @HomeDrive & '\'
    Local $output = Run(@ComSpec & " /c " & 'dir ' & '"' & $path & '' & '*.*" /b /s', '', @SW_HIDE, 2)
    $counter = 0
    While 1  ; <-- this whole loop is only for the DIR command not outputting everything at once!
        $stdout &= StdoutRead($output) ; <-- any output will be added to what was already read
        $counter += 1
        If $counter / 100 = Int($counter / 100) Then ; <-- only updates the label when $counter is divisible by 100, makes it a lot faster and better looking, not doing this calculation and the label altogether is WAAAY faster though :)
            GUICtrlSetData($counterLabel,"Amount of stdoutReadings: "&$counter)
        EndIf
        If @error Then ExitLoop
    WEnd

    GUIDelete($mainGui)
    
    $found = StringSplit(StringStripCR($stdout), @LF)  ; <-- the $found array will contain ALL output from the DIR command
    
    ProgressOn("Lines read", "Lines Read: ", "", "", "", 18)
    $pct = 0
    $1pct = UBound($found) / 100
    For $i = 1 To UBound($found) - 1
        If Int($i / $1pct) > $pct Then  ; <-- Percentage is at least 1 bigger than previous percentage so update progress bar
            $pct += 1
            ProgressSet($pct, "That means percentage count: " & $pct & " %", "Lines read: " & $i & " of " & UBound($found) - 1)
        EndIf
        If StringRight($found[$i], StringLen($file) + 1) = '\' & $file Then myfunc($found[$i])
    Next
    ProgressOff()
EndFunc   ;==>_findfile

Func myfunc($text)
    MsgBox(64 + 262144, "test", "file found: " & $text & ". Exiting.")
    Exit
EndFunc   ;==>myfunc

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

When I try the progress bar it continues to flash in a loop instead of increasing with each file found. I'm not sure how to correct this.

Func _findfile($file)

$path = InputBox("CVS Migration Tool", "Enter the pathname of the Agent Gateway CVS Repository", "C:\SCM")

If $path <> '' And StringRight($path, 1) <> '\' Then $path = $path & '\'

If $path = '' Then $path = @HomeDrive & '\'

Local $output = Run(@ComSpec & " /c " & 'dir ' & '"' & $path & '' & '*.*" /b /s', '', @SW_HIDE, 2)

While 1

$stdout = StdoutRead($output)

If @error Then ExitLoop

$found = StringSplit(StringStripCR($stdout), @LF)

ProgressOn("Updated", "Lines Read: ", "", "", "", 18)

$pct = 0

$1pct = UBound($found) / 100

For $i = 1 To UBound($found) - 1

If Int($i / $1pct) > $pct Then ; <-- Percentage is at least 1 bigger than previous percentage so update progress bar

$pct += 1

ProgressSet($pct, "That means percentage count: " & $pct & " %", "Files Changed: " & $i & " of " & UBound($found) - 1)

EndIf

If StringRight($found[$i], StringLen($file) + 1) = '\' & $file Then myfunc($found[$i])

Next

ProgressOff()

WEnd

EndFunc

Link to comment
Share on other sites

When I try the progress bar it continues to flash in a loop instead of increasing with each file found. I'm not sure how to correct this.

That's because you loop from 0% - 100% through every read output of stdOutRead. Since StdOutRead will read max 64 kb of data (see help), you will have countless amounts of loops because a big dir output is many times 64 kb. So for every 64 kb of output, you will make a 0%-100% loop in that piece of data only, then you start over.

As mentioned before, you cannot know how many lines it will read before you finish, so you just cannot make a progress bar WHILE reading a big output before you know where it ends, because then you never know the percentage. If you have the full array and THEN loop through it you know where it ends so you can can calculate the percentage.

Roses are FF0000, violets are 0000FF... All my base are belong to you.

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