Jump to content

replace a line in txt file?


Recommended Posts

; read in the file
$list = FileRead ($filename, FileGetSize ($filename))
If @error = 1 Then
    MsgBox (0, "", "Error reading from " & $filename)
    Exit 1

; split into lines
$list = StringSplit ($list, @CRLF, 1)

; replace specific line
$linenum = 50
$list[$linenum] = "This is the text on the replaced line."

; overwrite the file
$handle = FileOpen($filename, 1)

If $handle = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit 1

For $i = 1 To $list[0]
    FileWriteLine ($handle, $list[$i])

How's that? Should be fairly self-explanatory I think...

Link to comment
Share on other sites


I am writing a program where I want to replace a specific line in a txt file.

I know the line number that i want to replace but I dont know how to get the program to replace it with my new text line.

Appreciate any suggestions...



If you know the content of the line you could use _ReplaceStringInFile().



__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...