Checking files on startup

A few of the files for which I'm responsible at work need to be updated regularly to ensure team members and clients always have access to the latest data. The problem is that each file has a different "expiration", and I'm quite a forgetful person. To solve this issue, I created a simple script to check the last time my files were edited. If a file is expired, I get an alert to update it. This script runs in the background every time I start my computer, so I don't notice it when I don't need it.

To do this, let's create a new python file. Rather than ending our file in .py, however, we'll use .pyw. This will ensure our script runs without a console window, meaning it will go unnoticed unless it finds an issue for us to address.

Next, we'll add our imports and global variables. We'll create a list of the file paths we want to check and, crucially, we'll establish the maximum age of the files. To keep things simple, we'll pretend all our files expire at the same time, at one month old, but you could easily imagine how to set up different expiry dates for different files.

# ----------------------------------------------------------------
# IMPORTS
# ----------------------------------------------------------------
import os
import time
import win32api
# ---------------------------------------------------------------------
# GLOBAL VARIABLES
# ---------------------------------------------------------------------
path1 = (fr"your\path\here")
path2 = (fr"your\path\here")
path3 = (fr"your\path\here")
target_file_paths = [
path1,
path2,
path3
]
current_time = int(time.time()) #Returns time in seconds since EPOCH (1970.1.1)
one_month_ago = current_time - 2592000 #Seconds in thirty days
Next, we'll need to set up the function that'll we'll use on each file in our target_file_paths list. In my case, I used the date the file was modified, but you could also use the creation date by replacing getmtime() with getctime().
def get_modified_date(path):
mdate = os.path.getmtime(path)
if mdate < one_month_ago:
outdated = True
else:
outdated = False
return outdated
Now all that's left to do is run the function on our files. If we find any expired files, we'll append them to a list. That way, we can finish checking all files and wait ten minutes (this is a personal choice - I don't like being bombarded with warning messages the second I turn on my computer) before sending any alerts.
outdated_files = []
for file_path in target_file_paths:
try:
outdated = get_modified_date(file_path)
except:
win32api.MessageBox(0,
fr"{file_path} not found.",
"ERROR",
0x00001000
)
if outdated:
outdated_files.append(file_path)
if outdated_files:
time.sleep(600) #Wait until I'm settled before giving me this warning.
for file in outdated_files:
win32api.MessageBox(0,
fr"{file_path} is over 30 days old.",
"Outdated Files",
0x00001000
)

Note: The next part of the tutorial applies specifically to Windows computers.

Once our code is written and checked, we can save it (or a linked version of it) to Shell:Startup. This will ensure the script runs every time we run our computer. That way, we can't forget to run it. The easiest way to do this is to hit Windows + R, then enter shell:startup. Then, we simply drag our desired .pyw file into the resulting window, and we're done!

Comments

Popular posts from this blog

Check which user owns an element in a shared model

Create dimensions

Create Dimensions - Rooms