WorldTeacher
WorldTeacher

Reputation: 103

Auto rename files with sequential numbers and basename in powershell

I have some video files that I need to rename. the name is something like this: [video name] [number].[file-extension] I have recently switched my media play software that requires a special naming order. The Order is as follows: [video name] e(increment start at 01)].[file-extension] additionally, the media player requires the folder structure like this: C:\Media\[series]\[season(increment start at 01)] I can do the folder structure manually, and renaming the files manually would be a possibility too, but I'd like to automate the process to save some time.

The best way to create the filename would be like this: check path to file like this:

$path = Get-Location
get-childitem "$path" -recurse | where {$_.extension -eq ".mkv"} 

and detect the path before \season[number]. Ideally, the script would then remane the file like this: [video name = path(before season)] and then add e(increment start at 01)] based on a script like this:

$i = 1 Get-ChildItem *.mkv| %{Rename-Item $_ -NewName ('$_.Fullname{0:D4}.mkv' -f $i++)} as seen here:

Bulk renaming of files with powershell however the media player will get confused if the series has 12 episodes and the filename is like this: s01e001

If it is not possible to do the part of getting the name based on the path, I'd like to have a script that renames the file to [series name] e[increment start from 01].mkv

Are there any ways to create a script to rename the files?

Upvotes: 0

Views: 2066

Answers (1)

jasocoder
jasocoder

Reputation: 1

Here Is a script that I use for that:

Write-Output "Press Ctrl+c to Abort"
Write-Output ""
Write-Output "This Script will rename all files of a specified file type with a zero-padded sequential "
Write-Output "number like 01.jpg. An optional basename ending before file type can be added like 01-Jake.jpg"
Write-Output "Enter an integer not including the zero-padded numbers to start the sequential numbering."
[int]$startInt = Read-Host "Example 0 or 1 or 10... etc."

$file_type = Read-Host 'Enter the file type that you would like to target. Example: .jpg .png .m4v .mp4 .pdf'
# $fileNameEnding = Read-Host 'Add a file basename ending before its file type extension.'
$fileNameEnding = Read-Host 'Add an optional basename ending before file type extension. Enter it now or press enter to skip'

Get-ChildItem *$file_type | ForEach-Object{Rename-Item $_ -NewName ("{0:D2}$fileNameEnding$file_type" -f $startInt++)}

Where {0:D2} is used, that is the number of zeros padded. So {0:D4} would padd to 4 zeros.

You can run this script to see how the different parts of the file name can be accessed.

$filePath = Read-Host 'Enter the file name and path to see its parts'
Write-Output ""
Write-Output "Extension: $((Get-Item $filePath ).Extension)"
Write-Output "Basename: $((Get-Item $filePath ).Basename)"
Write-Output "Name: $((Get-Item $filePath ).Name)"
Write-Output "DirectoryName: $((Get-Item $filePath ).DirectoryName)"
Write-Output "FullName: $((Get-Item $filePath ).FullName)"
Write-Output ""
Write-Output "Mode: $((Get-Item $filePath ).Mode)"

Upvotes: 0

Related Questions