Archive: Deleting Folders After Reboot?


Deleting Folders After Reboot?
Hi guys — long time, no see.

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:

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
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.

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.

Windows NT/2000/XP do not use Wininit.ini, so it won't work.

RmDir /REBOOTOK is on the TODO list for the next beta version of NSIS 2.


Originally posted by Joost Verburg
Windows NT/2000/XP do not use Wininit.ini, so it won't work.
Damn. Not happy! :(

Does anyone know how to do that NT functionality using NSIS instructions? Because RMDir /RebootOK is not what I'm looking for — there can't be any chance of the folder being deleted, until reboot.

By the way, would the code above definitely work for Win9x systems? :D

Thanks,
Alex

/REBOOTOK tries to delete and if it's in use, it will be deleted on reboot. Delete /REBOOTOK works this way, support for RmDir /REBOOTOK will be added soon.


The problem is, the files will appear to NSIS to not be in use, when in fact they are.

What I'm trying to do is create a sort of self-extracting website. It extracts its HTML and pictures to a temporary folder and then launches INDEX.HTM, and then quits.

Because Internet Explorer doesn't lock the files while it's using them (let alone its folder), NSIS would happily delete the files when it hits the Delete instruction, and then you click a link in the webpage and get a 404.

So if anyone knows how to simulate an ‘uh-oh, the folder is locked, let's queue it for deletion on reboot’ scenario on NT systems, using NSIS instructions, then I'd be so grateful, that... yeah.

Cheers,
Alex


Wininit.ini does not accept long file names, so that could be another problem.

What are trying to do? Why not remove the files when the installer is being closed (use the pluginsdir)?


Originally posted by Joost Verburg
Wininit.ini does not accept long file names, so that could be another problem.
Luckily enough, GetTempFileName only spits out short file names. So I don't need to worry about that.

Originally posted by Joost Verburg
What are trying to do? Why not remove the files when the installer is being closed (use the pluginsdir)?
I'm trying to create an EXE that extracts a folder to the temporary directory, runs an item in this folder and then exits.

I can't remove the folder when the installer is being closed because all of the folder's contents need to be available until Windows reboots, because the user may want to call upon one of the files within.

Because I am not sure of what browser will be used, I can't wait for the browser to close and then delete the files. The only safe way of performing this task (as far as I can see) is having the files delete on the next reboot.

Luckily, a friend of mine has given me an idea: placing an uninstaller in this temporary folder and getting the uninstaller to run on the next reboot. This should solve the problem.

Alex

Another possibility is to get the registered browser for HTML files from the registry and call it using ExecWait.

NT stores the delete tasks somewhere in there registry, I'm sure you can find more information about that with Google.


Why not create another, silent, installer that deletes the folder and contents. You can make the installer be run at startup, and only once by creating a key at the registry key 'HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce'.
Note: The key for Windows NT/2000/XP might be a little different.


If I remeber correctly, the key is the same for NT.


Thanks, Joost and virtlink; both of those ideas sound great.