installationnsisfile-readfile-copyingline-by-line

nsis - How to copy files through loop


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.


Solution

  • 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