Remove from line...

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
-Elaphe-
Posts: 75
Joined: 24 Dec 2020, 13:24

Remove from line...

Post by -Elaphe- » 09 Dec 2023, 06:40

How can I remove all lines a in text file from line 500 to the end of the list?

User avatar
mikeyww
Posts: 27314
Joined: 09 Sep 2014, 18:38

Re: Remove from line...

Post by mikeyww » 09 Dec 2023, 08:09

Code: Select all

; This script deletes lines at the end of a text file
#Requires AutoHotkey v1.1.33
filePath    := A_ScriptDir "\test.txt"
delFromLine := 500 ; Delete from this line number
truncateFile(filePath, delFromLine - 1)
Run % filePath
Sleep 300
MsgBox 64, Status, Done!

truncateFile(filePath, keepLines) {  ; Keep a specified number of lines in a text file
 Static delim := "`r`n" ; Line delimiter
 If FileExist(filePath) {
  FileRead txt, % filePath
  If !ErrorLevel {
   If pos := InStr(txt, delim,,, keepLines) {
    FileRecycle % filePath
    If ErrorLevel
     MsgBox 48, Error, % "An error occurred while deleting the file.`n`n" filePath
    Else FileAppend % SubStr(txt, 1, pos - 1 + StrLen(delim)), % filePath
   }
  } Else MsgBox 48, Error, % "An error occurred while reading the file.`n`n" filePath
 } Else MsgBox 48, Error, % "File not found.`n`n" filePath
}

ahk7
Posts: 577
Joined: 06 Nov 2013, 16:35

Re: Remove from line...

Post by ahk7 » 31 Dec 2023, 07:00

fyi for those working with text files, many functions in the TF lib https://github.com/hi5/TF

Post Reply

Return to “Ask for Help (v1)”