Additional keywords: folder, recursive, tree
WARNING:
Use this tip with extreme care! You could easily wipe out an entire hard
drive if you specify a root directory by mistake! You have been warned...
I strongly suggest that your app checks if len(dir$) before calling this
procedure, because it's most likely to be a root directory if less than 4 chars,
for example C: or C:\, and you can perhaps warn the user about their actions.
In association with Ralf, we have come up with this Windows procedure that deletes
a directory and EVERYTHING inside it, regardless of whether the contents are marked
as system, hidden, or read-only.
USAGE: DelDirectory(dir$,0) ; dir$ = directory to delete.
Code: Select all
Procedure DelDirectory(dir$,Start)
If ExamineDirectory(Start,dir$,"*.*")
Repeat
Type=NextDirectoryEntry()
If Type=2 ; Directory.
If DirectoryEntryName()"." And DirectoryEntryName()".."
c$=dir$+DirectoryEntryName()+"\"
DelDirectory(c$,Start+1)
UseDirectory(Start)
EndIf
Else
If Type=1 ; File.
SetFileAttributes_(dir$+DirectoryEntryName(),#FILE_ATTRIBUTE_NORMAL)
DeleteFile_(dir$+DirectoryEntryName()) ; Yes, I prefer API calls. :wink:
EndIf
EndIf
Until Type=0 ; No more entries.
SetFileAttributes_(dir$,#FILE_ATTRIBUTE_NORMAL)
RemoveDirectory_(dir$) ; Remove empty dir as we go.
EndIf
EndProcedure
;
; NOW, TO TEST IT! :)
;
; Make a test directory.
CreateDirectory_("C:\TestDir\",0) ; Yes, I prefer API calls. :wink:
;
; Make a test file inside it.
If CreateFile(0,"C:\TestDir\test.txt")
WriteStringN("test")
CloseFile(0)
; Make the test file "undeleteable".
SetFileAttributes_("C:\TestDir\test.txt",#FILE_ATTRIBUTE_READONLY)
EndIf
;
; Let user check it out.
MessageRequester("Info","Look at C:\TestDir\text.txt to see that it's there...",0)
;
; Delete the folder and EVERYTHING inside it, including the "undeleteable" file.
DelDirectory("C:\TestDir\",0)
MessageRequester("Info","And now it's all gone!",0)
PB - Registered PureBasic Coder
Edited by - PB on 30 January 2002 16:08:17