Adding text to every end of line in a .txt file? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Fefint
Posts: 28
Joined: 18 Aug 2017, 09:27

Adding text to every end of line in a .txt file?

18 Aug 2017, 10:15

Been working on a script half the day, but I got suck.

I'm using ctrl+c to copy text to the clipboard, then I paste it to a txt file, I sort it and remove all duplicates. But now I need to count them as ,red1, red2, etc. at the eol. But I can't find how to add it.

so I get 10 Address's 1 to 10, at the end I want to add ",red%x%" to the end, where x is a counter.
result would be,

Address here,red1
Address there,red2
Address overthere,red3
...

Here is what I got.

Code: Select all

SPEED = 2 ; mouse movement speed
MOUSE_OVER_DELAY = 20 ; delay after mouse moved
CLIPBOAD_DELAY = 50 ; delay after ctrl + c pressed
TOP_RIGHT_CORNER_X = 30 ; Top right X coordinate of stash
TOP_RIGHT_CORNER_Y = 235 ; Top right Y coordinate of stash
_InvOffsetX = 50
_InvOffsetY = 50

a::
t = red
Gosub GET_ADDRESS ; OK
Gosub SORT_IT ; NOK
return

GET_ADDRESS:
i = 1
while (i < 33){
    j = 1
    clipboard := ""
    while (j < 33){
	MouseMove, TOP_RIGHT_CORNER_X + _InvOffsetX*(j-1), TOP_RIGHT_CORNER_Y + _InvOffsetY*(i-1), SPEED
        Sleep, MOUSE_OVER_DELAY
        Send ^c
        Sleep, CLIPBOAD_DELAY
		if (clipboard == ""){
        }
        else{
		var := RegExReplace(clipboard, "s)---..*$|^.*?\R|\R") ;remove all but two first lines, remove first line, remove enter.
		FileAppend, 
		(
		%var%`n
		), %t%.txt
	}
        j := j + 1        
    }
    i := i + 1
}
return

SORT_IT:
x = 0

FileRead, Contents, %t%.txt
if not ErrorLevel
{
    Sort, Contents, u
    FileAppend, %Contents%, %t%.txt
	
	;add red%x% to eol here.
	
	FileAppend, 
	(
	`n
	
	), classes.txt
    Contents =  ; Free the memory.
}
return
I think I would need something like
Loop, read, start, %t%.txt
{
x++
loop, parse, A_LoopReadLine, %A_Tab%
{
if(A_Index==`r`n){
,red%x%`r`n=%A_LoopField%
}
}
FileAppend, %start%, %t%.txt

But I can't get it to work. Anyone know a simple way to add this?
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Adding text to every end of line in a .txt file?  Topic is solved

18 Aug 2017, 10:29

Code: Select all

Loop, Read,% t ".txt"
      lines .= A_LoopReadLine . ", red" . A_Index . "`n"
MsgBox % lines
Not tested.
Fefint
Posts: 28
Joined: 18 Aug 2017, 09:27

Re: Adding text to every end of line in a .txt file?

18 Aug 2017, 11:58

That worked great, but if I wanted to add %t% instead of "red". How would I do it then?

Code: Select all

Loop, Read,% t ".txt"
	lines .= A_LoopReadLine . ", " . %t% . A_Index . "`n"
FileAppend, %lines%, classes.txt
This didn't work as expected.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Adding text to every end of line in a .txt file?

18 Aug 2017, 14:33

Fefint wrote:That worked great, but if I wanted to add %t% instead of "red". How would I do it then?

Code: Select all

Loop, Read,% t ".txt"
	lines .= A_LoopReadLine . ",  %t%" . A_Index . "`n" ; using %t% literaly
FileAppend, %lines%, classes.txt

Code: Select all

Loop, Read,% t ".txt"
	lines .= A_LoopReadLine . ",  " . t . A_Index . "`n" ; using t as a variable 
FileAppend, %lines%, classes.txt

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, JoeWinograd, Luan_86, mikeyww, peter_ahk and 110 guests