FOR /F "tokens=1-4 delims=/ " %%a IN ("%date%") DO (
SET _MyDate=%%a%%b%%c
)
set /A _MyDate=%_MyDate%-30
rmdir %_MyDate% /s/q
1.使用forfiles指令自動刪除特定目錄下超過N天的任何文件,下面工作目錄為e:\97Temp,7天,包含子目錄
<pre class="c" name="code">@echo off
@forfiles /p "e:\97Temp" /s /m *.* /d -7 /c "cmd /c del @path"
@dir /ad /b /s "e:\97Temp" |sort /r > "E:\Emptydir.txt"
@For /f "tokens=*" %%i in (E:\Emptydir.txt) DO rd "%%i"
@del E:\Emptydir.txt
@exit
2.日期變數
<pre class="c" name="code"> %date:~0,4%%date:~5,2%%date:~8,2%
Robocopy 也能做到
搬移7日前檔案:先建立當日目錄,才搬移檔案,含目錄,並記錄
<pre class="c" name="code">/PURGE --刪除來源中不再存在的目的檔案/目錄
<pre class="c" name="code">mkdir E:\98Delete\%date:~0,4%%date:~5,2%%date:~8,2%
@robocopy E:\99Delete\ E:\98Delete\%date:~0,4%%date:~5,2%%date:~8,2% /s *.* /maxage:999 /minage:7 /move /v /fp /TS /tee /log+:E:\97Temp\move7DaysBefore_%date:~0,4%%date:~5,2%%date:~8,2%.log
修正:實測無誤的指令:
<pre class="c" name="code">forfiles -pE:\97Temp -d-7 -c"CMD /C del @FILE"