Archive: Using ${Locate} with SetFileAttributes


Using ${Locate} with SetFileAttributes
I’m fairly new to NSIS, but I’ve managed to get it to do almost everything I want for this little installation script I wrote:


; Holocron.nsi
; This script installs the Holocron of Star Wars Galaxies /notepad help files.
;--------------------------------
; Includes
!include "FileFunc.nsh"
!insertmacro Locate

Name "The SWG Holocron" ; The name of the installer
OutFile "Holocron.exe" ; The file to write
InstallDir $INSTDIR ; The default installation directory
InstProgressFlags smooth colored
RequestExecutionLevel user ; Request application privileges for Windows Vista
;--------------------------------
; Pages
Page directory
Page instfiles
;--------------------------------
; Installation tasks
Section ""

; Set output path to the installation directory.
SetOutPath $INSTDIR

; Put files there
File /r Holo\*.*

; Set these files to read-only so that
; /notepad can't corrupt them or so they
; don't get changed accidentally.
${Locate} "$INSTDIR\*.*" "/L=F" "SetReadOnly"

SectionEnd

Function .onInit
ReadRegStr $0 HKEY_LOCAL_MACHINE \
"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\StarwarsGalaxies" \
"Path"
StrCpy $INSTDIR $0\profiles\Holocron
FunctionEnd

Function SetReadOnly
SetFileAttributes $R9 READONLY
FunctionEnd

Everything works—or at least, no error is generated—but the attribute flags on the installed files in any of the folders are not set to read-only. What am I missing?

Remove the mask from path. The mask is set with /M option and "*.*" mask is the default. And you should add "Push $0" in the SetReadOnly function or it wont loop. :)

PaR


I bow to your superior wisdom. :) Going back to the ${locate} description and examples in the NSIS documentation, I see that these things were indeed true, and those two small changes fixed the code. Thank you.

For the benefit of others who may come after me, this is the corrected code:


; Holocron.nsi
; This script installs the Holocron of Star Wars Galaxies /notepad help files.
;--------------------------------
; Includes
!include "FileFunc.nsh"
!insertmacro Locate

Name "The SWG Holocron" ; The name of the installer
OutFile "Holocron.exe" ; The file to write
InstallDir $INSTDIR ; The default installation directory
InstProgressFlags smooth colored
RequestExecutionLevel user ; Request application privileges for Windows Vista
;--------------------------------
; Pages
Page directory
Page instfiles
;--------------------------------
; Installation tasks
Section ""
SetOutPath $INSTDIR
File /r Holo\*.*

; Set these files to read-only so that
; /notepad can't corrupt them or so they
; don't get changed accidentally.
${Locate} "$INSTDIR" "/L=F" "SetReadOnly"

SectionEnd ; end the section

Function .onInit
ReadRegStr $0 /
HKEY_LOCAL_MACHINE /
"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\StarwarsGalaxies" /
"Path"
StrCpy $INSTDIR $0\profiles\Holocron
FunctionEnd

Function SetReadOnly
SetFileAttributes $R9 READONLY
Push $0 ; Required to make the code loop
FunctionEnd


I guess that'll teach me to code on too little sleep. :igor: