Moving Files & Folders - Files into Folders

Overview

This example shows how to move particular files into folders that have matching Employee Numbers, using the Python library, shutil.

As with all Scripts, the specifics shown below can be adapted to meet your particular needs.

Python Script

The following shows the full script

Python
import os, glob, shutil
from pathlib import Path

# Specify Input and Output Locations
working_directory = Path.cwd()
source_folder = Path('Payslips')
destination_folder = Path('Folders')
source_folder_path = working_directory / source_folder
destination_folder_path = working_directory / destination_folder

# find all the destination folders (going down a depth of 2 child directories)
folderDepth_1 = filter(lambda f: os.path.isdir(f), destination_folder_path.glob('*/'))
folderDepth_2 = filter(lambda f: os.path.isdir(f), destination_folder_path.glob('*/*/'))
folders = list(folderDepth_1) + list(folderDepth_2)

# find all the source files
source_files = list(source_folder_path.glob('*.pdf'))

# Do nothing if there are no destination folders or source files
if len(folders) == 0 or len(source_files) == 0:
    print("No destination folders, or no source files found")

# Otherwise move files into folders
else:
    for file in source_files:
        filename = file.name
        # Extract the Employee number as the text between the paranthesis - i.e. ( )
        empNo = filename[filename.find("(")+1:filename.rfind(")")]

        # Check that Employee number does not equal to EMPTY
        if empNo != 'EMPTY':

            # Otherwise move matching file into matching folder
            for folder in folders:
                if str(folder).find(empNo) != -1:
                    shutil.move(str(file), str(folder / os.path.basename(file)))
                    print('File moved successfully!')
                    break
        
# Print final message
print('All files moved') 

How to Use it

  • The source folder is called Payslips. This has a list of all the source files

  • The parent destination folder is called Folders. This has, within it, several destination folders (where the source files need to be moved to). All child directories going down 2 levels are searched.

  • The script can be given any meaningful name but should have a file extension .py to tell the operating system it is a Python Script

  • The (i) source folder, (ii) parent destination folder, and (iii) Python Script should all sit along side each other within the same working directory

  • The script is super fast! It can move 1000+ files in less than 5 seconds

How it Works

The script works by:

  • Extracting all sub-folders (level 1 and level 2 child directories) from within the parent destination folder
  • Extracting all source files from the source folder (that have the file extension .pdf)
  • Checking that at least some destination folders in the parent folder, and some source files in the source folder actually exist (i.e. that neither are nil)
  • Extracting the Employee number from the filename of each source file
  • Matching this Employee number (provided one exists) with the first folder, whose folder name contains the same Employee number
  • Then, once there is a match, moving the file into the relevant folder
  • A final message is then printed

And Voila!