Delete a line Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Reloaded
Posts: 283
Joined: 25 Aug 2017, 08:48

Delete a line

08 Jan 2019, 07:24

Hey!,

Lets say i have a .ahk file and i want to delete a line Until the end of the line, its hard to say but i show you how i mean ._.
Here is a def that i found, but its just add a new line. Maybe its help you.

Code: Select all

; test 123
; another just for fun

; And the def
InsertLine(Line, File, Pos) {
    FileRead, Content, %File%
    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%
}
And i want to delete line one until the " ; " but i dont get it to work!

Thankss
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: Delete a line

08 Jan 2019, 08:11

damn this is something I tried too, it was always overlapping with the next content in the file when i tried to replace a line. I'll follow other replies
User avatar
Reloaded
Posts: 283
Joined: 25 Aug 2017, 08:48

Re: Delete a line

08 Jan 2019, 08:23

DRocks wrote:
08 Jan 2019, 08:11
damn this is something I tried too, it was always overlapping with the next content in the file when i tried to replace a line. I'll follow other replies
Jeah.. i search like every day for a answer but found nothing yet :d. When you find something tell me xD
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: Delete a line  Topic is solved

08 Jan 2019, 08:51

do you mean something like this?

Code: Select all

DeleteLine(File, Pos) {
    FileRead, Content, %File%
    Loop, Parse, Content, `n, `r
        If (A_Index != Pos)
            Front .= A_LoopField "`n"

    FileDelete, %File%
    FileAppend, % Front, %File%
}
Note that this is not tested, just the basic idea to see if I am on the right track.
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: Delete a line

08 Jan 2019, 09:53

Reloaded wrote:
08 Jan 2019, 08:23
DRocks wrote:
08 Jan 2019, 08:11
damn this is something I tried too, it was always overlapping with the next content in the file when i tried to replace a line. I'll follow other replies
Jeah.. i search like every day for a answer but found nothing yet :d. When you find something tell me xD
So far the only thing I found is to use RegExReplace.
Haystack being the FileRead output content.
Search string being the old EXACT content of the line you want to delete
this is a bit risky but it works
ahk7
Posts: 575
Joined: 06 Nov 2013, 16:35

Re: Delete a line

08 Jan 2019, 14:49

Reloaded wrote:
08 Jan 2019, 08:23
i search like every day for a answer but found nothing yet
I find that very hard to believe, try http://lmgtfy.com/?q=delete+line+autohotkey
Of course you can go directly to TF here https://www.autohotkey.com/boards/viewt ... ?f=6&t=576 :lol:
User avatar
Reloaded
Posts: 283
Joined: 25 Aug 2017, 08:48

Re: Delete a line

09 Jan 2019, 08:43

MannyKSoSo wrote:
08 Jan 2019, 08:51
do you mean something like this?

Code: Select all

DeleteLine(File, Pos) {
    FileRead, Content, %File%
    Loop, Parse, Content, `n, `r
        If (A_Index != Pos)
            Front .= A_LoopField "`n"

    FileDelete, %File%
    FileAppend, % Front, %File%
}
Note that this is not tested, just the basic idea to see if I am on the right track.
THAANKKSSS :superhappy:
How can i just show you my love? <3
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: Delete a line

09 Jan 2019, 08:46

Then where is the ending part of file after that?

If the line is in the middle and you only keep front wheres the rest? Sorry for my stupidity :D
User avatar
Reloaded
Posts: 283
Joined: 25 Aug 2017, 08:48

Re: Delete a line

09 Jan 2019, 09:26

DRocks wrote:
09 Jan 2019, 08:46
Then where is the ending part of file after that?

If the line is in the middle and you only keep front wheres the rest? Sorry for my stupidity :D
I think its just doesnt need anymore or so ._. i dont know xD
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: Delete a line

09 Jan 2019, 09:53

For that look at the If (A_Index != Pos) which is saying that everything except the line specified should be part of the front. So it just happens to be named front (since I used the same variables). So the only line that is being removed from the text is the line specified. You could also do it like this

Code: Select all

DeleteLine(File, Pos) {
    Loop, Read, %File% {
        If (A_Index != Pos) ;Here if the line matches the line you want to removed it will not be in the variable
            Data .= A_LoopField "`n"
    }
    FileDelete, %File%
    FileAppend, % Data, %File%
}
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: Delete a line

09 Jan 2019, 12:07

MannyKSoSo wrote:
09 Jan 2019, 09:53
For that look at the If (A_Index != Pos) which is saying that everything except the line specified should be part of the front. So it just happens to be named front (since I used the same variables). So the only line that is being removed from the text is the line specified.
OK!! Thanks for that clarification it totally makes sense and I did not think about that simple variable name. Thanks and cool method

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ReyAHK and 305 guests