Archive: Very Basic Question About FileRead


Very Basic Question About FileRead
  Hey All,

Very basic issue I'm running into. FileRead is not getting the contents of a file for me... or I'm not understanding why at least.

I'm trying the following:

FileOpen $MySettingsFile "DESKTOP\MyFile.txt"
FileRead $MySettingsFile $MySettings
DetailPrint $MySettings
FileClose $MySettingsFile

The above does not display the contents of my file to the Detail window. I've tried everything I could think of and followed some tutorials.

Any advice would be welcome
-mariocatch


first: is that the exact code? if so - are you missing the $ in $DESKTOP?
second: that should only read the first line in the file, not the entire file. if you want to read the entire file, you have to use FileRead/DetailPrint until an error occurs.. then close the file.


Hi Animaether,

Thanks for the reply.

Yes you're right, $Desktop\FileName is what it should be.

Also, I'm am a .NET Developer, so the concept of loops/etc are trivial, however the syntax required to do so in NSIS is not clicking with me. Mind showing me a quick example of a loop that reads in a file and prints it out?

The end goal I'm trying to do here is to reflect a settings file
ie:
Setting1=SomeValue
Setting2=SomeValue
Setting3=SomeValue

And display that in the Installer. Then When they leave the page, I want to save those settings back out to the file. I'll probably just end up displaying the contents to the installer page, then, writing over the file afterwards with the saved settings.


Not sure what exactly you have in mind, but as for looping - LogicLib is your friend.

Something like this:


"LogicLib.nsh"


>outfile "$%temp%\test.exe"

>var MySettingsFile
>var MySettings

ShowInstDetails show

Section
ClearErrors
FileOpen $MySettingsFile "$WINDIR\win.ini" "r"
${If} ${Errors}
DetailPrint "Could not open file '$WINDIR\win.ini'"
${Else}
${Do}
FileRead $MySettingsFile $MySettings
${If} ${Errors}
${
ExitDo}
${Else}
DetailPrint "$MySettings"
${EndIf}
${Loop}
${EndIf}
>SectionEnd
>
Note that LogicLib can have conditionals on the loop as well - so it could be made to loop until an error occurs. However, as part of the loop is printing the message, I've decided to inline that logic instead - so that no DetailPrint occurs if the FileRead produces an error.