Archive: Delete Files and Directory not working


Delete Files and Directory not working
I'm having problems getting files and directory deleted. My Uninstall routine is:

_________________________________

Section Uninstall
;Delete Files
Delete "$INSTDIR\*.*"

;Delete Uninstaller And Unistall Registry Entries
Delete "$INSTDIR\Uninst.exe"
DeleteRegKey HKEY_LOCAL_MACHINE "SOFTWARE\MyApp"
DeleteRegKey HKEY_LOCAL_MACHINE "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\MyApp"
RMDir /r "$INSTDIR"

__________________________________

After the Uninstall method is run, the $INSTDIR including all subdirectories and files is still there. Any help?

Thanks,
Eric


* Is the uninstaller located in the directory you want to delete?

* Is the directory not in use? Can you delete it manually?


Uninstaller should no longer be there by the point in the code where it should be deleting the directory. I have no problems deleting the directory manually.


Try this :



Section Uninstall

Delete "$INSTDIR\*.*"
Delete "$INSTDIR\Uninst.exe"
DeleteRegKey HKLM "SOFTWARE\MyApp"
DeleteRegKey HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\MyApp"
RMDir /r "$INSTDIR"


I'm confused by your reply Vegetasan. I'm not having problems with the registry. The problem I'm having is with:

RMDir /r "$INSTDIR"

The installer recognizes when the app is already installed... Can you give me more info on this change if I'm not understanding?

Thanks,
Eric


No I just set it between the script like you

Your script


Section Uninstall
;Delete Files
Delete "$INSTDIR\*.*"

;Delete Uninstaller And Unistall Registry Entries
Delete "$INSTDIR\Uninst.exe"
DeleteRegKey HKEY_LOCAL_MACHINE "SOFTWARE\MyApp"
DeleteRegKey HKEY_LOCAL_MACHINE "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\MyApp"
RMDir /r "$INSTDIR"



So I did it like you. But without the registry:


Delete "$INSTDIR\*.*"
Delete "$INSTDIR\Uninst.exe"
RMDir /r "$INSTDIR"


Just try this one and say if it works

That will make no difference. Are you sure $INSTDIR is correct?


Joost,

Here's the entire script. Looks to me like it would be right, but I'm not positive. See a problem?

______________________________________

;NSIS Script

;Backgound Colors
BGGradient 800080 000000 FFFFFF
BrandingText " "

;Title Of Your Application
Name "MyApp"

;Do A CRC Check
CRCCheck On

;Included file(s)
!include servicelib.nsh

;Output File Name
OutFile "InstallMyApp.exe"

;The Default Installation Directory
InstallDir "D:\Program Files\MyApp"

;The text to prompt the user to enter a directory
DirText "Please select the folder below"

Section "Install"
;Install Files
SetOutPath $INSTDIR
SetCompress Auto
SetOverwrite IfNewer
File /r "D:\projects\MyApp\*.*"

; Write the uninstall keys for Windows
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\MyApp" "DisplayName" "MyApp (remove only)"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\MyApp" "UninstallString" "$INSTDIR\Uninst.exe"
WriteUninstaller "Uninst.exe"
SectionEnd

Section "Shortcuts"
;Add Shortcuts
SectionEnd

UninstallText "This will uninstall MyApp from your system"

Section Uninstall
;Delete Files
Delete "$INSTDIR\*.*"

;Delete Uninstaller And Unistall Registry Entries
Delete "$INSTDIR\Uninst.exe"
DeleteRegKey HKEY_LOCAL_MACHINE "SOFTWARE\MyApp"
DeleteRegKey HKEY_LOCAL_MACHINE "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\MyApp"
RMDir /r "$INSTDIR"
SectionEnd

;--------------------------------
;Installer Functions

Function .onInit

ReadRegStr $R0 HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\MyApp" "UninstallString"
StrCmp $R0 "" done

MessageBox MB_OKCANCEL|MB_ICONEXCLAMATION \
"MyApp is already installed. $\n$\nClick `OK` to remove the \
previous version or `Cancel` to cancel this upgrade." \
IDOK uninst
Abort

;Run the uninstaller
uninst:
ClearErrors
ExecWait '"$R0" _?=$INSTDIR' ;Do not copy the uninstaller to a temp file


done:

FunctionEnd


Are you running the uninstaller manually and in the right directory?

Is there a possible problem with user rights?


No, I don't run it manually. The executable asks me if I want to uninstall and I say yes. Are you saying that the "Uninstall" section won't get run? Sorry, it's been quite a while since I created these scripts.

I'm administrator on all the machines that I'm installing on.


