Archive: How to detect a file is locked when deleting a file


How to detect a file is locked when deleting a file
Hi All,

When I try to delete a file during the uninstallation, just like some .dll, .exe files. If they are still in use or rights issues, I couldn't delete them.
When trying to overwrite the files, i couldn't overwrite them?
how can I detect them? and how to solve?
Any thoughts?


James


Use the Library macros.

http://nsis.sourceforge.net/Docs/AppendixB.html


coulde you give me an example????
Thank you.


That page contains examples.


IfFileExists "$INSTDIR\MyApp.exe" 0 new_installation ;Replace MyApp.exe with your application filename
StrCpy $ALREADY_INSTALLED 1
new_installation:

!insertmacro InstallLib REGDLL $ALREADY_INSTALLED REBOOT_NOTPROTECTED dllname.dll $SYSDIR\dllname.dll $SYSDIR
hmmmm
if i have 20 files, i should write them like above 20 times?


If your DLL files are not shared, there is no need for anything but the InstallLib macro. Instead of $ALREADY_INSTALLED, use NOTSHARED. If they are shared, do the test just once, keep the value in $ALREADY_INSTALLED and just add more InstallLib lines.


If a file is locked, how can i get the error message?
If the istaller has error, how can I display error message, I mean NSIS error message..
Iferrors 0
MessageBox MB_OK error_message(NSIS message,not my own message)


-------------------
on the link you gave to me, it says it can:
***
Windows File Protection checks
***
How to checks, if file protected, how can show message?


If a file is locked the error flag will be set and, if you've enabled reboot, the reboot flag will also be set. There's no message, just a state of failure to delete or replace the file.


Files installed by your application are most probably not protected. Only system files are protected by WFP.


when I run in windows mode (not silent mode, it will show the file is in use. However, run in silent mode . it won't show the error message.....
I am expecting the error message in the silent mode.


What error message? The Library macros show no error message. if you're talking about the normal error message for when a file can't be overwritten, then on silent mode it takes the default action of skipping the file or aborting if AllowSkipFiles is set to off.


Yeah. You are right.
The error message is like " Error opening file for writing:
c:\myapp.dll ....."
Can I get this error message in silent mode. If I can't, can I set an error flag for this copying file error?

Thank you for your help.


Or before getting this error message, can I pop up an message like "myApp.dll is in use, can't copy this file!", then I can exit the installer.


The error flag is already set when this message appears. You can try deleting the file and checking the error flag to avoid this message on non-silent mode.


How can set the error flag? which error flag?

Thank you....


clearerrors
delete "file.bla"
iferrors error_routine
;no errors

;error
error_routine:


Regarding to the uninstaller, if you're using MUI, you may add the MUI_UNPAGE_FINISH and for every suspected delete instruction, add the switch /REBOOTOK.
If file(s) failed to delete, the finish page should appear with reboot checkboxes.
Without MUI_UNPAGE_FINISH you may check in function un.onUninstSuccess the reboot flag (IfRebootFlag), and if is set pop up a message about.


From experience - reboots are not necessary in most cases.
it only need some brain and an idea to replace/rename
files. the reboot only do the cleanup.
- push the filename
- call replacing routine:

-- delete file -> on success return
-- unregister file
-- delete file -> on success return
-- rename file
-- delete renamed file -> on success return
-- delete renamed file on reboot
-- return

- copy new file
- reset reboot flag (we dont really need it)

normally user dont work on windows system files so that a reboot is a must


Thank you all for help. I will test it up. Hopefully, i can fix the bug we have.


Wrote: "Can I get this error message in silent mode."??

"silent" = no UI interaction nothing displayed only error(result codes)

"un-attended"= Display of progress but no UI interaction

"regular"= UI interaction and Display of progress