fix a script about the progress bar Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
crypter
Posts: 90
Joined: 15 Dec 2020, 09:57

fix a script about the progress bar

Post by crypter » 19 Jun 2021, 18:53

I have a script that inserts text lines from source text file to text files with/under the text "[content]"

i added

Code: Select all

Gui,Add,Progress,x10 y10 w461 range0-%MaxLines% vMyProgressBar
Gui,Show
and

Code: Select all

GuiControl,,MyProgressBar, +1
the progress bar doesn't seem to work

here's the script

Code: Select all

#NoEnv
SetBatchLines, -1

SourcePath := "C:\New folder (3)\New folder\source.txt"
TargetPath := "C:\New folder (3)\New folder (2)" ; NO CLOSING \
TargetExt := "txt" ; File type
Gui,Add,Progress,x10 y10 w461 range0-%MaxLines% vMyProgressBar
Gui,Show

FileRead, SourceData, %SourcePath%
SourceLines := StrSplit(SourceData, "`r`n")
MaxLines := SourceLines.Length()

Loop Files, %TargetPath%\*.%TargetExt%
{
   If (A_Index > MaxLines)
      ErrorExit("More files than source lines (" . MaxLines . ")!")
   If !(FileObj := FileOpen(A_LoopFileLongPath, "rw"))
      ErrorExit("Could not open the target file " . A_LoopFileLongPath . "!")
   FileContent := FileObj.Read()
   If (Pos := RegExMatch(FileContent, "Pim)^\[content\]$", Len)) {
      FileObj.Pos := Pos + Len - 1
      FileObj.Write("`r`n" . SourceLines[A_Index])
      FileObj.Write(SubStr(FileContent, Pos + Len))
	GuiControl,,MyProgressBar, +1
   }
   FileObj.Close()
   If (Pos = 0)
      ErrorExit(A_LoopFileLongPath . "`r`ncould not find the [content] line!")
}


ErrorExit(Msg) {
   MsgBox, 16, ERROR!, %Msg%
   ExitApp
}
i attached the text files and ahk script
Attachments
New folder (3).rar
(275.43 KiB) Downloaded 6 times

doubledave22
Posts: 343
Joined: 08 Jun 2019, 17:36

Re: fix a script about the progress bar  Topic is solved

Post by doubledave22 » 19 Jun 2021, 20:51

Hi. Looks like you are building the progressbar range before initializing the variable MaxLines. See below

Code: Select all

#NoEnv
SetBatchLines, -1

SourcePath := "C:\New folder (3)\New folder\source.txt"
TargetPath := "C:\New folder (3)\New folder (2)" ; NO CLOSING \
TargetExt := "txt" ; File type

FileRead, SourceData, %SourcePath%
SourceLines := StrSplit(SourceData, "`r`n")
MaxLines := SourceLines.Length()  ; <-------- look below

Gui,Add,Progress,x10 y10 w461 range0-%MaxLines% vMyProgressBar   ; <-------- We need to know the MaxLines variable before Progress bar is built
Gui,Show

Loop Files, %TargetPath%\*.%TargetExt%
{
   If (A_Index > MaxLines)
      ErrorExit("More files than source lines (" . MaxLines . ")!")
   If !(FileObj := FileOpen(A_LoopFileLongPath, "rw"))
      ErrorExit("Could not open the target file " . A_LoopFileLongPath . "!")
   FileContent := FileObj.Read()
   If (Pos := RegExMatch(FileContent, "Pim)^\[content\]$", Len)) {
      FileObj.Pos := Pos + Len - 1
      FileObj.Write("`r`n" . SourceLines[A_Index])
      FileObj.Write(SubStr(FileContent, Pos + Len))
	GuiControl,,MyProgressBar, +1
   }
   FileObj.Close()
   If (Pos = 0)
      ErrorExit(A_LoopFileLongPath . "`r`ncould not find the [content] line!")
}


ErrorExit(Msg) {
   MsgBox, 16, ERROR!, %Msg%
   ExitApp
}

crypter
Posts: 90
Joined: 15 Dec 2020, 09:57

Re: fix a script about the progress bar

Post by crypter » 20 Jun 2021, 12:56

thanks a lot, it works

Post Reply

Return to “Ask for Help (v1)”