Explorar o código

add cmd scripts:

* delexcept.cmd
delete all files in folder specified exept file or search pattern specified
* delemptydirs
delete all empty folders from directory specified
Dalibor Votruba hai 1 ano
pai
achega
d22dced0b2

+ 59 - 0
@Scripts/Cmd/delemptydirs.cmd

@@ -0,0 +1,59 @@
+@echo off
+setlocal enabledelayedexpansion
+
+:: Check if directory path was provided as argument
+if "%~1"=="" (
+    echo Usage: delemptydirs.cmd "C:\Path\To\Directory"
+    echo.
+    echo Example: delemptydirs.cmd "C:\Users\YourName\Documents"
+    echo.
+    pause
+    exit /b 1
+)
+
+:: Set the target directory
+set "TARGET_DIR=%~1"
+
+:: Verify the directory exists
+if not exist "%TARGET_DIR%" (
+    echo Error: Directory "%TARGET_DIR%" does not exist.
+    pause
+    exit /b 1
+)
+
+echo Starting cleanup of empty folders in: %TARGET_DIR%
+echo.
+
+:: Counter for deleted folders
+set /a DELETED_COUNT=0
+
+:: Function to delete empty folders recursively
+:delete_empty_folders
+for /f "delims=" %%d in ('dir "%TARGET_DIR%" /ad /b /s 2^>nul') do (
+    :: Check if directory is empty (no files and no subdirectories)
+    dir "%%d" /a /b 2>nul | findstr "^" >nul
+    if errorlevel 1 (
+        echo Deleting empty folder: %%d
+        rmdir "%%d" 2>nul
+        if !errorlevel! equ 0 (
+            set /a DELETED_COUNT+=1
+        ) else (
+            echo Warning: Could not delete "%%d" - may be in use or access denied
+        )
+    )
+)
+
+:: Check if any folders were deleted and run again if needed
+:: (parent folders might become empty after deleting subfolders)
+if %DELETED_COUNT% gtr 0 (
+    echo.
+    echo Pass completed. Checking for newly empty parent folders...
+    set /a DELETED_COUNT=0
+    goto delete_empty_folders
+)
+
+echo.
+echo Cleanup completed!
+echo All empty folders have been removed from: %TARGET_DIR%
+echo.
+pause

+ 100 - 0
@Scripts/Cmd/delemptydirs.md

@@ -0,0 +1,100 @@
+# 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
+
+```cmd
+delemptydirs.cmd "C:\Path\To\Target\Directory"
+```
+
+### Examples
+
+```cmd
+# 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:
+
+```cmd
+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.

+ 124 - 0
@Scripts/Cmd/delexcept.cmd

