for /f "delims=" %%a in ('dir /b /ad-h /t:c "%target_dir%" ^| findstr /l /c:"<"') do (
set "dir_path=%target_dir%%%a"
for /f "delims=" %%b in ('dir /b /a-d /t:c "%dir_path%" ^| findstr /l /c:"<"') do (
set "file_path=%dir_path%%%b"
setlocal enableDelayedExpansion
set "file_create_time=!file_path:=!"
for /f "usebackq tokens=1-6 delims=/: " %%c in ('dir /tc "!file_path!" ^| findstr /l /c:"<"') do (
set "create_date=%%f-%%d-%%e"
set "create_time=%%g"
set "create_timestamp=!create_date!T!create_time!Z"
)
setlocal disableDelayedExpansion
set "current_time=%date:~10,4%-%date:~4,2%-%date:~7,2"T"%time:~0,2%:%time:~3,2%:%time:~6,2%Z"
set /a "time_diff=(%current_time:/=-%) - (%create_timestamp:/=-%)"
if !time_diff! leq 300 (
echo Deleting file: !file_path!
del /q "!file_path!"
)
)
rd /s /q "!dir_path!"
)
解释:
1. set "target_dir=C:Windows1" - 设置目标目录的路径。
2. for /f "delims=" %%a in ('dir /b /ad-h /t:c "%target_dir%" ^| findstr /l /c:"<"') do ( - 遍历目标目录下的所有子目录,排除隐藏目录,并使用 findstr 过滤出创建时间在当前时间之前的目录(即创建时间与当前时间相差不超过5分钟的目录)。
3. set "dir_path=%target_dir%%%a" - 获取目录的完整路径。
4. for /f "delims=" %%b in ('dir /b /a-d /t:c "%dir_path%" ^| findstr /l /c:"<"') do ( - 遍历目录下的所有文件,排除目录,并使用 findstr 过滤出创建时间在当前时间之前的文件。