#!/usr/bin/env python
# Usage:
# remove_files_older_than.py ./*.jpg
import sys
import glob
import stat
import os
import time
if len(sys.argv)<2:
print "Need 1 argument!"
sys.exit(-1)
expireDays = 7
fileList = glob.glob( sys.argv[1] )
print "Found %d files." % len(fileList)
currentTime = time.localtime()
deletedCount = 0
for filename in fileList:
filestat = os.stat( filename )
modifiedTime = time.localtime( filestat[ stat.ST_MTIME ] )
diff=time.mktime( currentTime )-time.mktime( modifiedTime )
if (diff/(60.0*60*24))>expireDays :
print diff/(60.0*60*24)
print "Remove %s, mtime=%s..." % (filename, time.strftime(
"%Y/%m/%d %H:%M:%S", modifiedTime ) )
os.remove( filename )
deletedCount=deletedCount+1
if deletedCount > 0:
print "Remove %d files" % deletedCount
else:
print "No files expired."
以下則是摘錄自:Windows Shell 刪除七天前資料
PowerShell的用法
Get-ChildItem -Recurse -force C:\files | Where-Object {!($_.Mode -match "d") -and ((Get-Date).Subtract($_.LastWriteTime).TotalDays -gt 7) } | Remove-Item -Force
Bash 的用法
find -mtime +7 -exec rm -f {} \;
沒有留言:
張貼留言