4
\$\begingroup\$

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:

  1. 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)
  2. Checks the selected directory for files with a "-" in their name. (Physics- Homework.docx)
  3. 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")
  4. 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
\$\endgroup\$
1
\$\begingroup\$

A few changes I would make:

import pathlib

docs_dir = pathlib.Path('')
target_dir = pathlib.Path('')

for file in docs_dir.glob('*-*'):  # Each file gets moved one by one
    subject_name, file_name = str(file.name).split('-', maxsplit=1)
    subject_dir = target_dir.joinpath(subject_name)
    
    subject_dir.mkdir(exist_ok=True)
    file.rename(subject_dir.joinpath(file_name))

As an aside, it may be worth considering using a folder just for unsorted school-related files. It would be so easy to add a non-schoolwork file which contains a '-' to your documents folder, and have it moved unintentionally by the program.

You wrote in a comment:

However I aim for that "I saved the file and it directly moved to the targeted folder" approach.

If that's the case, you should look into file system watchers/watchdogs.

\$\endgroup\$
0
\$\begingroup\$
  • For automation, I think you can look for Task Scheduler in windows (as you mention *.bat, I imagine cron isn't an option.

  • And just add a job daily (or weekly, etc.) to run your script and forget about that.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks for sharing this with me, I didn't know about the the scheduled tasks on Windows (at least that it was open for users). This is quiete cool actually to know. However I aim for that "I saved the file and it directly moved to the targeted folder" approach. And there's no option in it to start a program, when another one is closed. \$\endgroup\$
    – user182461
    Nov 10 '20 at 20:34
  • \$\begingroup\$ in this case the option to use a bat script with after word is closed seem better (i don't think you want to reverse engeenering word to detour it) \$\endgroup\$
    – jolrael
    Nov 10 '20 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy