Archive: Delete all directories except 1


Delete all directories except 1
Is there a way to:

RMDIR /R "C:\SomeDir\*" exclude="OneDirName"

Thanks,
Eric


You'll have to specify the names of all other directories.


Ouch! Thanks!


Or use FindFirst, FindNext and FindClose to find all the directories and simply skip the directory you don't want to delete.


I actually made an app which does the reverse of what you're looking for stonkers. It will scan a folder you have specified to include in an installer and exclude those files/folders you specify. Very useful if you have something like a CVS working copy and want to exclude all those "CVS" folders.

You can read about it more here,

http://forums.winamp.com/showthread....ght=FolderList

I was going to make a RMDIR type version as well but got side tracked with a bunch of other tasks at work. If there is demand perhaps I'll look into it again or if there's anyone who knows STL and C++ you can help me do it. I would welcome the extra hands.


It looks to me like FindFirst, FindNext, and FindClose work on files rather than directories. Is there a way to specify only to find directories?

Thanks Again,
Eric


no, findfirst etc. also finds directorys. you can check for directorys by using:

IfFileExists $DIRECTORY\*.* isDirectory isFile

OK, I wrote a little tester and got it to work. Can someone point out things I could have done to make this simpler:

***********************************************
;Backgound Colors
BGGradient 800080 000000 FFFFFF
BrandingText " "

;Title Of Your Application
Name "KillAllButOne"

;Output File Name
OutFile "KillAllBut1.exe"

;The Default Installation Directory
InstallDir "D:\KillButOne"

;The text to prompt the user to enter a directory
DirText "Wait for processes to finish and reboot"

Section "KillDirectories"

FindFirst $0 $1 D:\KillButOne\*
StrCmp $1 "" GetOut
IfFileExists D:\KillButOne\$1 kill continue

kill:
call KillDir

continue:

DoAgain:
FindNext $0 $1
StrCmp $1 "" GetOut
IfFileExists D:\KillButOne\$1 kill2 continue2

kill2:
call KillDir

continue2:
goto DoAgain
GetOut:
FindClose $1

SectionEnd

;--------------------------------
; Functions


Function KillDir
StrCmp $1 "." skip
StrCmp $1 ".." skip
StrCmp $1 "DontKillMe" skip
RMDIR /r D:\KillButOne\$1

MessageBox MB_OKCANCEL|MB_ICONEXCLAMATION "Killed the directory $1" IDOK skip
skip:

FunctionEnd
***********************************************


Thanks,
Eric


I'm definitely doing something wrong because it kills files as well as directories.


The find commands find both files and directories.


Is there a command that tells me whether I have a file or directory? I thought I could use fileExists to do that, but it doesn't seem to do this.


IfFileExists "FileOrFolderName/*.*" Folder File

Replace "FileOrFolderName" with the variable where the folder and file names are found.

Files don't have other files inside themselves and folders that don't have files in them have at least the folder ".", so this above will work.


Ahhhh, you tricky deguix, Thanks!


I'll post the working example in case anyone needs one in the future:

;NSIS Script For Doc

;Backgound Colors
BGGradient 800080 000000 FFFFFF
BrandingText " "

;Title Of Your Application
Name "KillAllButOne"

;Output File Name
OutFile "KillAllBut1.exe"

;The Default Installation Directory
InstallDir "D:\KillButOne"

;The text to prompt the user to enter a directory
DirText "Wait for processes to finish and reboot"

Section "KillDirectories"

FindFirst $0 $1 $INSTDIR\*
StrCmp $1 "" GetOut
IfFileExists $INSTDIR\$1\*.* kill continue

kill:
call KillDir

continue:

DoAgain:
FindNext $0 $1
StrCmp $1 "" GetOut
IfFileExists $INSTDIR\$1\*.* kill2 continue2

kill2:
call KillDir

continue2:
goto DoAgain
GetOut:
FindClose $1

SectionEnd

;--------------------------------
; Functions


Function KillDir
StrCmp $1 "." skip
StrCmp $1 ".." skip
StrCmp $1 "DontKillMe" skip
RMDIR /r $INSTDIR\$1

MessageBox MB_OKCANCEL|MB_ICONEXCLAMATION "Killed the directory $1" IDOK skip
skip:

FunctionEnd


Hi stonkers:

Read your code to remove all but one dir...:)

Optimized it and made it into a function. :)

Modular coding and all...you know :)

There are a bunch of messageboxes for "debugging" purposes :)

Enjoy!


I have re-written both functions (for dirs and files) onto the archive:

http://nsis.sourceforge.net/archive/...b.php?page=618

-Stu