So what happends when you start the uninstaller manually? Are the installation directories of the installer and uninstaller equal?


Hmmmmmmm. If I run the uninstaller manually, it works fine. The log shows it deleting the folder and it does. However, when I run the installer and do uninstall first, it doesn't even show the line being executed (as if it doesn't exist). Obviously something is wrong in my code. Any clue?


So when I run the installer manually, I see output from the entire thing running. When I run the uninstaller from the installer, it does everything except for the rmdir. In other words:

Delete "$INSTDIR\*.*"

does happen and the app gets unregistered. I'm REALLY lost!


What is the value of $INSTDIR in the uninstaller? You are setting it using the command line, make sure that the installer $INSTDIR is the folder to uninstall from.


$INSTDIR has to be correct because the lines:

Delete "$INSTDIR\*.*"
and
Delete "$INSTDIR\Uninst.exe"

are deleting from the correct place; the directory that I want to delete with:

RMDir /r "$INSTDIR"

Which doesn't even get attempted based on the log.


I hard coded the path to delete:

RMDir /r "D:\MyApp"

and got the same results. The uninstaller doesn't even attempt to perform the command. Do I need to put the delete of the uninstaller after my RMDir?


And does it make any difference whether you start it manually or not?


It always works when I start it manually which makes no sense to me.


Bump


You can submit a bug report at the project page. Also attach a script that reproduces the bug and nothing else (no registry entries, files etc.).


When you start it from the installer directory itself, it can't delete itself. RMDir /R stops if it can't delete one of the files and therefore you are left with more than just the uninstaller. Simply copy the uninstaller somewhere else just like it does on its own when executed without the _?= switch.


So if I take out the _?= switch, I should be fine?


Yes, but the installer won't wait for the uninstaller to finish.


I'm not sure what you mean by copy it somewhere else. Do you mean to have it run from a different place in the first place or move it before doing the RMDIR? How about if I use the Rename command just before the RMDIR? Sorry, I'm very green with this...


Copy the uninsatller somewhere else before you execute it. For example, the temporary directory ($TEMP).


OK, I tried this. I must be doing something wrong. Here's what I changed:

---------------------------------
Before
---------------------------------

;Run the uninstaller
uninst:
ClearErrors
ExecWait '"$R0" _?=$INSTDIR' ;Do not copy the uninstaller to a temp file

---------------------------------
After
---------------------------------

ClearErrors
CopyFiles /SILENT /FILESONLY "$INSTDIR\Uninst.exe" "$TEMP\Uninst.exe"
ExecWait '"$TEMP\Uninst.exe" _?=$INSTDIR'


---------------------------------

Am I missing something else? I apologize if I'm being dense!


Please excuse the double post, I used Quick Reply and it doesn't seem to update the "Last Post" information. Please see my previous post. Thanks - Eric


Are you sure $R0 is "$INSTDIR\Uninst.exe"? Other than that, it looks ok.


Part of my change was to quit using $R0. I'm running the Uninst.exe from where I copy it to now:

CopyFiles /SILENT /FILESONLY "$INSTDIR\Uninst.exe" "$TEMP\Uninst.exe"
ExecWait '"$TEMP\Uninst.exe" _?=$INSTDIR'
so that the uninstaller doesn't keep a hold on that directory keeping me from deleting it. Seems like it should work to me, but doesn't execute the RMDIR command still.

So the uninstaller is executed but nothing is deleted? Can you attach a minimal complete example so I can test this on my own computer and see what's wrong?


This is so frustrating. I ripped out the substance and it works fine. I didn't change my Uninstall functionality at all. I'm taking a break from this. Thanks for trying!


Well, one difference I found when compiling with substance vs. without substance. When I build the one that doesn't work, I get the warnings:

2 warnings:
uninstall function "un.Service" not referenced - zeroing code (0-278) out

install function "Service" not referenced - zeroing code (0-278) out


Could this have something to do with it?


No, that only means those functions were not used and were therefore deleted from the output.

Maybe if you can attach both the working script and the script that doesn't work I can spot the difference.


Did anything every happen with this? I'm also trying to get an unistaller to run prior to installing new (updated)files and it doesn't seem to be working. I was just curious if there was something very specific in Stonkers script, or a more general error that might apply to my situation as well.

thanks,

-marc


As there was no other response from stonkers, the issue was never resolved. Looking at it again, it seems $INSTDIR was simply not initialized with the correct value in .onInit in his last attempt (no InstallDirRegKey in the big script above). I think his issue is pretty specific to his script. If you have a problem with calling the uninstaller, open up another thread with more details.