Page 1 of 1

Help with Progress bar

Posted: 13 Aug 2017, 02:20
by Tomer
hii
i guess this may be very simple for you to solve:

i would like have the progrss bar to be completed (100%) once the reading of list file is finished,
problem is the progress bar is completed to 100% before the read-list is finished.

Code: Select all

Loop, read, c:\file.txt
{
    Progress, %a_index%, %A_LoopReadLine%, Installing..., Draft Installation
    Sleep, 50
}
thanks for any help..

Re: Help with Progress bar  Topic is solved

Posted: 13 Aug 2017, 03:52
by Helgef
Hello.
You need to set the progress in relation to the number of lines of the file. If you have 100 lines in file.txt, your code will end at 100 % progress. I'd do something like this,

Code: Select all

fileRead, fileContent, file.txt
strReplace(fileContent,"`n","`n",lineCount) ; Count the number of lines

Loop, Parse, fileContent, `n, `r
{
    Progress, % p:= round(100 * (A_Index-1) / lineCount,2), %A_LoopField% `n %p% `%, Installing..., Draft Installation
    Sleep, 250
}
Cheers

Re: Help with Progress bar

Posted: 13 Aug 2017, 04:14
by Tomer
Thanks Helgef,
thats working great.
i also liked the percentage show.

i came up with this code too,
which works for me good as well but its looks more complicated,
what your prefers\thoughts ?

Code: Select all

1 = file.txt

; this not necessary for my qustion
FileRead, Text, %1%
Loop, Parse, Text, `n, `r
    Lines := A_Index
;

count=0
Loop, read, %1%
   files:=(files ? "`n" : "") . A_LoopFileFullPath,count:=count+1

percentage := 100 / count
start := 0

Loop, read, %1%
{

increment := ++start
result := percentage * increment

Progress,  %result%, %A_LoopReadLine%, comps found: %a_index%\%lines%
sleep 250

}

sleep 200
progress, off

exitapp

Re: Help with Progress bar

Posted: 13 Aug 2017, 05:14
by Helgef
Hello.
Short answer: if it is working for you, great :thumbup:
Slightly less short answer: It is a little wasteful to count the lines twice, that is, both Lines and count, holds the number of lines. Since you read the file with FileRead, Text, %1%, you might aswell use loop, parse instead of loop, read, to avoid reading the file again. I do not see where you use files:=(files ? "`n" : "") . A_LoopFileFullPath. Maybe that is part of a bigger script.

Cheers.

Re: Help with Progress bar

Posted: 13 Aug 2017, 06:54
by garry
thank you for the examples , here an example with GUI

Code: Select all

f1=%a_scriptdir%\test.txt
ifnotexist,%f1%
{
loop,150
  {
  i++
  e .= "This is LineXX=" . i . "`r`n"
  }
fileappend,%e%,%f1%
}
WA=%A_screenwidth%
wdt :=(WA*99)/100   ;width    guishow width
Gui,2: Font, default, FixedSys
Gui,2 :Add, Progress, x10 y10 w%wdt% h20 vPRBAR cFF7200 range0-100
Gui,2: Add, Text,     x10 y35 w%wdt% h20  vText22,
Gui,2: Add, Text,     x10 y60 w%wdt% h20  vText23,
Gui,2: Add, Button,   x10 y90 w130   h22  gStart,PRBAR-TEST
Gui,2:Show,h120 , Progress-Bar
return

start:
Gui,2:submit,nohide
FileRead, Text, %f1%
total=0
a    =0
res1 =0
c=FF7200
GuiControl,2: +c%c%, prbar
Loop, Parse, Text, `n, `r
    total := (A_Index-1)