@@ -0,0 +1,124 @@
+@echo off
+setlocal enabledelayedexpansion
+
+:: Parse command line arguments
+set "TARGET_DIR=%~1"
+set "KEEP_FILES=%~2"
+
+:: Use current directory if TARGET_DIR not provided
+if "%TARGET_DIR%"=="" set "TARGET_DIR=%CD%"
+
+:: Default keep files if not provided
+if "%KEEP_FILES%"=="" set "KEEP_FILES=*.log *.cfg"
+
+:: Display usage if help requested
+if "%~1"=="-h" goto :usage
+if "%~1"=="--help" goto :usage
+if "%~1"=="/?" goto :usage
+
+:: Confirm the operation
+echo WARNING: This will delete all files in "%TARGET_DIR%" and subdirectories
+echo EXCEPT files matching these patterns: %KEEP_FILES%
+echo.
+set /p confirm="Are you sure? (Y/N): "
+if /i not "%confirm%"=="Y" (
+    echo Operation cancelled.
+    exit /b
+)
+
+:: Change to target directory
+cd /d "%TARGET_DIR%" 2>nul
+if errorlevel 1 (
+    echo ERROR: Cannot access directory "%TARGET_DIR%"
+    pause
+    exit /b 1
+)
+
+echo.
+echo Processing files in: %TARGET_DIR%
+echo Keep patterns: %KEEP_FILES%
+echo.
+
+:: Process all files recursively
+for /r %%f in (*) do call :ProcessFile "%%f"
+
+echo.
+echo Operation completed!
+pause
+exit /b
+
+:: Subroutine to process individual file
+:ProcessFile
+setlocal enabledelayedexpansion
+set "filepath=%~1"
+set "filename=%~nx1"
+set "skip=0"
+
+:: Check if current file matches any keep pattern
+for %%k in (%KEEP_FILES%) do (
+    if /i "%filename%"=="%%k" (
+        set "skip=1"
+        goto :skipcheck
+    )
+)
+
+:: Check wildcard patterns
+if %skip%==0 (
+    for %%k in (%KEEP_FILES%) do (
+        set "pattern=%%k"
+        set "matched=0"
+        
+        :: Check if pattern contains wildcards
+        echo !pattern! | findstr /C:"*" >nul 2>&1
+        if !errorlevel!==0 set "has_wildcard=1"
+        if not defined has_wildcard (
+            echo !pattern! | findstr /C:"?" >nul 2>&1
+            if !errorlevel!==0 set "has_wildcard=1"
+        )
+        
+        if defined has_wildcard (
+            :: Simple wildcard matching - if pattern is *.jpg, check if filename ends with .jpg
+            if "!pattern:~0,1!"=="*" (
+                set "extension=!pattern:~1!"
+                if /i "!filename:~-4!"=="!extension!" (
+                    set "skip=1"
+                    goto :skipcheck
+                )
+            )
+        )
+    )
+)
+
+:skipcheck
+:: Delete if not in keep list
+if %skip%==0 (
+    echo Deleting: %filepath%
+    del "%filepath%" 2>nul
+    if errorlevel 1 echo WARNING: Could not delete %filepath%
+) else (
+    echo Keeping: %filepath%
+)
+endlocal
+exit /b
+exit /b
+
+:usage
+echo.
+echo Selective File Deletion Script
+echo Usage: %~nx0 [TARGET_DIR] [KEEP_FILES]
+echo.
+echo Arguments:
+echo   TARGET_DIR   - Directory to process (default: current directory)
+echo   KEEP_FILES   - Space-separated patterns of files to keep (supports wildcards)
+echo.
+echo Examples:
+echo   %~nx0                                    - Delete all files in current dir except *.log *.cfg
+echo   %~nx0 "C:\MyFolder"                      - Delete all files in C:\MyFolder except *.log *.cfg
+echo   %~nx0 "C:\MyFolder" "*.txt *.ini"        - Keep only .txt and .ini files
+echo   %~nx0 "." "important.* *.log config.?"   - Keep files matching these patterns
+echo.
+echo Wildcard patterns supported:
+echo   *     - Matches any number of characters
+echo   ?     - Matches single character
+echo.
+exit /b

+ 173 - 0
@Scripts/Cmd/delexcept.md

@@ -0,0 +1,173 @@
+# 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
+
+```cmd
+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
+```cmd
+# 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
+```cmd
+# 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 | `*.txt` → `file.txt`, `document.txt` |
+| `?` | Single character | `log?.txt` → `log1.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:
+```cmd
+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
+```cmd
+# Keep source code, remove build artifacts
+delexcept.cmd "C:\MyProject" "*.cs *.sql *.js *.html *.css"
+```
+
+### Log Directory Maintenance
+```cmd
+# Keep recent logs, remove everything else
+delexcept.cmd "C:\Logs" "*.log *.txt"
+```
+
+### Backup Directory Cleanup
+```cmd
+# Keep specific backup files
+delexcept.cmd "D:\Backups" "backup_*.sql important.*"
+```
+
+### Temporary File Cleanup
+```cmd
+# 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:
+```cmd
+# 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.

+ 6 - 2
@Scripts/Cmd/readme.txt

@@ -1,2 +1,6 @@
-backupsrc.cmd
-backup source files to destination archive located in current directory. Before use edit backupsrc.cmd to correct parameters (paths) 
+* backupsrc.cmd
+backup source files to destination archive located in current directory. Before use edit backupsrc.cmd to correct parameters (paths) 
+* delexcept.cmd
+delete all files in folder specified exept file or search pattern specified
+* delemptydirs
+delete all empty folders from directory specified

BIN=BIN
Assets/Images/qdr_q_logo.png