Skip to content
⌘ NSIS Forum Archive

deleting question

13 posts

gieltje#

deleting question

Hi all,

I need to delete some folders that are not in the install folder, and have varying names.

I cannot simply delete the folder in which they are because it is a system folder.

example;
c:\blaat
c:\blaat\1
C:\blaat\2
Now 1 & 2 need to be deleted, but blaat cannot be.

How do I this?
evilO#
Hi 🙂

4.9.1.8 RMDir
[/r|/REBOOTOK] directory_name

Remove the specified directory (which should be a full path). Without /r, the directory will only be removed if it is completely empty. If /r is specified, the directory will be removed recursively, so all directories and files in the specified directory will be removed. If /REBOOTOK is specified, and the directory cannot be overwritten, then the directory will be deleted when the system reboots -- if the directory will be removed on a reboot, the reboot flag will be set. The error flag is set if the directory cannot be removed.
I don't know exactly if I understood your problem, but to remove these directories you just have to do that:

- If the folders are empty:

RmDir c:\blaat\1
RmDir c:\blaat\2


- If the folders are *not* empty:

RmDir /r c:\blaat\1
RmDir /r c:\blaat\2



Is that what you want to achieve ?

evilO/Olive
gieltje#
allmost, another problem is the names of these dirs are always changing so RMDir "c:\blaat\*" should be it but it does not delete every dir in c:\blaat but trys to remove c:\blaat which cannot be done (because it is a system folder)
evilO#
Ok, I got it now. You can use the following code to achieve this, but BE CAREFUL: it will remove EVERY SUBDIRECTORY (not the files) of the specified directory. Especially if "c:\blaat" is a system folder.

Function EmptyFolder
  Exch $R0 ; Name of the folder to empty
  Push $R1 ; Search handle
  Push $R2 ; Current file or folder
  
  ${If} ${FileExists} "$R0\\*.*"                  ; If the current element is a directory
    ClearErrors
    FindFirst $R1 $R2 "$R0\\*.*"                  ; Starts the search
    ${Unless} ${Errors}
      ${Do}
        ${Select} $R2
          ${Case2} "." ".."                      ; If the current element is '.' or '..' do nothing
          ${CaseElse}                            ; Otherwise
            ${If} ${FileExists} "$R0\\$R2\\*.*"    ; If the current element is a directory
              Rmdir /r "$R0\\$R2"                 ; Removes it
            ${EndIf}
        ${EndSelect}
        FindNext $R1 $R2                         ; Continues the search
      ${LoopUntil} ${Errors}
      FindClose $R1                              ; Closes the search handle
    ${EndUnless}
  ${EndIf}
  Pop $R2
  Pop $R1
  Pop $R0
FunctionEnd 
And to use it:

Push "c:\\blaat"
Call EmptyFolder 
I hope that helps 🙂

evilO/Olive
gieltje#
it does the correct thing, but wont compile😕
${If} ${FileExists} "$R0\*.*" ; If the current element
that's the line it breaks on😔
gieltje#
${If} ${FileExists} "$R0\*.*" ; If the current element is a directory
ClearErrors
FindFirst $R1 $R2 "$R0\*.*" ; Starts the search
${Unless} ${Errors}
${Do}
${Select} $R2
${Case2} "." ".." ; If the current element is '.' or '..' do nothing
${CaseElse} ; Otherwise
${If} ${FileExists} "$R0\$R2\*.*" ; If the current element is a directory
Rmdir /r "$R0\$R2" ; Removes it
${EndIf}
Delete "$R0\*.*" ; Removes every file from the root folder
${EndSelect}
FindNext $R1 $R2 ; Continues the search
${LoopUntil} ${Errors}
FindClose $R1 ; Closes the search handle
${EndUnless}
${EndIf}

Added a little thing to remove every file in the root folder if anyone is interested