Loop, Parse, Text, `n, `r
    {
    lf=%a_loopfield%
    if lf=
       continue
    A +=1
    RES1:=Round((100*A)/TOTAL)
    GuiControl,2:,PRBAR, %RES1%
    GuiControl,2:,Text22,%A%  from TOTAL=%TOTAL%       %RES1% `%
    GuiControl,2:,Text23,Line-%a%=%LF%
    sleep,50
    }
;GuiControl,2:,Text23,ENDED
text=
;- test changecolor prbar
c=Gray
GuiControl,2: +c%c%, prbar
return
2Guiclose:
exitapp
esc::exitapp
;==========================================================================

Re: Help with Progress bar

Posted: 13 Aug 2017, 11:56
by Tomer
Very impressive garry! Will check it out at work tomorrow.
Thanks!!

Re: Help with Progress bar

Posted: 13 Aug 2017, 12:07
by Helgef
Nice indeed garry. :wave:

Re: Help with Progress bar

Posted: 20 Aug 2017, 06:32
by Tomer
garry ive tryed to use your sample for my project,
when i use it to read only 1 file its works great (although i had to change total := (A_Index-1) tototal := (A_Index) only when the reading file doesnt have blank line at the end),
anyway i have a problem to use it while reading 2 or more separately files,
the "percents" goes over 100% and the list goes over its total round.

i tryed "reset" the gui controls so it may start a new counting but i failed to make it work..
i have added an smaple code so you can look out help me solving this issue.
tnx!

Code: Select all

f1=%a_scriptdir%\test.txt
ifnotexist,%f1%
{
loop,150
  {
  i++
  e .= "This is LineXX=" . i . "`r`n"
  }
fileappend,%e%,%f1%
}

f2=%a_scriptdir%\test2.txt
ifnotexist,%f2%
{
loop,150
  {
  i++
  e .= "This is LineXX2=" . i . "`r`n"
  }
fileappend,%e%,%f2%
}

gui,2: -SysMenu
Gui,2: Font,s14, Comic Sans MS
Gui,2: Add, Text, vMSG x10 y90 h30 w500, loading...
Gui,2 :Add, Progress, x10 y10 w500 h20 vPRBAR cFF7200 range0-100
Gui,2: Add, Text, x10 y35 w500 h20 vText22,
Gui,2: Add, Text, x10 y60 w500 h20 vText23,
Gui,2: Add, Button, vStop gStop x210 y115 w100 h22,stop
; focus on other control-id to unfocus the default button (prevent pressing the space button to select it)
GuiControl,2: Focus, msg
;
Gui,2:Show,h140 , test123


FileRead, Text, %f1%
Loop, Parse, Text, `n, `r
    total := (A_Index-1)
Loop, Parse, Text, `n, `r
    {
    lf=%a_loopfield%
    if lf=
       continue
    A +=1
    RES1:=Round((100*A)/TOTAL)
    GuiControl,2:,PRBAR, %RES1%
    GuiControl,2:,Text22,%A%\%TOTAL%       %RES1% `%
    GuiControl,2:,Text23,%LF%
    ;MsgBox, %a_loopfield%
    sleep, 1
    }
;GuiControl,2:,Text23,ENDED
text=
;- test changecolor prbar
c=Gray
GuiControl,2: +c%c%, prbar


FileRead, Text, %f2%
Loop, Parse, Text, `n, `r
    total := (A_Index-1)
