How to delete first 10 lines of a .txt file?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

How to delete first 10 lines of a .txt file?

16 Mar 2017, 12:25

Friends I want that when I press f1 button then it should delete first 10 lines of a specific .txt file and it should keep the save changes after deleting first 10 lines of that .txt file. For this I tried to use so many codes but nothing seems to work. you can see in this image what i want-
Image

As I tried-

Code: Select all

f1::
Loop, read, E:\data collection\all reports\data1.txt
{
    Loop, parse, A_LoopReadLine, %A_Tab%
    {
      	msgbox % a_loopfield
    }
}
return
These codes are showing the content of each line of data1.txt in one by one msgbox. But i do not know how can i delete the first ten line of data1.txt
I also tried to use tf functions library from here- https://github.com/hi5/TF
but when i download tf.ahk and included it in my script and tried to run it then it showed this error-
Image

I do not know what to do to solve this issue.
please help me.....
thanks a lot....
I don't normally code as I don't code normally.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: How to delete first 10 lines of a .txt file?

16 Mar 2017, 12:32

You just need two functions:
InStr() - Use this to find the 10th `n. (Use the "Occurrence" parameter to specify the 10th one.)
SubStr() - Use this to get everything after the 10th `n.

Instead of Loop, Read, use FileRead.
4GForce
Posts: 553
Joined: 25 Jan 2017, 03:18
Contact:

Re: How to delete first 10 lines of a .txt file?

16 Mar 2017, 12:52

Like kon said, you need to read the whole file, delete the lines and overwrite the file with the remaining content. FileAppend
ahk7
Posts: 572
Joined: 06 Nov 2013, 16:35

Re: How to delete first 10 lines of a .txt file?

16 Mar 2017, 14:02

Sounds like you didn't download TF.ahk properly, either use the ZIP option or save the RAW version https://raw.githubusercontent.com/hi5/TF/master/tf.ahk

Could also be there is a syntax error elsewhere in your script, TF_Countlines() is of course not a hotkey function.

Removing lines using TF is as easy as 1-2-3 as shown in the example TF_RemoveLines("File.txt", 1, 10) will remove the first 10 lines from a file and copy the result to File_copy.txt, if you want result to be file.txt add a ! in front of it TF_RemoveLines("!File.txt", 1, 10)
just me
Posts: 9406
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How to delete first 10 lines of a .txt file?

17 Mar 2017, 02:49

What do you want to happen if the file contains less than 10 lines?
User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

Re: How to delete first 10 lines of a .txt file?

17 Mar 2017, 13:25

just me wrote:What do you want to happen if the file contains less than 10 lines?
Dear just me, i have a so many system generated .txt files which contain excessive data. Those .txt file's starting lines are vain and i can not import those .txt files into excel for further analysis of data with those starting lines. Moreover those .txt files have useless lines in the middle and end part also. so i want to delete all that useless part from those .txt files so that i can easily import them into excel for better management of data and analysis.........
I don't normally code as I don't code normally.
Guest

Re: How to delete first 10 lines of a .txt file?

17 Mar 2017, 15:26

Sabestian Caine wrote:
just me wrote:What do you want to happen if the file contains less than 10 lines?
Dear just me, i have a so many system generated .txt files which contain excessive data. Those .txt file's starting lines are vain and i can not import those .txt files into excel for further analysis of data with those starting lines. Moreover those .txt files have useless lines in the middle and end part also. so i want to delete all that useless part from those .txt files so that i can easily import them into excel for better management of data and analysis.........
You didn't answer the question.
just me
Posts: 9406
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How to delete first 10 lines of a .txt file?

17 Mar 2017, 16:34

I think he did. ;)
Guest

Re: How to delete first 10 lines of a .txt file?

17 Mar 2017, 17:19

OK maybe I'm just dense. What does he say should happen if there are less than 10 lines?
Is the answer: "There will never be less than 10 lines, so don't worry about it."; because he didn't explicitly say that :?
User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

