Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Windowsのbatファイルとして保存し、好きなフォルダに入れて実行すると過去7日間アクセスしていないファイルをすべてゴミ箱に移す。
REM = r"""
SET PYTHON_EXE="python.exe"
%PYTHON_EXE% %0 %1
PAUSE
EXIT
"""
import os
import glob
import time
import send2trash
# TODO: make a log file
root_dir = os.path.abspath(os.path.dirname(__file__))
os.chdir(root_dir)
files = glob.glob("./**")
print(f"{len(files)} files here.")
print(files)
current_time = time.time()
print("Now deleting unused files...")
for f in files:
name, ext = os.path.splitext(f)
if ext == ".bat" or ext == ".py":
continue
atime = os.path.getatime(f) # last access time
ctime = os.path.getctime(f) # last metadata change time
mtime = os.path.getmtime(f) # last modification time
# delete file which you don't access within a week
days_from_last_access = (current_time - atime)/3600/24
if (days_from_last_access >= 7):
send2trash.send2trash(f)
print(f"--- {f}")
print("Completed.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment