Code: Select all
; AutoHotkey Script to Delete All Empty Folders and Subfolders in O:/Test
DeleteEmptyFolders(DirPath) {
; Get a list of all directories within the specified directory
Loop, Files, %DirPath%\*, D
{
; Recursively call the function on each subdirectory
DeleteEmptyFolders(A_LoopFileFullPath)
}
; Check if the directory is empty
DirIsEmpty := true
Loop, Files, %DirPath%\*, DR
{
; If any file or directory is found, the directory is not empty
DirIsEmpty := false
break
}
; If the directory is empty, delete it
if (DirIsEmpty) {
FileRemoveDir, %DirPath%
if ErrorLevel
MsgBox, 16, Error, Failed to remove directory: %DirPath%
}
}
; Set the path to the directory you want to clean
TargetDir := "O:/Test"
; Ensure the target directory exists
if FileExist(TargetDir)
{
; Call the function to delete empty folders
DeleteEmptyFolders(TargetDir)
MsgBox, 64, Complete, Empty folders in %TargetDir% have been deleted.
}
else
{
MsgBox, 16, Error, The directory %TargetDir% does not exist.
}
![Image](https://i.vgy.me/xab5cZ.png)
Does anyone know why this message happens and how to prevent it? The script works as intended.
[Mod action: Topic moved from "Ask for Help (v2)" since this is v1 code.]