Re: How to delete first 10 lines of a .txt file?

17 Mar 2017, 23:25

Guest wrote:OK maybe I'm just dense. What does he say should happen if there are less than 10 lines?
Is the answer: "There will never be less than 10 lines, so don't worry about it."; because he didn't explicitly say that :?
Dear Guest, the no of useless lines is fixed in each .txt file. there are 740 lines are useless in every .txt file. i wrote only 10 lines in the example because 740 is a big amount of lines. So i think if i get the codes to delete first ten lines then i can modify those codes to delete 740 lines also. Thanks......
I don't normally code as I don't code normally.
ahk7
Posts: 572
Joined: 06 Nov 2013, 16:35

Re: How to delete first 10 lines of a .txt file?

18 Mar 2017, 04:14

@Sabestian Caine just fyi you can remove various sections of lines in one go using TF see the "range" https://github.com/hi5/TF#startline-endline-syntax

Example TF_RemoveLines("File.txt", "1-10,"90-100") will remove lines 1 to 10 and 90 to 100 from the file in one go, so there is no need to call TF twice for that. If you want to search for specific lines to remove you can use the result of TF_Find.
just me
Posts: 9406
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How to delete first 10 lines of a .txt file?

18 Mar 2017, 04:24

You might be interested in the File Object:

Code: Select all

#NoEnv
FilePath := "Test.txt"
Contents := ""
Loop, 20
   Contents .= "Line " . A_Index . "`r`n"
FO := FileOpen(FilePath, "w")
FO.Write(RTrim(Contents, "`r`n"))
FO.Close()
DeleteLeadingLines(FilePath, 10)
FO := FileOpen(FilePath, "r")
Contents := FO.Read()
FO.Close()
MsgBox, 0, Current Contents, %Contents%
FileDelete, %FilePath%
ExitApp

DeleteLeadingLines(FilePath, NumOfLines) {
   If (FO := FileOpen(FilePath, "rw")) {
      StartPos := FO.Pos
      Contents := FO.Read()
      If (FoundPos := InStr(Contents, "`n", , , NumOfLines)) {
         FO.Pos := StartPos
         FO.Write(Sub Str(Contents, FoundPos + 1)) ; <<<<<< S u b S t r  is currently blocked by the forum, please correct the function name
         FO.Length := FO.Pos
         FO.CLose()
         Return True
      }
      FO.Close()
   }
   Return False
}
User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

Re: How to delete first 10 lines of a .txt file?

19 Mar 2017, 01:34

just me wrote:You might be interested in the File Object:

Code: Select all

#NoEnv
FilePath := "Test.txt"
Contents := ""
Loop, 20
   Contents .= "Line " . A_Index . "`r`n"
FO := FileOpen(FilePath, "w")
FO.Write(RTrim(Contents, "`r`n"))
FO.Close()
DeleteLeadingLines(FilePath, 10)
FO := FileOpen(FilePath, "r")
Contents := FO.Read()
FO.Close()
MsgBox, 0, Current Contents, %Contents%
FileDelete, %FilePath%
ExitApp

DeleteLeadingLines(FilePath, NumOfLines) {
   If (FO := FileOpen(FilePath, "rw")) {
      StartPos := FO.Pos
      Contents := FO.Read()
      If (FoundPos := InStr(Contents, "`n", , , NumOfLines)) {
         FO.Pos := StartPos
         FO.Write(Sub Str(Contents, FoundPos + 1)) ; <<<<<< S u b S t r  is currently blocked by the forum, please correct the function name
         FO.Length := FO.Pos
         FO.CLose()
         Return True
      }
      FO.Close()
   }
   Return False
}
Thanks a lot dear just me.....
you are awesome.............
thanks a lot once again.....
I don't normally code as I don't code normally.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: morkovka18, sanmaodo and 222 guests