delemptydirs.md 3.3 KB

delemptydirs.cmd

A Windows CMD script to recursively delete empty folders from a specified directory and all its subdirectories.

Description

This script provides a safe and efficient way to clean up directory structures by removing empty folders. It's particularly useful for:

  • Cleaning up project directories after file moves or deletions
  • Maintaining organized folder structures
  • Preparing directories for backup or archival
  • General filesystem maintenance

The script performs multiple passes to ensure that parent directories which become empty after their subdirectories are removed are also cleaned up.

Features

  • Recursive scanning - Processes all subdirectories at any depth
  • Multi-pass cleanup - Handles nested empty folders by running multiple passes
  • Input validation - Verifies that the target directory exists before processing
  • Progress feedback - Shows which folders are being deleted
  • Error handling - Reports folders that cannot be deleted (in use, access denied, etc.)
  • Safe operation - Only deletes truly empty folders (no files, no subdirectories)

Usage

Command Line

delemptydirs.cmd "C:\Path\To\Target\Directory"

Examples

# Clean up a Downloads folder
delemptydirs.cmd "C:\Users\YourName\Downloads"

# Clean up a project directory
delemptydirs.cmd "D:\Projects\MyProject"

# Clean up with spaces in path
delemptydirs.cmd "C:\Program Files\Some Application"

Interactive Usage

You can also run the script without parameters, and it will display usage instructions:

delemptydirs.cmd

Installation

  1. Save the script content as delemptydirs.cmd
  2. Place it in a directory that's in your PATH (optional, for global access)
  3. Make sure you have appropriate permissions for the target directories

Requirements

  • Windows operating system
  • CMD.exe (Command Prompt)
  • Read/write permissions for target directories

How It Works

  1. Input Validation: Checks if a directory path was provided and if it exists
  2. Directory Scanning: Uses dir /ad /b /s to find all subdirectories recursively
  3. Empty Check: For each directory, uses dir /a /b to check if it contains any files or subdirectories
  4. Deletion: Removes directories that are completely empty using rmdir
  5. Multi-pass: Repeats the process until no more empty directories are found

Safety Notes

  • The script only deletes completely empty folders
  • It will not delete folders containing hidden files or system files
  • Folders that are in use or access-denied will be skipped with a warning
  • Always test on a backup or non-critical directory first

Troubleshooting

"Access Denied" Errors

  • Run Command Prompt as Administrator
  • Check folder permissions
  • Ensure folders are not in use by other applications

Script Doesn't Run

  • Verify the script is saved with .cmd extension
  • Check that execution policies allow batch file execution
  • Ensure the target path is enclosed in quotes if it contains spaces

No Folders Deleted

  • Verify the target directory actually contains empty folders
  • Check that you have write permissions to the target directory
  • Some "empty" folders may contain hidden system files

License

This script is provided as-is for educational and utility purposes. Use at your own risk and always backup important data before running cleanup operations.