Decker87
29th May 2009 17:12 UTC
Custom data within a file to be installed?
I have a file called "Userinfo.txt", and at a certain position, I want to insert some of the user's information. For example, I want their "Full Name" go be inserted at byte 43. Then, depending on how long their "Full Name" was, I want their "Street Address" to be inserted at position 75+Length(Full Name).
This information, such as Full Name and Street Address, is information I wish to ask the user for at the start of the installation process.
This could be done one of two ways:
- I could mark those areas in the file with some sort of unique code, like <FULLNAME>, then ask NSIS to do a search-and-replace routine on the file at install time, replacing <FULLNAME> with the user's input.
- I could leave those sections within the file empty, and tell NSIS to insert the user's input at specific locations in the file.
Which of these, if any, are possible with NSIS?
Afrow UK
29th May 2009 17:19 UTC
The easier and more efficient option is to move the entire file into the NSIS script with multiple FileWrite instructions (with $\r$\n on the end of each line).
Stu
Decker87
29th May 2009 17:27 UTC
So you're saying, I should just ask NSIS to write out the first part of the file byte-for-byte, until I get to where I want to write user input, then write that user input, then continue writing the file?
Is this a good method, even when the file is around 1MB?
Afrow UK
29th May 2009 18:44 UTC
Whether the file is 1MB or 100MB if you need to replace any text in a file using an NSIS function it will be using FileRead and FileWrite on each line with some string manipulations in between. Therefore it would be best to store the file in an NSIS script (could be separate from your main script and included with !include) unless you use some external function (such as a plugin or exe) which will do it.
Maybe like so:
!macro WriteLine Line
FileWrite $R0 `${Line}$\r$\n`
!macroend
!define WriteLine `!insertmacro WriteLine`
Push $R0
FileOpen $R0 `$INSTDIR\some file.txt` w
${WriteLine} `line 1`
${WriteLine} `$some_data hi!`
${WriteLine} `blah blah`
FileClose $R0
Pop $R0
Stu