Skip to content

Instantly share code, notes, and snippets.

@Kianda
Last active May 1, 2025 12:38
Show Gist options
  • Save Kianda/574a1392aad34f8afe729bbcb783c8b9 to your computer and use it in GitHub Desktop.
Save Kianda/574a1392aad34f8afe729bbcb783c8b9 to your computer and use it in GitHub Desktop.
PowerShell script to rename files by adding SxxExx to the beginning
# Create a shortcut to this file with 'Target'
# C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File "X:\Yourpath\season_renamer.ps1"
param(
[int]$seasonNumber = 0, # Default to 0 to indicate that no parameter was passed
[int]$episodeNumber = 0 # Default to 0 to indicate that no parameter was passed
)
# Prompt the user if no parameters are passed
if ($seasonNumber -eq 0) {
$seasonNumber = [int](Read-Host "Enter the season number (default is 1)")
if ($seasonNumber -eq 0) { $seasonNumber = 1 } # Default to 1 if the input is empty or zero
}
if ($episodeNumber -eq 0) {
$episodeNumber = [int](Read-Host "Enter the starting episode number (default is 1)")
if ($episodeNumber -eq 0) { $episodeNumber = 1 } # Default to 1 if the input is empty or zero
}
$folderPath = Get-Location # Uses the current directory where the script is located
# Define the list of video file extensions
$videoExtensions = @(".mp4", ".mkv", ".avi", ".mov", ".flv", ".webm")
# Get all video files in the folder, sorted by creation date (oldest first)
Get-ChildItem -Path $folderPath -File |
Where-Object { $videoExtensions -contains $_.Extension } |
Sort-Object CreationTime |
ForEach-Object {
# Format the season and episode numbers
$season = "{0:D2}" -f $seasonNumber
$episode = "{0:D2}" -f $episodeNumber
# Construct the new file name
$newName = "S${season}E${episode}_" + $_.Name
# Rename the file
Rename-Item -Path $_.FullName -NewName (Join-Path $folderPath $newName)
# Increment the episode number for the next file
$episodeNumber++
}
Write-Output "Video files renamed successfully!"
# Exit the PowerShell window
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment