Remove from line...

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

Remove from line...

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: 28871
Joined: 09 Sep 2014, 18:38

Re: Remove from line...

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: 591
Joined: 06 Nov 2013, 16:35

Re: Remove from line...

31 Dec 2023, 07:00

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

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AlFlo, mikeyww, vanove and 146 guests