Automating File Creation To Keep My Backup Solution Updated

by Luke Willey


Posted on 12/17/2021

I have a lot of data. Too much data, even. But I’m not downsizing any time soon, and cloud storage is expensive. So I use an online backup service to keep an offsite copy of my files in the event of losing a drive.

It’s easy and cost effective, but it has a quirk. I’m not sure why, but the provider will automatically assume a drive is inactive and stop keeping it stored if it hasn’t had any file changes in the last 30 days.

For a drive that consists of what is essentially just an archive, that means I have to manually make some sort of file change once a month so the backup stays current. Usually, that just means I make a text file and then the backup detects the change and marks the drive as active. The whole process takes about 2 minutes, but the hard part is the “remembering to do it” bit.

After about 6 months of this, I got dissatisfied enough to apply critical thinking and realize I could automate this process. So with about 40 minutes of work, I don’t have to do this anymore. You can find my source code here.

Essentially, all I’m doing is providing a list of what drives I want to create a file in, then creating a text file in each one. I then use the built in Windows Task Scheduler to automate this script being run every week.

Here’s the code to do it:

from datetime import date

drivepaths = {
   "Movies": "I:\\",
   "Shows": "H:\\",
   "Family": "E:\\",
   "Overflow": "D:\\"
}


def make_garbage(string_path):
   with open(string_path+'/dummy.txt', 'w') as file:
       file.write('Created new dummy file on ' + str(date.today()))
   return file


if __name__ == '__main__':
   for i in drivepaths.values():
       make_garbage(i)

It’s obviously short and simple. Here’s a quick overview of what’s happening: Create a dictionary of the drives we want to create files in. I made my keys indicative of the drive’s content for my sake, but these can be literally whatever. Then the values are the paths of the drive’s directory where I want the file to be created. Create a function called make_garbage() that takes a string called string_path, creates a file called dummy.txt at that path, and writes a quick message indicating the date of the file’s creation. Yes, Windows has built-in file creation information, but this way I don’t have to deal with the OS being fickle. Iterate over the values of our dictionary of drives and their locations and make_garbage() in each of them.

Simple. Ugly. Effective. Just the way I like it.

Once that’s in place, all that’s left is to automate it. Thankfully, Windows has a built in tool called Task Scheduler we can use for this component. It’s easy, I promise. First, you’ll need a .bat file. This is essentially just a prepared list of instructions to give Windows to execute. To make one, just open notepad, type in your instructions, and then save it as a .bat file instead of .txt. Make sure to note where you save it - you’ll need that path later!

For this implementation, it’s super simple. We’ll tell it where our Python installation is (if you don’t know, just type “where python” in command prompt - love it). Then we’ll point it to the location of our new python script. I didn’t care enough to name it creatively, so for me, we end up with this:

C:\Users\lukee\AppData\Local\Microsoft\WindowsApps\python.exe "C:\Users\lukee\PycharmProjects\pythonProject\main.py"

With that saved, open up Task Scheduler - you can find it by just searching for it in Windows. Once you’re in, click on Create Task, then name the task. Ideally something helpful, like “Automated File Creator for Backup Refresh” or “Task1.” Make sure you configure it for whichever version of Windows you’re on (Vista, really?!)

Next, go to the Triggers tab, click new, and make your schedule.

Then click the Actions tab, click new, and choose the “Start a program” action in the dropdown. In the Program/script box, just paste in the full path of your .bat file. Click OK, OK again, and you’re done!

Just wait for your scheduled time to arrive to verify it’s working, then spend some time reflecting on how to spend the 2 minutes per month that this automation will save you! You’ll have so much time for...reevaluating your life. Sorry for any lack of clarity in the above - full disclosure, because I’m a goofball, I had to re-write this post 3 times due to technical hiccups of my own doing, so by the time of posting this I’m fueled by pure self-loathing. Feel free to shoot me an email if you have any questions!

Links