I wrote a script that places documents to the folders they should be (based on my preferences of course). It even creates the folder if it doesn't exists.
Here's how it works:
- You run de script by clicking on it. (idk if I should let this run in the background constantly, I don't use it that much)
- Checks the selected directory for files with a "-" in their name. (Physics- Homework.docx)
- It checks the targeted directory (where my folder hierarchy is meant to be) if there's a folder named after the file's name before the "-" part. ("Physics- Homework.docx" goes to a folder named "Physics")
- If there isn't a folder, it creates one. If there is a folder, it just moves the file inside
So Like I said in the first step, you have to click to run this, I would love to make it automatic, but I don't want it to run in the background just to move a file twice in a week or so. I would love to know if there's another cool way to achieve this automatic way of working.
My idea (to achieve what I said above) was to create a ".bat" file that runs Word and the script at the same time (this time script keeps running in the background once started), next to script's based functionality, it would also check for the apps running in the background and when Word is closed, it performs the file movement and closes itself. But I don't know if this is a great idea or if it can be done in much simpler way.
I want to know what I could've done better. Since the code is relatively short, I feel like I have done a great job, but I believe that there's always room for improvement.
Here's the code:
import shutil import os Dir = "D:/OneDrive/Documents/" # Where are the files? Add "/" at the end of your directory. TragetDir = "D:/OneDrive/Documents/school/6a9/" # Where are the target folders? Add "/" at the end of your directory. FileNames = glob.glob(Dir + "*-*") # scans the dir with the selected character (*[selected character]*), searches directly for the name now. StartIndex = len(Dir) # The chars of the Dir are useless for x in range(len(FileNames)): # Each file gets moved one by one Index = FileNames[x].find("-") # I always create my files using: "class + "-" + topic" way SFolder = FileNames[x][StartIndex:Index] # My folders are always named after the subject, so "Math- exam 1.docx" should be placed in a folder named "Math" try: #tries to make a target dir os.makedirs(TragetDir + SFolder) except: #if the directory already exists just skips this process pass shutil.move(FileNames[x], TragetDir + SFolder) # moves to the target folder