Archive: @Afrow Write to text file line number


@Afrow Write to text file line number
Hi Afrow or anyone, I'm trying to use your script to write to a text file, but I'm having a problem, I'm trying to place a bit of text onto the first line of the text file, I'm only presuming that the other text in the file will be move down by on row, but when I try it, it deleted the whole text file and only places the "hello" on the second line down.


Could you or anyone else tell me where I'm going wrong please



Function .onInit
Push hello
Push 1
Push "$INSTDIR\file.txt"
Call WriteToFileLine


Function WriteToFileLine
Exch $0 ;file
Exch
Exch $1 ;line number
Exch 2
Exch $2 ;string to write
Exch 2
Push $3
Push $4

FileOpen $4 $0 w
StrCpy $3 0

loop:
IntOp $3 $3 + 1
ClearErrors
FileRead $4 $0
IfErrors +2
StrCmp $3 $1 0 loop
FileWrite $4 "$\r$\n$2$\r$\n"
FileClose $4

Pop $4
Pop $3
Pop $2
Pop $1
Pop $0
FunctionEnd

You can't just stick text in the middle of the file, it won't push the following text further. You must open another file, write what you want into it, delete the original file and then copy over the new file. There are examples in the Archive that do that, you can use them as a reference.


Oh well thanks


Sorry, I wrote the script while lacking a bit of skills.
It now works.

-Stu


Thanks Afrow I'll try it :)


Yep it does work, now, I looked thorugh the archive but I wasn't sure which script was right, the thing is, how would I go about writing to more than one line
Say I wanted for example

Hello
Hi again
Bye Bye
Im outta here
Gone

would it work if I placed $/r$/n and so on (if thats right I've forgotten this is correct or not)

or say I wanted to write different lines to lines 1 5 10 7 22 and so on is this possible?

Thanks


Originally posted by TonyDS
Yep it does work, now, I looked thorugh the archive but I wasn't sure which script was right, the thing is, how would I go about writing to more than one line
Say I wanted for example

Hello
Hi again
Bye Bye
Im outta here
Gone

would it work if I placed $/r$/n and so on (if thats right I've forgotten this is correct or not)

or say I wanted to write different lines to lines 1 5 10 7 22 and so on is this possible?

Thanks
Yes all of this is possible.
Write multiple lines to a single line number using $\r$\n, and as for writing to multiple line numbers, you will have to call the function multiple times.
Don't forget that once a line has been entered, the preceeding lines will be moved down. Therefore, what used to be line 14 will now be line 15. You will have to take this into account while writing to different lines one after another.

-Stu

Of course, you could always write from the bottom up and not worry about the numbers changing :)


Originally posted by lewellyn
Of course, you could always write from the bottom up and not worry about the numbers changing :)
Good idea!

-Stu :)

True I could do it bottom up, but for this specific file it need to be top down

Thanks again Afrow :)