@echo off
setlocal EnableDelayedExpansion
set target=C:\The\Target Directory
set source=C:\The\Source Directory
rem Deletes everything in the target directory
echo Y | del "%target%" > NUL
rem Copies any files or folders created in the past hour in the source directory
for /F "tokens=1,2 delims=:" %%a in ("%time%") do set /A "oneHourAgo=(1%%a-101)*60+1%%b-100"
for %%a in ("%source%\*.*") do (
for /F "tokens=2-4 delims=: " %%b in ("%%~Ta") do (
set /A "fileTime=(1%%b-100)*60+1%%c-100"
if "%%d" equ "p.m." (
if %%b neq 12 (
set /A fileTime+=12*60
) else (
set /A fileTime-=12*60
)
)
)
if !fileTime! gtr !oneHourAgo! copy "%%a" "%target%"
)
当“最后一小时”超过午夜时,此批处理文件无法正常工作。如果需要,可以添加这一点。