Automating file organization via PowerShell

My process in sharing and archiving event media (aka the numerous pictures and videos that I take in various formal and informal events) is to, first, download the file from the memory card and sort them out according to the date and type of event. This is a tedious task especially if a batch consists of several days of events, or worse, several weeks. You can imagine that I would copy the folder from the memory card to my personal computer and that folder would contain all the raw files of the pictures that I took — they are not organized in folders initially.

File Sorter Script via PowerShell
Close-up picture of Windows Explorer showing folder names.

The next step would be to categorize them in folders according to the date of the event they were taken. For the folder names, I’m using the ISO representation of date format, for example the date 2 October 2023 would be 2023-10-02. In this way, it’s easy to sort the folders chronologically; and even without the day or month, sorting still makes sense. Of course, I have done this manually for a very long time, until I’ve thought of using a batch file in creating all the dates of the year. The batch file approach helped a lot because it setup the folders with dates for all of 2023. With this approach, I simply check the files’ dates and drag them to their respective folders that were created by the batch file.

I still felt that it could be better — my current process was tedious than it’s supposed to be. So I searched for a batch file program that will automatically distribute the files into folders that have their respective dates. Where I arrived in my shallow search is at PowerShell.

The PowerShell script

# file_sorter.ps1
# Change $source_path accordingly to the folder whose files are to be sorted out
# Change $file_type to target specific file types
# To run this script, run PowerShell as an administrator
# Set-ExecutionPolicy Unrestricted
# Source of this script: https://stackoverflow.com/a/61053215

$source_path = "C:\Users\Dys\Pictures\Moments\_File_Sorter\"
$file_type = "*.*"
$source_path_file_type = "$($source_path)$($file_type)"
$files = Get-ChildItem $source_path_file_type

foreach ($file_item in $files){

    $x = $file_item.LastWriteTime.ToShortDateString()
    $new_folder = Get-Date $x -Format yyyy-MM-dd
    $des_path = "$($source_path)$($new_folder)"

    if (Test-Path $des_path){
        if (Test-Path "$($des_path)\$($file_item.Name)"){
            $index = 1
            do {
                $new_name = $des_path + "\" + $file_item.BaseName + " ($($index))" + $file_item.Extension
                $index++
            } while (Test-Path $new_name)
            move-item $file_item.fullname -destination $new_name
        } else {
            move-item $file_item.fullname $des_path
        }
    } else {
        new-item -ItemType directory -Path $des_path
        move-item $file_item.fullname $des_path 
    }
}

Steps in using the file sorter script

Step 1: prepare the .ps1 file

  1. Open Windows PowerShell ISE
  2. Copy and paste that script into a new .ps1 file in Windows PowerShell ISE
  3. Save as file_sorter.ps1

Step 2: prepare the file sorter folder

  1. Create a folder in Windows Explorer wherein you will always put files to be sorted (in my case, I have a folder named _File_Sorter in my My Pictures folder)
  2. Put the file_sorter.ps1 file on the same level of that folder (although it won’t matter where the .ps1 file is located because the path in the script is absolute)

Step 3: allow execution of PowerShell scripts

  1. Run Windows PowerShell as an administrator
  2. Run this command in the PowerShell command line: Set-ExecutionPolicy Unrestricted

Step 4: test the script

  1. Put some files inside _File_Sorter folder like .jpg and / or .mp4
  2. Run file_sorter.ps1 by right-clicking it and choose Run with PowerShell
  3. PowerShell will briefly open and automatically close after running the script
  4. Check the _File_Sorter folder if, indeed, the script had categorized the files into different folders according to their last modified date
  5. Move the folders out of _File_Sorter folder and into their proper directories so that this folder is ready for the next batch of file sorting

That’s it!

References


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *