Skip to content

Instantly share code, notes, and snippets.

@leduyquang753
Last active March 2, 2020 07:46
Show Gist options
  • Save leduyquang753/9d6bf3212153720d835c1401ea8452f6 to your computer and use it in GitHub Desktop.
Save leduyquang753/9d6bf3212153720d835c1401ea8452f6 to your computer and use it in GitHub Desktop.
Temporary folder cleaner
""" TEMPORARY FOLDER CLEANUP
This is a small script that allows you to set up a folder to temporary files, which will be automatically erased after some time. Set this script to run periodically on your system, on Windows you can use the Task scheduler.
A file will be erased if it has been first added/edited for more than the lifetime specified.
––– BEGIN LICENSE –––
Copyright © 2020 Lê Duy Quang
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "software"), to deal in the software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the software, and to permit persons to whom the software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the software.
The software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software.
–––– END LICENSE ––––
This script adapts a wildcard matching algorithm by IBM Corporation, which is licensed under the Apache license version 2.0. You may read the license from: www.apache.org/licenses/LICENSE-2.0.
"""
import os
import os.path
import math
import time
import pathlib
import hashlib
# The path of the temporary folder.
temporaryFolderPath = r"D:\Temporary files"
# Lifetime of each file until it is deleted, in seconds.
lifetime = 86400
# These files will be ignored by the script. Put for example system-related files here. You may use wildcards: "*" and "?".
ignoredFiles = ["desktop.ini"]
ignoredFiles = [str.lower() for str in ignoredFiles]
def getHash(file):
hashInstance = hashlib.sha3_512()
with open(file, r"rb") as file:
for chunk in iter(lambda: file.read(4096), b""): hashInstance.update(chunk)
return hashInstance.hexdigest()
def getChar(string, strLen, index):
return None if index >= strLen else string[index]
def matchWildcard(string, wildcard):
iWild = 0
strLen = len(string)
wildLen = len(wildcard)
while True:
if getChar(string, strLen, iWild) == None:
if getChar(wildcard, wildLen, iWild) != None:
while getChar(wildcard, wildLen, iWild) == "*":
iWild += 1
if getChar(wildcard, wildLen, iWild) == None: return True
return False
else: return True
elif getChar(wildcard, wildLen, iWild) == "*":
iStr = iWild
iWild += 1
while (getChar(wildcard, wildLen, iWild) == "*"): iWild += 1
if getChar(wildcard, wildLen, iWild) == None: return True
if getChar(wildcard, wildLen, iWild) != "?":
while getChar(wildcard, wildLen, iWild) != getChar(string, strLen, iStr):
iStr += 1
if getChar(string, strLen, iWild) == None: return False
iWildSeq = iWild
iStrSeq = iStr
break
elif getChar(wildcard, wildLen, iWild) != getChar(string, strLen, iWild) and getChar(wildcard, wildLen, iWild) != "?": return False
iWild += 1
while True:
if getChar(wildcard, wildLen, iWild) == "*":
iWild += 1
while getChar(wildcard, wildLen, iWild) == "*": iWild += 1
if getChar(wildcard, wildLen, iWild) == None: return True
if getChar(string, strLen, iStr) == None: return False
if getChar(wildcard, wildLen, iWild) != "?":
while getChar(wildcard, wildLen, iWild) != getChar(string, strLen, iStr):
iStr += 1
if getChar(string, strLen, iStr) == None: return False
iWildSeq = iWild
iStrSeq = iStr
elif getChar(wildcard, wildLen, iWild) != getChar(string, strLen, iStr) and getChar(wildcard, wildLen, iWild) != "?":
if getChar(string, strLen, iStr) == None: return False
while getChar(wildcard, wildLen, iWildSeq) == "?":
iWildSeq += 1
iStrSeq += 1
iWild = iWildSeq
iStrSeq += 1
while getChar(wildcard, wildLen, iWild) != getChar(string, strLen, iStrSeq):
if getChar(string, strLen, iStrSeq) == None: return False
iStrSeq += 1
iStr = iStrSeq
if getChar(string, strLen, iWild) == None: return getChar(wildcard, wildLen, iWild) == None
iWild += 1
iStr += 1
dataFile = os.path.join(pathlib.Path(__file__).parent.absolute(), "Temporary folder cleanup data.txt")
currentTime = math.floor(time.time())
eraseTime = currentTime - lifetime + 1
currentTimeStr = str(currentTime)
# Key: File path; Value: (File hash; Time)
entries = {}
newEntries = {}
try:
f = open(dataFile, "r", encoding="utf-8")
dataPiece = 0 # 0: File path; 1: Hash; 2: Time
currentFile = ""
currentHash = ""
for line in f:
stripped = line.strip()
if stripped == "": continue
if dataPiece == 0: currentFile = stripped # File path
elif dataPiece == 1: currentHash = stripped # Hash
else: entries[currentFile] = (currentHash, int(stripped)) # Timestamp
dataPiece = (dataPiece+1)%3
f.close()
except: pass
def shouldFileBeIgnored(fileName):
for wildcard in ignoredFiles:
if matchWildcard(fileName, wildcard): return True
return False
for (path, folders, files) in os.walk(temporaryFolderPath, topdown=False):
for file in files:
if shouldFileBeIgnored(file.lower()): continue
filePath = os.path.join(path, file).lower()
fileHash = getHash(filePath)
if filePath in entries and entries[filePath][0] == fileHash:
if entries[filePath][1] < eraseTime:
try: os.remove(filePath)
except: newEntries[filePath] = entries[filePath] # File deletion failed. Retry next time.
else: newEntries[filePath] = entries[filePath]
else: newEntries[filePath] = (fileHash, currentTime)
for folder in folders:
try: os.rmdir(os.path.join(path, folder))
except: pass
f = open(dataFile, "w", encoding="utf-8")
for filePath in newEntries:
val = newEntries[filePath]
f.write(filePath + "\n" + val[0] + "\n" + str(val[1]) + "\n")
f.close()
<?xml version="1.0" encoding="UTF-16"?>
<!-- WINDOWS TASK SCHEDULER TASK FOR TEMPORARY FOLDER CLEANUP
This is a Windows task scheduler's task that runs the cleanup script periodically. To use:
1. Set the two commented values below according to the instructions.
2. Open Task scheduler and select "Import task...".
-->
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<Actions Context="Author">
<Exec>
<Command>"pythonw.exe"</Command><!-- Put the path to a Python 3 installation's pythonw here. -->
<Arguments>"clean.py"</Arguments><!-- Put the path to clean.py here. -->
</Exec>
</Actions>
<RegistrationInfo>
<Date>2020-03-01T16:47:23.0137887</Date>
<Author>Le Duy Quang</Author>
<Description>Cleans the temporary folder periodically.</Description>
<URI>\User-defined\Temporary folder cleanup</URI>
</RegistrationInfo>
<Triggers>
<TimeTrigger>
<Repetition>
<Interval>PT30M</Interval>
<StopAtDurationEnd>false</StopAtDurationEnd>
</Repetition>
<StartBoundary>2020-02-01T19:00:00</StartBoundary>
<Enabled>true</Enabled>
</TimeTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<GroupId>S-1-5-32-544</GroupId>
<RunLevel>LeastPrivilege</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>P1D</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
</Task>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment