将下面的代码复制粘贴到记事本,然后将其扩展名改为.bat,双击运行即可。
@echo off
chcp 65001
REM 设置搜索路径和关键字
set searchPath=C:\Users\XXXX\Desktop\BAT
set searchKey=test
REM 查找包含关键字的文件
for /f "delims=" %%i in ('dir /b /s /a-d "%searchPath%" ^| findstr /i /c:"%searchKey%"') do (
REM 输出包含关键字的文件到文件
echo "%%i" >> deletedFileList.txt
REM 删除包含关键字的文件
del "%%i" /f /s /q
)
REM 查找包含关键字的文件夹
for /f "delims=" %%i in ('dir /b /s /ad "%searchPath%" ^| findstr /i /c:"%searchKey%"') do (
REM 删除包含关键字的文件夹
rd "%%i" /s /q
)
pause