NSIS - Delete all files except one file
Asked Answered
W

2

6

Could any one clarify me that, when uninstalling I need to delete everything form the Installation folder except the License file. How can I do it with NSIS scripting?

Thanks Regards, RoboAlex.

Whig answered 1/12, 2010 at 6:33 Comment(0)
F
6

Instead of opening the file, as in Anders' third point, I'd do it this way:

Rename $INSTDIR\license.txt $PLUGINSDIR\license.txt
RMDir /R $INSTDIR # Remembering, of course, that you should do this with care
CreateDirectory $INSTDIR
Rename $PLUGINSDIR\license.txt $INSTDIR\license.txt

Depending on when it gets to the file it can't delete, RMDir /R may leave most of it behind, as I believe it will stop when it can't delete something; this way will get rid of it all properly. This will also lose the directory stats, but that's probably not important.

I'd recommend one of Anders' first two solutions over this, though. They're more precise.

Fabi answered 1/12, 2010 at 22:45 Comment(3)
And if there is not enough room on the drive where $pluginsdir is? $PLUGINSDIR does not have to be on the same drive as $INSTDIR (Granted, a license file is probably not very large, but other files could be)Miley
I forgot about the case where it's on a different drive - Rename won't work across volumes anyway!Fabi
The NSIS docs has this to say about the rename command: "You can use it to move a file from anywhere on the system to anywhere else and you can move a directory to somewhere else on the same drive"Miley
M
4

Off the top of my head, there are 3 ways to do this:

  • Use Delete on one file at the time on a list generated at compile time with !system etc
  • Use FindFirst/FindNext/FindClose at runtime and Delete everything except the license based on filename
  • A bit of a hack, but you should be able to open the license file for write/append, then Delete/RMDir will not be able to delete the file since it has a open handle.
Miley answered 1/12, 2010 at 19:10 Comment(5)
I don't think keeping a handle open on the file would do what's desired - it'd make other things be left behind.Fabi
@Chris Morgan It works just fine: nsis.pastebin.com/wDvmNnCg What do you believe could be left behind? RMDir /r should not really be used, so I'm not sure if that is what you are talking about or notMiley
RMDir /R was what I was talking about. With Delete it'll work as desired.Fabi
Thanks a lot Anders. Could you please elaborate something more on your first suggestion? Since I couldn't get it.Whig
You would just have a list of Delete's. If the list of files you install is large you probably want to call some external script with !system, this script would generate a .nsh full of Delete commands that you include in your main script.Miley

© 2022 - 2024 — McMap. All rights reserved.