Skip to content

Instantly share code, notes, and snippets.

@t3knoid
Last active October 9, 2018 14:49
Show Gist options
  • Save t3knoid/0de81522f270bde8983e3b2bb759e433 to your computer and use it in GitHub Desktop.
Save t3knoid/0de81522f270bde8983e3b2bb759e433 to your computer and use it in GitHub Desktop.
Python script that deletes old files
import os
import datetime
import win32wnet
from datetime import date
# Destination path
host = 'devfs02' # SMB server
share = 'K' # SMB share
username = 'tde' # SMB user
password = 'etech' # SMB user password
unc = ''.join(['\\\\', host]) # UNC path
win32wnet.WNetAddConnection2(0, None, unc, None, username, password) # Connect to destination share
today = date.today()
dir_to_search = unc + '\\' + share + '\\trace\\'
for dirpath, dirnames, filenames in os.walk(dir_to_search):
for file in filenames:
filename = os.path.join(dirpath, file)
file_modified = datetime.datetime.fromtimestamp(os.path.getmtime(filename))
# Delete files older than 6 months
if datetime.datetime.now() - file_modified > datetime.timedelta(weeks=26):
try:
os.remove(filename)
print "Deleted " + filename
except OSError:
pass
print "Cannot delete " + filename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment