Search code examples
nsis

Is there a way to write a log of which options were selected, or which files were installed?


I have a bunch of options for which files need to be installed It looks something like this:

SectionGroup /e "All" SEC0010
SectionGroup "A" SEC0020
    Section "A1"
        SectionIn 1 2
        File /r ".\Path\To\A1.txt"
    SectionEnd
    Section "A2"
        SectionIn 1 2
        File /r ".\Path\To\A2.txt"
    SectionEnd
    Section "A3"
        SectionIn 1 2
        File /r ".\Path\To\A3.txt"
    SectionEnd
SectionGroupEnd
SectionGroup "B" SEC0020
    Section "B1"
        SectionIn 1 2
        File /r ".\Path\To\B1.txt"
    SectionEnd
    Section "B2"
        SectionIn 1 2
        File /r ".\Path\To\B2.txt"
    SectionEnd
    Section "B3"
        SectionIn 1 2
        File /r ".\Path\To\B3.txt"
    SectionEnd
SectionGroupEnd
SectionGroup "C" SEC0030
    Section "C1"
        SectionIn 1 2
        File /r ".\Path\To\C1.txt"
    SectionEnd
    Section "C2"
        SectionIn 1 2
        File /r ".\Path\To\C2.txt"
    SectionEnd
    Section "AC3"
        SectionIn 1 2
        File /r ".\Path\To\C3.txt"
    SectionEnd
SectionGroupEnd
SectionGroupEnd

I would like to write a log of which files were installed and/or which options were selected.

Ideally, it would look like the following:

2023-09-18 12:00:00|All
2023-09-18 12:01:00|C2
2023-09-18 12:02:00|A1, A2, B3, C1, C2
2023-09-18 12:03:00|All

Note: All the files are being installed in the same directory.

Is there a way to know which options were selected & write them to a file?

Alternatively, is there any way using something outside of NSIS, like a .bat file, that I could track which options were selected, or which files were installed?


Solution

  • You cannot easily get the list of files but you can get the list of sections:

    If you just want the list because you want to remember the configuration for the next time the installer runs, use Memento.nsh.

    Or just loop from 0 until SectionGetFlags fails:

    !include LogicLib.nsh
    !include Sections.nsh
    
    Section
    StrCpy $1 0
    loop:
        ClearErrors
        SectionGetText $1 $2
        SectionGetFlags $1 $3
        IfErrors end
        IntOp $5 $3 & ${SF_SELECTED}
        IntOp $4 $3 & ${SF_SECGRP}
        IntOp $3 $3 & ${SF_SECGRPEND}
        ${If} $2 != ""
        ${AndIf} $5 <> 0 ; Must be checked
        ${AndIf} $4 = 0 ; Don't list groups
        ${AndIf} $3 = 0
            System::Call 'KERNEL32::GetLocalTime(p@r3)'
            System::Call '*$3(&i2.r4,&i2.r5,&i2,&i2.r6,&i2.r7,&i2.r8,&i2.r9)'
            IntFmt $5 "%.2u" $5
            IntFmt $6 "%.2u" $6
            IntFmt $7 "%.2u" $7
            IntFmt $8 "%.2u" $8
            IntFmt $9 "%.2u" $9
            DetailPrint "$4-$5-$6 $7:$8:$9|$2" ; TODO: Write to file
        ${EndIf}
        IntOp $1 $1 + 1
        Goto loop
    end:
    SectionEnd
    
    
    Section "Root"
    SectionEnd
    SectionGroup "G1"
    SectionGroup "G1.1"
    Section "S1.1"
    SectionEnd
    Section "S1.2"
    SectionEnd
    SectionGroupEnd
    Section "S1"
    SectionEnd
    Section "S2"
    SectionEnd
    SectionGroupEnd