본문으로 바로가기

날짜기준 파일 삭제하기

category Win Style/WinAll 2019. 6. 17. 10:11
반응형

날짜기준 폴더삭제하기
하위까지 4일전
ForFiles /P D:\ /D -4 /C “CMD /C if @ISDIR==TRUE echo RD /Q @FILE &RD /Q /S @FILE”

날짜기준으로 파일 삭제하기
~날 이전.
아래는 현재기준 15일이전에 생성된 파일 삭제
/s                 모든 하위폴더포함
/m 파일명      *.* 모든파일
/d 날짜         -30 30일이전


-d /d 같은 옵션

forfiles /p D:\drvbak /s /m *.* /d -30 /c "cmd /c del /f /q @path"


해당폴더에서 해당날수 이하 삭제할 파일보기 할때는 아래처럼 echo로
forfiles /p "c:\FOLDERpath" /s /d -30 /c "cmd /c echo @path @fdate"

http://stackoverflow.com/questions/51054/batch-file-to-delete-files-older-than-n-days/1322886#1322886

7daysClean.CMD
현재부터 7일치유지 7일 이전것들 지움.
____Usage____
set /a strip=day*7: Change 7 for the number of days to keep.
set dSource=C:\temp: This is the starting directory to check for files.

____Notes____

This is non-destructive code, it will display what would have happened. Change :
if !epoch! LEQ %slice% (echo DELETE %%f ^(%%~tf^)) ELSE echo keep %%f ^(%%~tf^) 
to something like :
if !epoch! LEQ %slice% del /f %%f
so files actually get deleted



@echo off
setlocal ENABLEDELAYEDEXPANSION
set day=86400
set /a year=day*365
set /a strip=day*7
set dSource=C:\temp

call :epoch %date%
set /a slice=epoch-strip

for /f "delims=" %%f in ('dir /a-d-h-s /b /s %dSource%') do (
    call :epoch %%~tf
    if !epoch! LEQ %slice% (echo DELETE %%f ^(%%~tf^)) ELSE echo keep %%f ^(%%~tf^)
)
exit /b 0

rem Args[1]: Year-Month-Day
:epoch
    setlocal ENABLEDELAYEDEXPANSION
    for /f "tokens=1,2,3 delims=-" %%d in ('echo %1') do set Years=%%d& set Months=%%e& set Days=%%f
    if "!Months:~0,1!"=="0" set Months=!Months:~1,1!
    if "!Days:~0,1!"=="0" set Days=!Days:~1,1!
    set /a Days=Days*day
    set /a _months=0
    set i=1&& for %%m in (31 28 31 30 31 30 31 31 30 31 30 31) do if !i! LSS !Months! (set /a _months=!_months! + %%m*day&& set /a i+=1)
    set /a Months=!_months!
    set /a Years=(Years-1970)*year
    set /a Epoch=Years+Months+Days
    endlocal& set Epoch=%Epoch%
    exit /b 0

반응형