insert line from/in text file at line number

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
way2000
Posts: 58
Joined: 15 Jul 2016, 08:57

insert line from/in text file at line number

17 Oct 2017, 17:09

i have this script that inserts lines in text files from a text file full of text lines

currently it's set so it inserts a line in a file that has the text string [content] in it from another source text file full of text lines

i need to make it so it inserts lines but not under [content] but to a specific line number, eg: set a line number so the text lines are inserted there instead of a random location under [content]

Code: Select all

#NoEnv
SetBatchLines, -1

SourcePath := "C:\Users\username\Desktop\source.txt"
TargetPath := "C:\Users\username\Desktop\New folder" ; NO CLOSING \
TargetExt := "txt" ; File type


FileRead, SourceData, %SourcePath%
SourceLines := StrSplit(SourceData, "`r`n")
MaxLines := SourceLines.Length()

Loop Files, %TargetPath%\*.%TargetExt%
{
   If (A_Index > MaxLines)
      ErrorExit("More files than source lines (" . MaxLines . ")!")
   If !(FileObj := FileOpen(A_LoopFileLongPath, "rw"))
      ErrorExit("Could not open the target file " . A_LoopFileLongPath . "!")
   FileContent := FileObj.Read()
   If (Pos := RegExMatch(FileContent, "Pim)^\[content\]$", Len)) {
      FileObj.Pos := Pos + Len - 1
      FileObj.Write("`r`n" . SourceLines[A_Index])
      FileObj.Write(SubStr(FileContent, Pos + Len))
   }
   FileObj.Close()
   If (Pos = 0)
      ErrorExit(A_LoopFileLongPath . "`r`ncould not find the [content] line!")
}


ErrorExit(Msg) {
   MsgBox, 16, ERROR!, %Msg%
   ExitApp
}
way2000
Posts: 58
Joined: 15 Jul 2016, 08:57

Re: insert line from/in text file at line number

17 Oct 2017, 19:26

any help please

if possible, help to add this feature to the script
Guest

Re: insert line from/in text file at line number

19 Oct 2017, 04:11

Not sure what the problem is but simply loop through the lines and insert the new text where you need it, you have the A_Index in a loop so you know the line number. There is also https://github.com/hi5/TF which has many functions including TF_InsertLine() which you can have a look at (to see how its done or just use it as is).
way2000
Posts: 58
Joined: 15 Jul 2016, 08:57

Re: insert line from/in text file at line number

19 Oct 2017, 13:33

is it possible for anyone to help out with this

i don't program personally, i keep using some ahk scripts for a project but i cannot program them myself

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: wilkster and 332 guests