I have a project that I'm working on, that requires me to delete a folder that I have placed in the TEMP folder. I need to delete it on reboot, because while NSIS may think it's no longer in use, it may still be in use until then (it contains a website).
I remember a topic similar to this a long time ago, found here, but it never really got anywhere.
So I've been trying to figure out how to add an entry to the WININIT.INI file, and I have come up with this script:
As you can see, the code is quite complex (or it seems to be for such a trivial task), so the question is this: does anyone know of a more efficient way of achieving what this script does? By the way, I'm using NSIS v1.98.OutFile Pricelessware.exe
; these are purely cosmetic
Caption Pricelessware
SubCaption 2 ' '
InstProgressFlags Smooth
WindowIcon Off
AutoCloseWindow True
ShowInstDetails NeverShow
Function .onInit
; find a filename that is guaranteed not to exist
GetTempFileName $0
; since this file is automatically created by
; NSIS, delete it and recreate it as a folder
Delete $0
SetOutPath $0
FunctionEnd
Function AdjustStatusMessage
; adjust the status message (doesn't work from .onInit)
DetailPrint 'Extracting website...'
SetDetailsPrint None
FunctionEnd
Section
Call AdjustStatusMessage
; copy the website to the previously created folder
File /R Website\*.*
SectionEnd
Function .onInstSuccess
; launch the website's main page
ExecShell '' $0\INDEX.HTM
; the rest of this function's code adds an entry to
; WININIT.INI to remove the website folder on reboot
; create a file with a unique name
GetTempFileName $1
; add an entry for the website folder to WININIT.INI
; as ‘[name of unique file]=[path to WININIT.INI]’
; (required in case a NUL entry already exists - it would be overwritten
WriteINIStr $WINDIR\WinInit.ini Rename $1 $0
; open WININIT.INI and this unique file for I/O
FileOpen $2 $WINDIR\WinInit.ini R
FileOpen $3 $1 W
Copy:
; read a line of WININIT.INI
FileRead $2 $4
; if there aren't any other lines then we're done
IfErrors Done
; check if this line is the one we wrote in before
StrCmp $4 $1=$0$\r$\n ChangeIt
; if it isn't then mimic it to the unique file
FileWrite $3 $4
GoTo Copy
ChangeIt:
; if it is then change [name of unique file] to NUL
FileWrite $3 NUL=$0$\r$\n
GoTo Copy
Done:
; finish I/O processing
FileClose $2
FileClose $3
; replace the original WININIT.INI file with its proper copy
Delete $WINDIR\WinInit.ini
Rename $1 $WINDIR\WinInit.ini
FunctionEnd
Function .onInstFailed
RMDir /R $0
FunctionEnd
And before anyone asks, I've split it up into functions (.onInit, AdjustStatusMessage, .onInitSuccess) to have the status bat at 0% before it copies files, and 100% afterwards.
Thanks, guys.