Skip to content
⌘ NSIS Forum Archive

How to delete files except some files?

3 posts

Contex#

How to delete files except some files?

Well I found this useful script and was wondering how i could add more then 1 file to keep?
Here's the code:


Section
Push "$INSTDIR\test\"
Push "test1.txt"
Call RmFilesButOne
SectionEnd

Function RmFilesButOne
Exch $R0 ; exclude file
Exch
Exch $R1 ; route dir
Push $R2
Push $R3
Push $R4

FindFirst $R3 $R2 "$R1\*.*"
IfErrors Exit

Top:
StrCmp $R2 "." Next
StrCmp $R2 ".." Next
StrCmp $R2 $R0 Next
IfFileExists "$R1\$R2\*.*" Next
Delete "$R1\$R2"

#Goto Exit ;uncomment this to stop it being recursive

Next:
ClearErrors
FindNext $R3 $R2
IfErrors Exit
Goto Top

Exit:
FindClose $R3

Pop $R3
Pop $R2
Pop $R1
Pop $R0
FunctionEnd
Tested alot of things but it seems like they aint working.. Help ^^ 🙂?