Last active
October 9, 2018 14:49
-
-
Save t3knoid/0de81522f270bde8983e3b2bb759e433 to your computer and use it in GitHub Desktop.
Python script that deletes old files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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