delexcept.cmd 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. :: Parse command line arguments
  4. set "TARGET_DIR=%~1"
  5. set "KEEP_FILES=%~2"
  6. :: Use current directory if TARGET_DIR not provided
  7. if "%TARGET_DIR%"=="" set "TARGET_DIR=%CD%"
  8. :: Default keep files if not provided
  9. if "%KEEP_FILES%"=="" set "KEEP_FILES=*.log *.cfg"
  10. :: Display usage if help requested
  11. if "%~1"=="-h" goto :usage
  12. if "%~1"=="--help" goto :usage
  13. if "%~1"=="/?" goto :usage
  14. :: Confirm the operation
  15. echo WARNING: This will delete all files in "%TARGET_DIR%" and subdirectories
  16. echo EXCEPT files matching these patterns: %KEEP_FILES%
  17. echo.
  18. set /p confirm="Are you sure? (Y/N): "
  19. if /i not "%confirm%"=="Y" (
  20. echo Operation cancelled.
  21. exit /b
  22. )
  23. :: Change to target directory
  24. cd /d "%TARGET_DIR%" 2>nul
  25. if errorlevel 1 (
  26. echo ERROR: Cannot access directory "%TARGET_DIR%"
  27. pause
  28. exit /b 1
  29. )
  30. echo.
  31. echo Processing files in: %TARGET_DIR%
  32. echo Keep patterns: %KEEP_FILES%
  33. echo.
  34. :: Process all files recursively
  35. for /r %%f in (*) do call :ProcessFile "%%f"
  36. echo.
  37. echo Operation completed!
  38. pause
  39. exit /b
  40. :: Subroutine to process individual file
  41. :ProcessFile
  42. setlocal enabledelayedexpansion
  43. set "filepath=%~1"
  44. set "filename=%~nx1"
  45. set "skip=0"
  46. :: Check if current file matches any keep pattern
  47. for %%k in (%KEEP_FILES%) do (
  48. if /i "%filename%"=="%%k" (
  49. set "skip=1"
  50. goto :skipcheck
  51. )
  52. )
  53. :: Check wildcard patterns
  54. if %skip%==0 (
  55. for %%k in (%KEEP_FILES%) do (
  56. set "pattern=%%k"
  57. set "matched=0"
  58. :: Check if pattern contains wildcards
  59. echo !pattern! | findstr /C:"*" >nul 2>&1
  60. if !errorlevel!==0 set "has_wildcard=1"
  61. if not defined has_wildcard (
  62. echo !pattern! | findstr /C:"?" >nul 2>&1
  63. if !errorlevel!==0 set "has_wildcard=1"
  64. )
  65. if defined has_wildcard (
  66. :: Simple wildcard matching - if pattern is *.jpg, check if filename ends with .jpg
  67. if "!pattern:~0,1!"=="*" (
  68. set "extension=!pattern:~1!"
  69. if /i "!filename:~-4!"=="!extension!" (
  70. set "skip=1"
  71. goto :skipcheck
  72. )
  73. )
  74. )
  75. )
  76. )
  77. :skipcheck
  78. :: Delete if not in keep list
  79. if %skip%==0 (
  80. echo Deleting: %filepath%
  81. del "%filepath%" 2>nul
  82. if errorlevel 1 echo WARNING: Could not delete %filepath%
  83. ) else (
  84. echo Keeping: %filepath%
  85. )
  86. endlocal
  87. exit /b
  88. exit /b
  89. :usage
  90. echo.
  91. echo Selective File Deletion Script
  92. echo Usage: %~nx0 [TARGET_DIR] [KEEP_FILES]
  93. echo.
  94. echo Arguments:
  95. echo TARGET_DIR - Directory to process (default: current directory)
  96. echo KEEP_FILES - Space-separated patterns of files to keep (supports wildcards)
  97. echo.
  98. echo Examples:
  99. echo %~nx0 - Delete all files in current dir except *.log *.cfg
  100. echo %~nx0 "C:\MyFolder" - Delete all files in C:\MyFolder except *.log *.cfg
  101. echo %~nx0 "C:\MyFolder" "*.txt *.ini" - Keep only .txt and .ini files
  102. echo %~nx0 "." "important.* *.log config.?" - Keep files matching these patterns
  103. echo.
  104. echo Wildcard patterns supported:
  105. echo * - Matches any number of characters
  106. echo ? - Matches single character
  107. echo.
  108. exit /b