add multiple source paths to a script with one source path 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

add multiple source paths to a script with one source path

20 Jun 2021, 15:00

i have this script, it has a SourcePath already and i need to add 4 more source paths (5 total source paths) that are ran by order one by one

i need to add

Code: Select all

SourcePath := "C:\New folder (3)\New folder\source1.txt"
SourcePath := "C:\New folder (3)\New folder\source2.txt"
SourcePath := "C:\New folder (3)\New folder\source3.txt"
SourcePath := "C:\New folder (3)\New folder\source4.txt"
SourcePath := "C:\New folder (3)\New folder\source5.txt"
right now i have this

Code: Select all

SourcePath := "C:\New folder (3)\New folder\source.txt"
this is 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


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
}
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: add multiple source paths to a script with one source path  Topic is solved

20 Jun 2021, 15:20

Try something like this:

Code: Select all

#NoEnv
SetBatchLines, -1

SourcePaths := ["C:\New folder (3)\New folder\source1.txt"
	, "C:\New folder (3)\New folder\source2.txt"
	, "C:\New folder (3)\New folder\source3.txt"
	, "C:\New folder (3)\New folder\source4.txt"
	, "C:\New folder (3)\New folder\source5.txt"]
TargetPath := "C:\New folder (3)\New folder (2)" ; NO CLOSING \
TargetExt := "txt" ; File type

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

	Gui, Destroy
	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
}

If the only difference between the various file names is the number on the end, then it can just be done with a loop without the array. The array approach lets you cycle through them even if their names aren't similar.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Frogrammer, gongnl, scriptor2016 and 277 guests