Loop, Parse, Text, `n, `r
    {
    lf=%a_loopfield%
    if lf=
       continue
    A +=1
    RES1:=Round((100*A)/TOTAL)
    GuiControl,2:,PRBAR, %RES1%
    GuiControl,2:,Text22,%A%\%TOTAL%       %RES1% `%
    GuiControl,2:,Text23,%LF%
    sleep, 1
    }
;GuiControl,2:,Text23,ENDED
text=
;- test changecolor prbar
c=Gray
GuiControl,2: +c%c%, prbar


GuiControl,2: hide, stop
GuiControl,2: hide, msg
Gui,2: Font,s14 c0x008000, Comic Sans MS
Gui,2: Add, Text, x10 y90 h30 w500, finsihed!
Gui,2: Add, Button, gExit x210 y115 w100 h22,exit
Gui,2: Font,
return

Stop:
Gui,2:submit,nohide
MsgBox 0x2004, , cancel ?

IfMsgBox Yes, {
MsgBox, Canceled!
} Else IfMsgBox No, {
   GuiControl,2: Focus, msg
   return
}

Exit:
2Guiclose:
exitapp
;==========================================================================

Re: Help with Progress bar

Posted: 20 Aug 2017, 07:17
by garry
shoul'd clear variables or set to zero
example reads first file (0-100%) than changes color from progressbar and reads second file ( 0-100% )

Code: Select all

;-------- https://autohotkey.com/boards/viewtopic.php?f=5&t=35793 ---
i=0
f1=%a_scriptdir%\test1.txt
ifexist,%f1%
  filedelete,%f1%   ;- in test
ifnotexist,%f1%
{
loop,150
  {
  i++
  e .= "This is LineXX=" . i . "`r`n"
  }
fileappend,%e%,%f1%
}
e=

i=0
f2=%a_scriptdir%\test2.txt
ifexist,%f2%
  filedelete,%f2%   ;- in test
ifnotexist,%f2%
{
loop,300
  {
  i++
  e .= "This is LineXX2=" . i . "`r`n"
  }
fileappend,%e%,%f2%
}
e=

gui,2: -SysMenu
Gui,2: Font,s14, Comic Sans MS
Gui,2: Add, Text, vMSG x10 y90 h30 w500, loading...
Gui,2 :Add, Progress, x10 y10 w500 h20 vPRBAR cFF7200 range0-100
Gui,2: Add, Text, x10 y35 w500 h20 vText22,
Gui,2: Add, Text, x10 y60 w500 h20 vText23,
Gui,2: Add, Button, vStop gStop x210 y115 w100 h22,stop
; focus on other control-id to unfocus the default button (prevent pressing the space button to select it)
GuiControl,2: Focus, msg
;
Gui,2:Show,h140 , test123


FileRead, Text, %f1%
total=0
a=0
Loop, Parse, Text, `n, `r
    total := (A_Index-1)
Loop, Parse, Text, `n, `r
    {
    lf=%a_loopfield%
    if lf=
       continue
    A +=1
    RES1:=Round((100*A)/TOTAL)
    GuiControl,2:,PRBAR, %RES1%
    GuiControl,2:,Text22,%A%\%TOTAL%       %RES1% `%
    GuiControl,2:,Text23,%LF%
    ;MsgBox, %a_loopfield%
    sleep, 50
    }
;GuiControl,2:,Text23,ENDED
text1=
;- test changecolor prbar
c=Gray
GuiControl,2: +c%c%, prbar


FileRead, Text2, %f2%
total=0
a=0
Loop, Parse, Text2, `n, `r
    total := (A_Index-1)
Loop, Parse, Text2, `n, `r
    {
    lf=%a_loopfield%
    if lf=
       continue
    A +=1
    RES1:=Round((100*A)/TOTAL)
    GuiControl,2:,PRBAR, %RES1%
    GuiControl,2:,Text22,%A%\%TOTAL%       %RES1% `%
    GuiControl,2:,Text23,%LF%
    sleep, 50
    }
;GuiControl,2:,Text23,ENDED
text2=
;- test changecolor prbar
c=Gray
GuiControl,2: +c%c%, prbar


GuiControl,2: hide, stop
GuiControl,2: hide, msg
Gui,2: Font,s14 c0x008000, Comic Sans MS
Gui,2: Add, Text, x10 y90 h30 w500, finished!
Gui,2: Add, Button, gExit x210 y115 w100 h22,exit
Gui,2: Font,
return

Stop:
Gui,2:submit,nohide
MsgBox 0x2004, , cancel ?

IfMsgBox Yes, {
MsgBox, Canceled!
} Else IfMsgBox No, {
   GuiControl,2: Focus, msg
   return
}

Exit:
2Guiclose:
exitapp
;==========================================================================

Re: Help with Progress bar

Posted: 20 Aug 2017, 07:24
by Tomer
so simple,
tnx garry