Jump to content
Sign in to follow this  
Valuater

File write line # question

Recommended Posts

Valuater

I know this was asked a while back.. but i can't find the thread

FileReadLine will read a specific line number...

how can i write/replace that file line of text

FileWriteLine, as far as i know, does not accept a line #

thx

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites
AutoChris

You silly:

From the Help File:

FileWriteLine ( filehandle or "filename", "line")

Edit: Formatting issues

Edited by SerialKiller

Share this post


Link to post
Share on other sites
Valuater

You silly:

From the Help File:

FileWriteLine ( filehandle or "filename", "line")

Edit: Formatting issues

<{POST_SNAPBACK}>

at first i thought you had a newer version of beta..... then maybe that was a trick statement.... but no

here is what i found

FileWriteLine ( filehandle or "filename", "line" )

Parameters

filehandle The handle of a file, as returned by a previous call to FileOpen. Alternatively, you may use a string filename as the first parameter.

line The line of text to write to the text file. If the line does NOT end in @CR or @LF then a DOS linefeed (@CRLF) will be automatically added.

so this is text not an actual line number... silly

lol

8)

Edited by Valuater

NEWHeader1.png

Share this post


Link to post
Share on other sites
AutoChris

Dag-blammit! You're right. I am silly.

I just remembered using FileReadLine() in a previous script. What I had done was use FileReadLine() and then use StringReplace() to overwrite that line of text.

Sorry for being dumb. :whistle:

Edit: Dumb mistake

Edited by SerialKiller

Share this post


Link to post
Share on other sites
Valuater

Dag-blammit! You're right. I am silly.

I just remembered using FileReadLine() in a previous script. What I had done was use FileReadLine() and then use StringReplace() to overwrite that line of text.

Sorry for being dumb.  :whistle:

Edit: Dumb mistake

<{POST_SNAPBACK}>

are you saying you were able to replace the text in that line... in that text file??

or have any ideas how?

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites
AutoChris

are you saying you were able to replace the text in that line... in that text file??

or have any ideas how?

8)

<{POST_SNAPBACK}>

Yes. The script I wrote had to insert new information into a text file because the data was constantly being updated. So I knew what text I was looking for so I did a StringReplace() and changed that line of text.

If you only know certain keywords, then StringInStr() will work along with StringTrim...().

Share this post


Link to post
Share on other sites
Valuater

ok, i am trying to understand the useage

here's the process

open the file

read the line of text

use string replace

and???... how to write/replace that line of text in the file

close the file

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites
AutoChris

Open the file.

Read the line of text (sticking it into a variable)

Do a string replace (variable) and replace it with whatever text you want.

Close the file.

I actually rarely use FileReadLine() since I rarely know what line the text might be on. I typically use $var = FileRead($thetextfileIamreading, FileGetSize($thetextfileIamreading)). And then I do a StringReplace() on $var so that it is sure to find it since I stuck the whole text file into the variable.

I hope this helps. If not, good luck to you because I am about to go home and take a nap. :whistle: After being both dumb and silly in one day, I need it.

Share this post


Link to post
Share on other sites
Valuater

ok i will try again

; line #7 of text.txt = "my computer,fire fox, xpclean menu, developer"

$search = FileOpen("C:\text.txt", 0)
If $search = -1 Then
    MsgBox(0, "Error", "File not found")
    Exit
EndIf

$read = FileReadLine($search, 7)

$names = StringSplit($read, ",")

if $names[1] <> "" then $names[1] = "Your computer"

for $i = 1 to $names[0]
    MsgBox(0,"names", $names[$i])
Next    

; FileWriteLine(????????)

FileClose("C:\text.txt")

; i want line #7 to have this
; line #7 of text.txt = "your computer,fire fox, xpclean menu, developer"

can this be done.... how?????

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites
AutoChris

Okay, here is a quick script:

$file = "c:\text.txt"

$read = FileRead ($file, FileGetSize ($file))

$read = StringReplace($read, "my computer", "your computer")

FileOpen ($file, 2)

FileWrite ($file, $read)

FileClose ($file)

Hopefully this gets you on your way to whatever you are writing.

Share this post


Link to post
Share on other sites
MHz

@ Valuater

SerialKiller shows a good concept to use. Do note that AutoIt cannot replace a single line in a text file without re-writing the whole file back to disk. If it is a big file, then _FileReadToArray maybe a better choice?, as it reads and writes lines to a new file quicker then reading the whole file FileRead, replacing, then Filewriting it.

A 3rd party tool like Gsar can be used to search and replace in a text file. But that is external and adds 60+ kb to your program.

HTH

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  

×