0

I saved the files to be copied to a .dat file as below.

\Bin\a.exe
\Bin\b.dll
\Bin\c.dll
\Bin\d.dll
\Bin\e.dll
\Bin\f.dll

Then I want to read the .dat file line by line. I read it by line as below code(.nsi).

  ClearErrors
  FileOpen $0 "CopyFileList.dat" r                    

  loop:

  FileRead $0 $1                          
  Strcmp $1 "" done
  StrCpy $varBinFileName $1
  File /r "${TARGETDIR}\$varBinFileName"             
  goto loop

  done:

  FileClose $0         

Then two problems arise!!

1) Read the name of the variable itself, not the stored value of the variable $varBinFileName.
So, even though the file is there, The file can not be found because it is not a file name.

2) When a .dat file is read, a "||" is added.
For example, reading the first line produces the following results: \Bin\a.exe||
I want to get the result of removing "||" to use it when copying files.


Please let me know if there is a better way to read the .dat file line by line and copy it over the loop instead of the code I wrote.

areum
  • 93
  • 3
  • 14

1 Answers1

1

1)

The File instruction does not work like that, it does not accept variables because the filename refers to a file on the machine you are compiling on. The file is compressed and stored inside the setup .exe. Use the CopyFiles instruction to copy files that are already on the end users system.

To conditionally extract something you need to write something like this:

!include LogicLib.nsh
Section
SetOutPath $InstDir
${If} $myvar == "something.ext"
  File "something.ext"
${Else}
  File "somethingelse.ext"
${EndIf}
SectionEnd

2)

FileRead includes the newline characters (if any) and you must remove them:

; Write example file
FileOpen $0 "$temp\nsistest.txt" w
FileWrite $0 "hello$\r$\n"
FileWrite $0 "world$\r$\n"
FileClose $0

; Parse file
FileOpen $0 "$temp\nsistest.txt" r
loop:
    FileRead $0 $1
    StrCmp $1 "" done
        StrCpy $2 $1 "" -1 ; Copy last character
        StrCmp $2 '$\r' +2
        StrCmp $2 '$\n' +1 +3
        StrCpy $1 $1 -1 ; Remove newline
        Goto -4 ; ...and check the end again
    DetailPrint line=$1
    Goto loop
done:
FileClose $0
Anders
  • 97,548
  • 12
  • 110
  • 164
  • Is that the only way to enter the binaries to be installed via hardcoding? – areum Apr 24 '18 at 00:35
  • You can use `!system` to execute a batch file that generates a text file with the NSIS instructions that you can then `!include`. See https://stackoverflow.com/a/49866848/3501 etc. – Anders Apr 24 '18 at 00:51
  • I created a batch file with reference to that link. I want to run a batch file from within the install section, but I get an error when I put that code in section. ( Error: command Section not valid in Section ) Is it code that can not be executed within sections or functions? – areum Apr 24 '18 at 02:24
  • You can't put a section in a section. Do not use the exact file I linked to, just a file that writes `echo File ...`. – Anders Apr 24 '18 at 06:58