Skip to content

Instantly share code, notes, and snippets.

@iisti
Last active May 31, 2019 00:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iisti/5ebbb47ff4af54cc0a43afbc7951a044 to your computer and use it in GitHub Desktop.
Save iisti/5ebbb47ff4af54cc0a43afbc7951a044 to your computer and use it in GitHub Desktop.
A PowerShell script for renaming multiple mp3 files to syntax: number_filename.mp3
param([string]$filename,[string]$path)
Write-Host "This script has been written to solve following issue:
A person is listening multiple hour mp3 audiobook file with mobile
phone before going to sleep. If the person falls a sleep and the
mp3 file keeps playing, the next day it's really hard to find the
spot that the listener still remebers as skipping precisely multiple
hour mp3 file is impossible with mobile phone screen.
Solution:
1. Split the mp3 file to smaller files.
-One has to use external software to split the mp3 file.
-The multiple hour file was split to 900 seconds (15 minutes) files.
-Splitting the mp3 file can lead to situation that the file names are
so long that it's impossible to recognize which file is which on the
mobile phone screen.
2. Rename the splitted files to short recognizeable names.
This script will rename files with file extension .mp3 in the provided folder.
The filename syntax will be number_filename.mp3
Numbering will be from 001 to n (n = number of files renamed).
You can run the script from PowerShell commandline with command:
script.ps1 filename `"path`"
If the parameters are not provided when script is run,
the script will ask for the filename and path.
"
if (!$filename) {
Write-Host "No filename provided as a parameter."
$filename = Read-Host "Give filename"
}else {
Write-Host "Filename was given as a parameter:"
Write-Host " $filename"
}
if (!$path) {
Write-Host "No path provided as a parameter."
$path = Read-Host "Give path to mp3 files without quotes `"`""
}else {
Write-Host "Path was given as a parameter:"
Write-Host " $path"
}
Write-Host "Filenames before renaming:"
Get-ChildItem -Path "$path\" -Include *.mp3 -File -Recurse | %{Write-Host " $_"}
$yesno = Read-Host "Files above were found. Are you sure you want to rename them [y/n] ?"
if ($yesno -ne 'y') {
Write-Host "y was not selected. Terminating the script."
exit
}
# Rename the files
# https://stackoverflow.com/questions/18304025/bulk-renaming-of-files-in-powershell-with-sequential-numeric-suffixes
# https://docs.microsoft.com/en-us/dotnet/standard/base-types/composite-formatting
$i = 1; Get-ChildItem -Path "$path\" -Include *.mp3 -File -Recurse | %{
Rename-Item $_ -NewName ("{0:D3}_{1}.mp3" -f $i++, $filename )
}
Write-Host "Filenames after renaming:"
Get-ChildItem -Path "$path\" -Include *.mp3 -File -Recurse | %{Write-Host " $_"}
Read-Host "Renaming ready. Press Enter to exit"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment