Sign in to follow this  
Followers 0
87450kalle

replace a line in txt file?

4 posts in this topic

Hi,

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...

/Johan

Share this post


Link to post
Share on other sites



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

; 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
EndIf

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

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

Share this post


Link to post
Share on other sites

Hi,

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...

/Johan

<{POST_SNAPBACK}>

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

Cheers

Kurt


__________________________________________________________(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 *

Share this post


Link to post
Share on other sites

Fast Reply!!

Thanks!!

/Johan

Share this post


Link to post
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
Sign in to follow this  
Followers 0