11-30-2023, 06:44 AM
(11-30-2023, 06:30 AM)SMcNeill Wrote:That is some nice code however I have been using the following code to do the same:(11-30-2023, 06:07 AM)eoredson Wrote: Hey. I got an idea for the makers of QB64pe:
Maybe you could add a KillDir(Dir$) function!?
That's what RMDIR does for you. It's just up to you to make certain the directory is empty, or use the function I provided above.
Code: (Select All)
Rem The Windows QB64 silent directory delete utility PD 2022.
Rem This code placed into the public domain free BASIC source.
Rem For usage please read Silent.txt which contains disclaimer notes.
' declare library constants.
Const MAX_PATH = 260
Const INVALID_HANDLE_VALUE = -1
' declare library structures.
Type FILETIME
dwLowDateTime As _Unsigned Long
dwHighDateTime As _Unsigned Long
End Type
Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Type WIN32_FIND_DATAA
dwFileAttributes As _Unsigned Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As _Unsigned Long
nFileSizeLow As _Unsigned Long
dwReserved0 As _Unsigned Long
dwReserved1 As _Unsigned Long
cFileName As String * Max_path
cAlternateFileName As String * 14
End Type
' declare external libraries.
Declare Dynamic Library "kernel32"
Function DeleteFileA% (F$)
Function FindFirstFileA~%& (ByVal lpFileName~%&, Byval lpFindFileData~%&)
Function FindNextFileA& (ByVal hFindFile~%&, Byval lpFindFileData~%&)
Function FindClose& (ByVal hFindFile~%&)
Function RemoveDirectoryA% (F$)
End Declare
Declare Library
Function GetFileAttributes& (f$)
Function SetFileAttributes& (f$, Byval a&)
End Declare
_Title "DELETE"
Print "Enter directory to delete";: Input G$
If _DirExists(G$) = 0 Then
Print "Directory not fuund."
End
End If
Print "Delete directory and all subfolders/files(y/n)?"
Do
_Limit 100
x$ = LCase$(InKey$)
If x$ = "n" Then End
If x$ = "y" Then Print: Print "OK..": Exit Do
Loop
If Right$(G$, 1) <> "\" Then G$ = G$ + "\"
Call Silentdel_Directories(G$)
If _DirExists(G$) = 0 Then
Print "Directory deleted."
Else
Print "Error deleting directory."
End If
End
' subroutine to access subdirectories
Sub Silentdel_Directories (Directory.Search$)
' declare subroutine variables
Dim Attribute As _Unsigned Long
Dim ASCIIZ As String * 260
Dim finddata As WIN32_FIND_DATAA
Dim Wfile.Handle As _Offset
' make directory filename
ASCIIZ = Directory.Search$ + "*.*" + Chr$(0)
' start directory search
Wfile.Handle = FindFirstFileA(_Offset(ASCIIZ), _Offset(finddata))
If Wfile.Handle <> INVALID_HANDLE_VALUE Then
' delete filenames
Call Silentdel_DeleteFiles(Directory.Search$)
' recurse subdirectories
Do
' check directory attribute
Attribute = finddata.dwFileAttributes
' check directory
If (Attribute And &H10) = &H10 Then
' store directory name
Directory$ = finddata.cFileName
Directory$ = Left$(Directory$, InStr(Directory$, Chr$(0)) - 1)
' store short filename
If InStr(Directory$, "?") Then
Directory$ = finddata.cAlternateFileName
V = InStr(Directory$, Chr$(0))
If V Then Directory$ = Left$(Directory$, V - 1)
End If
' check directory name
If Directory$ <> "." And Directory$ <> ".." Then
' make next search directory
Next.Directory$ = Directory.Search$ + Directory$ + "\"
' recursively search subdirectories
Call Silentdel_Directories(Next.Directory$)
End If
End If
Loop While FindNextFileA(Wfile.Handle, _Offset(finddata))
x = FindClose(Wfile.Handle)
End If
' delete directory
Call Silentdel_DeleteDirectory(Directory.Search$)
End Sub
' subroutine to delete an empty directory
Sub Silentdel_DeleteDirectory (Directory$)
' declare subroutine variables
Dim ASCIIZ As String * 260
' store directory filename
ASCIIZ = Directory$ + Chr$(0)
' change directory attribute
AttrX& = GetFileAttributes(ASCIIZ)
AttrX& = AttrX& And Not &H1 ' remove read-only bit
x = SetFileAttributes&(ASCIIZ, AttrX&)
' delete directory
x = RemoveDirectoryA(ASCIIZ)
End Sub
' subroutine to delete files in a directory
Sub Silentdel_DeleteFiles (Directory$)
' declare subroutine variables
Dim Attribute As _Unsigned Long
Dim ASCIIZ As String * 260
Dim ASCIIZ2 As String * 260
Dim finddata As WIN32_FIND_DATAA
Dim Wfile.Handle As _Offset
' make filename
ASCIIZ = Directory$ + "*.*" + Chr$(0)
Wfile.Handle = FindFirstFileA(_Offset(ASCIIZ), _Offset(finddata))
If Wfile.Handle <> INVALID_HANDLE_VALUE Then
' filename loop
Do
' store filename
Filename$ = finddata.cFileName
Filename$ = Left$(Filename$, InStr(Filename$, Chr$(0)) - 1)
' check filename
If Filename$ <> "." And Filename$ <> ".." Then
' store filename
ASCIIZ2 = Directory$ + Filename$ + Chr$(0)
' change filename attribute
AttrX& = GetFileAttributes(ASCIIZ2)
AttrX& = AttrX& And Not &H1 ' remove read-only bit
x = SetFileAttributes&(ASCIIZ2, AttrX&)
' delete long filename
x = DeleteFileA(ASCIIZ2)
' check error and delete 8.3 filename
If x = 0 Then
Short.Filename$ = finddata.cAlternateFileName
V = InStr(Short.Filename$, Chr$(0))
If V Then Short.Filename$ = Left$(Short.Filename$, V - 1)
ASCIIZ2 = Directory$ + Short.Filename$ + Chr$(0)
' change filename attribute
AttrX& = GetFileAttributes(ASCIIZ2)
AttrX& = AttrX& And Not &H1
x = SetFileAttributes&(ASCIIZ2, AttrX&)
' delete short filename
x = DeleteFileA(ASCIIZ2)
End If
End If
Loop While FindNextFileA(Wfile.Handle, _Offset(finddata))
x = FindClose(Wfile.Handle)
End If
End Sub