Bulk File Rename PowerShell Script

May 01, 2023

Code Baselined: May 01, 2023

Overview

Photographers frequently find themselves in a situation where they need to rename large numbers of photos that were imported from their camera(s) which use a default file naming convention such as DSCnnnnn.jpg, where nnnnn is a numeric value. These numeric values are usually incremented by 1 in the camera in the order the photos were taken. Files named using this type of convention present problems if the photographer wishes to re-sort the photos based on file name since there is no easy way to insert photos in the sequence without having to manually rename the inserted files and all files which appear higher in the sort order.

This problem is exacerbated if the photographer wishes to post their photos on OneDrive since the ability to specify custom sorting is no longer supported.

The solution presented herein is a [somewhat] simple script written in Windows PowerShell. For those readers not familiar with PowerShell, the following description was found on the Web:

It is an object-oriented scripting language and shell, and nearly everything in PowerShell is an object. It is designed mainly for IT professionals and system administrators to control and automate the administration of Windows OS and other applications.

Web Search

“PowerShell provides both the power and ease of use that have been missing from the Windows platform since the beginning of time.” writes author Ed Wilson in his book Microsoft Windows PowerShell: Step by Step. PowerShell can be viewed as a replacement for the old DOS Command Prompt but with additional capabilities that support Windows network and system administrators.

Disclaimer

The PowerShell script presented in this document is for informational purposes only. The author will not be liable for any data loss or system corruption resulting from the use of this script. Windows power users attempting to run the script should take the necessary precaution of backing up their documents (photos) prior to running the script. The author also advises the power user to first test the script using a small data sample in a separate folder prior to running it on important data files.

Sample Naming Convention

I’ve adopted the following naming convention for my own travel photos:

South Korea          => yyyy-SK00000
Italy                          => yyyy-ITA00000
United Kingdom => yyyy-UK00000 
Western Europe  => yyyy-WEU00000
Mexico                    => yyyy-MEX00000
Canada                    => yyyy-CAN00000
USA:                         => yyyy-{State}00000
                                   => yyyy-{Region}00000  e.g., 2022-DELRVTWNS00010
                                   => yyyy-{City}00000         e.g., 2023-SHILLS00040
Cuises:                    => yyyy-{Ship}00000        e.g., 2023-APEX00030

Code

################################################################################
#   Bulk file rename for imported camera JPG files.                            #
#                                                                              #
#      .\FileRename.ps1 -FilePath   {file directory}                           #
#                       -FilePrefix {file name prefix} e.g., 2019-ITA          #
#                       -Increment  {file name increment} e.g., 10             #
################################################################################

[CmdletBinding()]
param (
    [Parameter(Mandatory=$true)] [string] $FilePath,
    [Parameter(Mandatory=$true)] [string] $FilePrefix,
    [Parameter(Mandatory=$true)] [int] $Increment
)

$newCount = 0
$numFiles = 0

$fileNames = Get-ChildItem $FilePath -Filter *.jpg
foreach ($jpgFile in $fileNames){
    $oldJPG = [string]$jpgFile.Name
    $fileExt = [string]$jpgFile.Extension
    $newCount += $Increment
    $newJPG = $FilePrefix + $newCount.ToString("00000") + $fileExt
    Rename-Item ($FilePath + "\" + $oldJPG) $newJPG
    $numFiles++
}

Write-Output "Renamed -$numFiles- files in directory: $FilePath"

Running the Script

Before you can run the script, you will first need to copy the above code and paste it into an editor (Notepad++ for example) and then Save to a file with the extension .ps1 on your hard drive.

  1. Invoke the PowerShell application
  2. From the PS prompt, CD to the folder where you saved your .ps1 file
  3. Run your script using the following command: .\{your file}.ps1
  4. Respond to the prompts for the FilePath, FilePrefix, and Increment

Sample Command Line Output

PS C:\Users\uname\OneDrive\Documents\PowerShell> .\FileRename.ps1

cmdlet FileRename.ps1 at command pipeline position 1
Supply values for the following parameters:
FilePath: c:\users\uname\onedrive\documents\psfolder\jpg
FilePrefix: GOGO
Increment: 20
Renamed -3- files in directory: c:\users\uname\onedrive\documents\psfolder\jpg

Notes

The script was developed using Visual Studio Code.

The script will work using PowerShell versions 5 and 7.

The script will only rename files with the .jpg extension.

The script will not recurse through sub-directories.

When prompted for the FilePath, do not include a trailing backslash ( \ ).

The script is not idiotproof, that is, it does not perform any input validation; it will try to do whatever it is asked to do.

©2013-2024 Gerard Sczepura. All rights reserved.