delexcept.md 4.6 KB

DelExcept - Selective File Deletion Script

A Windows batch script (delexcept.cmd) that deletes all files in a directory and its subdirectories while preserving files that match specified patterns.

Features

  • Recursive deletion - Processes all subdirectories
  • Pattern matching - Supports wildcards (* and ?)
  • Safety confirmation - Prompts before execution
  • Flexible arguments - Command line configurable
  • Error handling - Handles access errors gracefully
  • Progress feedback - Shows which files are kept/deleted

Usage

delexcept.cmd [TARGET_DIR] [KEEP_FILES]

Arguments

Argument Description Default
TARGET_DIR Directory to process Current directory (%CD%)
KEEP_FILES Space-separated patterns of files to preserve *.log *.cfg

Examples

Basic Usage

# Delete all files in current directory except *.log and *.cfg
delexcept.cmd

# Process specific directory with default keep patterns
delexcept.cmd "C:\MyFolder"

# Keep only text files in specified directory
delexcept.cmd "C:\Temp" "*.txt"

Advanced Pattern Matching

# Keep multiple file types
delexcept.cmd "D:\Project" "*.log *.cfg *.ini *.txt"

# Mix wildcards with exact filenames
delexcept.cmd "." "important.sql backup.* *.log"

# Use single character wildcards
delexcept.cmd "C:\Data" "config.? log*.txt"

Wildcard Patterns

Pattern Description Example Matches
* Any number of characters *.txtfile.txt, document.txt
? Single character log?.txtlog1.txt, logA.txt
*.* Any file with extension All files with extensions
file.* Specific name, any extension file.txt, file.log, file.cfg

Safety Features

Confirmation Prompt

The script displays a warning and requires user confirmation:

WARNING: This will delete all files in "C:\MyFolder" and subdirectories
EXCEPT files matching these patterns: *.log *.cfg

Are you sure? (Y/N):

Error Handling

  • Validates directory access before processing
  • Reports files that cannot be deleted
  • Graceful handling of permission errors

Getting Help

Display usage information:

delexcept.cmd -h
delexcept.cmd --help
delexcept.cmd /?

Installation

  1. Save the script as delexcept.cmd
  2. Place it in a directory in your PATH, or
  3. Run it from its location using full path

Common Use Cases

Development Environment Cleanup

# Keep source code, remove build artifacts
delexcept.cmd "C:\MyProject" "*.cs *.sql *.js *.html *.css"

Log Directory Maintenance

# Keep recent logs, remove everything else
delexcept.cmd "C:\Logs" "*.log *.txt"

Backup Directory Cleanup

# Keep specific backup files
delexcept.cmd "D:\Backups" "backup_*.sql important.*"

Temporary File Cleanup

# Remove temp files but keep configuration
delexcept.cmd "%TEMP%" "*.cfg *.ini *.conf"

Important Notes

⚠️ Warning: This script permanently deletes files. Always test with a backup or in a safe environment first.

Best Practices

  1. Test first: Run the script in a test directory to verify pattern matching
  2. Use quotes: Always quote paths with spaces: "C:\My Folder"
  3. Check patterns: Verify your wildcard patterns match the intended files
  4. Backup important data: Always maintain backups of critical files

Limitations

  • Case-insensitive pattern matching only
  • No regular expression support (only * and ? wildcards)
  • Requires manual confirmation for each execution
  • Windows-only (batch script)

Troubleshooting

Common Issues

"Cannot access directory" error

  • Verify the directory path exists
  • Check permissions for the target directory
  • Ensure no other processes are using files in the directory

Files not matching expected patterns

  • Test patterns with dir command first: dir *.log
  • Remember patterns are case-insensitive
  • Use quotes around patterns with special characters

Permission denied errors

  • Run as administrator if needed
  • Close applications that might be using the files
  • Check file attributes (read-only, hidden)

Testing Patterns

Before running the deletion script, test your patterns:

# Test what files match your pattern
dir /s "*.log"
dir /s "config.*"

Version History

  • v1.0: Initial release with basic functionality
  • v1.1: Added command line arguments and wildcard support
  • v1.2: Enhanced pattern matching and error handling

License

This script is provided as-is for educational and practical use. Use at your own risk.