0

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?

WorldTeacher
  • 93
  • 1
  • 10
  • Please show us some **real filenames** to work with. Now it is totally unclear what `[video name]` contains and how to parse `[series name]` from that. – Theo Jan 23 '21 at 09:24
  • a real filename would be like this: dr. who 01.mkv which I would like to turn into dr. who s01e01.mkv. The file would be in the folder Dr. who\Season 01 so the script should parse the series name based on the folder above \Season 01 – WorldTeacher Jan 24 '21 at 10:04

1 Answers1

0

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)"
jasocoder
  • 1
  • 1
  • 1
  • Thanks for the reply. I tested you script, but for some reason it doesn't work for me. It keeps saying the path to the media can't be found, even though I changed it to a $path = Get-Location and it never asked me to input the extension etc. I found a software that appears to do what I want I just need to test it. Thanks anyways – WorldTeacher Jan 23 '21 at 13:20
  • You should have gotten a question or two from the beginning of the script. Check your Execution Policy with Get-ExecutionPolicy. If it is set to restricted you will need to change it. See this [Video](https://www.google.com/search?q=set-executionpolicy+for+learning+powershell+scripting&oq=set+ex&aqs=chrome.1.69i57j35i39j0i457j0i395l4j69i61.5402j1j7&sourceid=chrome&ie=UTF-8). Also the script needs to be saved in with the files that you want to rename. When you run reference its path as .\ or in the folder above it, reference its path as ..\ then cd to the scripts location, type: ./script.ps1 – jasocoder Jan 24 '21 at 15:27