- NSIS Discussion
- @Afrow Write to text file line number
Archive: @Afrow Write to text file line number
TonyDS
3rd December 2003 02:24 UTC
@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
kichik
3rd December 2003 11:31 UTC
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.
TonyDS
3rd December 2003 23:01 UTC
Oh well thanks
Afrow UK
5th December 2003 16:43 UTC
Sorry, I wrote the script while lacking a bit of skills.
It now works.
-Stu
TonyDS
5th December 2003 18:55 UTC
Thanks Afrow I'll try it :)
TonyDS
5th December 2003 21:03 UTC
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
Afrow UK
5th December 2003 23:29 UTC
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
lewellyn
5th December 2003 23:43 UTC
Of course, you could always write from the bottom up and not worry about the numbers changing :)
Afrow UK
5th December 2003 23:48 UTC
Originally posted by lewellyn
Of course, you could always write from the bottom up and not worry about the numbers changing :)
Good idea!
-Stu :)
TonyDS
6th December 2003 00:46 UTC
True I could do it bottom up, but for this specific file it need to be top down
Thanks again Afrow :)