Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Recursively Remove Directories and Content
#1
Code: (Select All)
CONST Testing = 0

RMDIR_Recursive "Z:\"

SUB RMDIR_Recursive (Dir$)
IF RIGHT$(Dir$, 1) = "\" OR RIGHT$(Dir$, 1) = "/" THEN Dir$ = LEFT$(Dir$, LEN(Dir$) - 1)
IF _DIREXISTS(Dir$) = 0 THEN EXIT SUB
REDIM FileList(0) AS STRING
GetFileList Dir$, FileList()
FOR i = 1 TO UBOUND(FileList)
IF FileList(i) <> "." AND FileList(i) <> ".." THEN
$IF WIN THEN
file$ = Dir$ + "\" + FileList(i)
$ELSE
file$ = Dir$ + "/" + FileList(i)
$END IF
IF _FILEEXISTS(file$) THEN
IF Testing THEN PRINT "DEL "; file$ ELSE KILL file$
ELSEIF _DIREXISTS(file$) THEN
RMDIR_Recursive file$
END IF
END IF
NEXT
IF LEN(Dir$) > 3 THEN
IF Testing THEN PRINT "RD "; Dir$ ELSE RMDIR Dir$
END IF
END SUB


SUB GetFileList (SearchDirectory AS STRING, FileList() AS STRING)
DECLARE CUSTOMTYPE LIBRARY ".\direntry"
FUNCTION load_dir& (s AS STRING)
FUNCTION has_next_entry& ()
SUB close_dir ()
SUB get_next_entry (s AS STRING, flags AS LONG, file_size AS LONG)
END DECLARE

DIM flags AS LONG, file_size AS LONG

REDIM _PRESERVE FileList(1000)
FileCount = 0

IF load_dir(SearchDirectory) THEN
DO
length = has_next_entry
IF length > -1 THEN
nam$ = SPACE$(length)
get_next_entry nam$, flags, file_size
FileCount = FileCount + 1
IF FileCount > UBOUND(FileList) THEN REDIM _PRESERVE FileList(UBOUND(FileList) + 100)
FileList(FileCount) = nam$
END IF
LOOP UNTIL length = -1
close_dir
ELSE
END IF
REDIM _PRESERVE FileList(FileCount)
END SUB


You'll need the header file below to place in the QB64 folder for this to work. Just point this command at a directory, and it'll remove all files and subdirectories inside that directory, and then remove the directory itself. Note that there's no "Are you certain" prompt to this, so I wouldn't suggest trying it out on your "C:" drive.

My suggestion? Before running, set TESTING to TRUE (any non-zero value), and take a look at what all it'll be getting rid of first for you! Any files you lose with this WILL NOT go into your computer's Recycle Bin, so you can't recover them. This nukes them once and for all, so BE CAREFUL with what you point it at!

You've been warned here first, and if you couldn't bother to read this warning... All I can say is, "I hope you regularly keep things backed up. Too bad for you now! /shrug!"


Attached Files
.h   direntry.h (Size: 1.15 KB / Downloads: 26)
Reply


Messages In This Thread
Recursively Remove Directories and Content - by SMcNeill - 11-30-2023, 05:47 AM



Users browsing this thread: 1 Guest(s)