Remove line from text file
Remove line from text file
Hi, How can I remove the line that contains "apple" from the text file? Please help me
Re: Remove line from text file
Code: Select all
FileRead ; get content from file
StrReplace() ; replace whatever from content with nothing
FileAppend ; create new file with the edited content
Re: Remove line from text file
You would have to check each line and not include lines that contain "apple" in the output that replaces the original file.
Btw, StrReplace() would be good for removing only the word "apple" itself, but not the whole line that contains "apple". You could do something like that with RegExReplace().
Code: Select all
FileRead, Text, mytextfile.txt
loop, Parse, Text, `n, `r
if !InStr(A_LoopField, "apple")
OutText .= A_LoopField "`n"
OutText := SubStr(OutText, 1, -1) ; remove last `n
FileMove, mytextfile.txt, mytextfile.bak, 1 ; create backup file
FileDelete, mytextfile.txt ; delete original file (or else it would add on to old contents)
FileAppend, % OutText, mytextfile.txt ; write new file
Btw, StrReplace() would be good for removing only the word "apple" itself, but not the whole line that contains "apple". You could do something like that with RegExReplace().
Last edited by boiler on 26 Jan 2022, 10:41, edited 1 time in total.
Re: Remove line from text file
Oops, correct!Btw, StrReplace() would be good for removing only the word "apple" itself