Loop through info in a file

Get help with using AutoHotkey and its commands and hotkeys
Drowseth
Posts: 2
Joined: 23 Feb 2017, 07:10

Loop through info in a file

23 Feb 2017, 07:19

Hi,

I want to create a script that loops through information in a text file.

The text file looks like this:

Code: Select all

acc1 char1 char2 char3
acc2 char1 char2 char3
And the idea is to pull the first ID on row 1 (acc1), and then loop through the rest of the IDs on the same line (char1,char2,char3). And then move to line 2 and do the same.
Kinda like this, although I know the syntax is incorrect for AHK.

Code: Select all

for 1 to accounts
{
 for 1 to characters
  {
  do something
  }
}
Anyone able to assist in how I can read the text file into a script, and process the IDs in it in such a loop? Thanks in advance.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Loop through info in a file

23 Feb 2017, 08:15

Try this:

Code: Select all

SetWorkingDir, %A_ScriptDir%

; write example file to work with
FileDelete, example.txt
FileAppend, acc1 char1 char2 char3`nacc2 char1 char2 char3`n, example.txt

; read the file
FileRead, Content, example.txt
MsgBox, %Content% ; show content

; process the content
Loop, Parse, Content, `n, `r ; line by line
{
    Loop, Parse, A_LoopField, %A_Space% ; word by word
    {
        MsgBox, %A_LoopField% ; do something
    }
}
I hope that helps.
Drowseth
Posts: 2
Joined: 23 Feb 2017, 07:10

Re: Loop through info in a file

23 Feb 2017, 08:56

Thanks, I will try that!

Could you please explain what

Code: Select all

`n, `r
does? Do I understand it correctly that `n means new line and is used as the delimiter and `r means page break and is omitted?

Also, if I wanted to return which line in the file I was currently on, how would I put that?
User avatar
jNizM
Posts: 2503
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Loop through info in a file

23 Feb 2017, 08:58

LoopParse wrote:Specifying `n prior to `r allows both Windows and Unix files to be parsed.

Ref:
commands/LoopParse.htm (see Example #2)
[AHK] 1.1.30.03 x64 Unicode | [WIN] 10 Pro (Version 1903) x64 | [GitHub] Profile
Donations are appreciated if I could help you
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Loop through info in a file

23 Feb 2017, 09:29

Try this:

Code: Select all

SetWorkingDir, %A_ScriptDir%

; write example file to work with
FileDelete, example.txt
FileAppend, acc1 char1 char2 char3`nacc2 char1 char2 char3`n, example.txt

; read the file
FileRead, Content, example.txt
MsgBox, %Content% ; show content

; process the content
Loop, Parse, Content, `n, `r ; line by line
{
    LineNum := A_Index
    Loop, Parse, A_LoopField, %A_Space% ; word by word
    {
        MsgBox, Line #%LineNum%:`t%A_LoopField% ; do something
    }
}
I hope that helps.

Don't forget you can click keywords in the code box to look up the relevant help page.
TravisQ
Posts: 15
Joined: 17 May 2015, 23:51

Re: Loop through info in a file

23 Feb 2017, 12:25

Maybe some of these ideas could be usefull

Code: Select all

SetWorkingDir,%A_ScriptDir%
Content=
(
acc1 char1 char2 char3
acc2 char21 char22 char23
)
Loop,Parse,Content,`n,`r 
{
	RegExMatch(A_LoopField,"([^\s]+)",fword)
	RegExMatch(A_LoopField,"(?<=\s)(.*)",match)
	MsgBox,3,,Line %A_Index% is :`n%fword% `n`n`trun or skip   (press Yes or No)
	IfMsgBox Yes
		Loop,Parse,match,%A_Space% 
		{
			MsgBox,%A_LoopField% ; do something
		}
	IfMsgBox No
		continue
	IfMsgBox Cancel
		break
} 
ExitApp

Return to “Ask For Help”

Who is online

Users browsing this forum: Flipeador, flyingDman, gigiiiii, Google [Bot], imnotgay, NewberEnerNeeder1, Permagrin, Sanbenito, x-tremespeed and 66 guests