File append in specific line (Possible?)

Get help with using AutoHotkey and its commands and hotkeys
asdads
Posts: 13
Joined: 07 Feb 2018, 09:45

File append in specific line (Possible?)

19 Jun 2018, 08:33

Hello,

I have one simple question..
How can I use "FileAppend" to write a certain text in a certain line?

Thanks :D
wolf_II
Posts: 2271
Joined: 08 Feb 2015, 20:55

Re: File append in specific line (Possible?)

19 Jun 2018, 08:50

Try this:
  • run the script
  • press F12 three times
  • check the effect of 3 insertions at Line #8 into the script

Code: Select all

Count := 0


;-------------------------------------------------------------------------------
F12:: InsertLine("; Hi " ++Count, A_ScriptFullPath, 8) ; hotkey for testing
;-------------------------------------------------------------------------------



;-------------------------------------------------------------------------------
InsertLine(Line, File, Pos) { ; insert a Line of text into File at Pos
;-------------------------------------------------------------------------------
    FileRead, Content, %File%

    ; split Content into Front and Back
    Loop, Parse, Content, `n, `r
        If (A_Index < Pos)
            Front .= A_LoopField "`n"
        Else
            Back .= A_LoopField "`n"

    FileDelete, %File%
    FileAppend, % Front Line "`n" Back, %File%
}
I hope that helps.
Guest

Re: File append in specific line (Possible?)

19 Jun 2018, 09:37

If you need to do more there is the TF library with 40+ functions incl. TF_InsertLine @ https://github.com/hi5/TF
User avatar
jeeswg
Posts: 5435
Joined: 19 Dec 2016, 01:58
Location: UK

Re: File append in specific line (Possible?)

19 Jun 2018, 11:29

With InStr you can get the nth occurrence of a string.
asdads
Posts: 13
Joined: 07 Feb 2018, 09:45

Re: File append in specific line (Possible?)

20 Jun 2018, 09:38

wolf_II wrote:Try this:
  • run the script
  • press F12 three times
  • check the effect of 3 insertions at Line #8 into the script

Code: Select all

Count := 0


;-------------------------------------------------------------------------------
F12:: InsertLine("; Hi " ++Count, A_ScriptFullPath, 8) ; hotkey for testing
;-------------------------------------------------------------------------------



;-------------------------------------------------------------------------------
InsertLine(Line, File, Pos) { ; insert a Line of text into File at Pos
;-------------------------------------------------------------------------------
    FileRead, Content, %File%

    ; split Content into Front and Back
    Loop, Parse, Content, `n, `r
        If (A_Index < Pos)
            Front .= A_LoopField "`n"
        Else
            Back .= A_LoopField "`n"

    FileDelete, %File%
    FileAppend, % Front Line "`n" Back, %File%
}
I hope that helps.
First of all thanks for the answer but..

Thats good but not that what i need :/

Let´s say i have two files called text.txt and insert.ahk, i open insert and it insert the text into text.txt

I already tryed F12:: InsertLine("; Hi " MyPath, 8)

But i get an Error message
wolf_II
Posts: 2271
Joined: 08 Feb 2015, 20:55

Re: File append in specific line (Possible?)

20 Jun 2018, 12:01

Try F12:: InsertLine("; Hi", MyPath, 8) notice the two , around MyPath.
asdads
Posts: 13
Joined: 07 Feb 2018, 09:45

Re: File append in specific line (Possible?)

21 Jun 2018, 07:38

wolf_II wrote:Try F12:: InsertLine("; Hi", MyPath, 8) notice the two , around MyPath.
How can i just show you my love ?, thanks <3

Return to “Ask For Help”

Who is online

Users browsing this forum: robodesign, Spikea and 103 guests