Replace filenames

I download a file.
Then I want to change the filename to filename without dots

  • .S00E00.mkv,.avi or .mp4.
    The filename is everything before .S00
    Everything after E00 I do not want except extention.
    Send this file to an external HD with folder name = filename and subfolder with S00 name
    Is this possible?

ingewll,
Welcome to the forum. :wave:t4:

Without actually getting what the filename really looks like and what it should be in the end I’d say - Yes, it is possible.

But why would you like to do it for one file with PowerShell? And if you really want to do it in PowerShell - what have you tried so far? Are you aware that there are tools available for file rename jobs like PowerToys PowerRename?

Please keep in mind this forum is for scripting questions rather than script requests. We do not write customized and ready to use scripts or solutions on request.

We expect you to make an own attempt to get your task done or to solve your problem. If you have done so already please document here what exactly you have done and show your code. Then we probably might be able to help you step further.

Yes I have tried PowerRename and like it!
This is how far I have gone
$files = Get-ChildItem -Path /Users/curt-ingehenning/Downloads/_KLARA
foreach ($file in $files)
{
$newFileName=$file.Name.Replace("."," ")
Rename-Item $file $newFileName
}

OK, I assume it is about more than one file, isn’t it? :wink:

Before we proceed: When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

Regardless of that could you please post a few filenames as they are before and how they should look in the end?

Thank you.

Yes it is a few.

Examples:
Americas.Funniest.Home Videos S32E09 720p HEVC x265-MeGusta.avi = Americas Funniest Home Videos.S32E09.avi
Superman.and.Lois.S02E13.HDTV.x264-TORRENTGALAXY[TGx].mkv = Superman and Lois.S02E13.mkv
Star.Trek.Strange.New.Worlds S01E06 1080p WEB H264-CAKES.mp4 = Star Trek Strange New Worlds.S01E06.mp4
Alone.S09E02 720p WEB-DL AAC2 0 h264-LBR.mkv = Alone.S09E02.mkv

Again … please … when you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

And … I assume that are the filenames before, right? How should they look like after the renaming?

Americas.Funniest.Home Videos S32E09 720p HEVC x265-MeGusta.avi = Americas Funniest Home Videos.S32E09.avi
Superman.and.Lois.S02E13.HDTV.x264-TORRENTGALAXY[TGx].mkv = Superman and Lois.S02E13.mkv
Star.Trek.Strange.New.Worlds S01E06 1080p WEB H264-CAKES.mp4 = Star Trek Strange New Worlds.S01E06.mp4
Alone.S09E02 720p WEB-DL AAC2 0 h264-LBR.mkv = Alone.S09E02.mkv 			

First original and after = how I want it.

For the samples you shared this should work:

$fileList = Get-ChildItem -Path /Users/curt-ingehenning/Downloads/_KLARA
foreach ($file in $fileList) {
    $newFileName = ($file.BaseName -replace '\.', ' ' -replace '\s*(S\d{1,2}E\d{1,2}).*', '$1') + $file.Extension
    Rename-Item -Path $file.FullName -NewName $newFileName
}

Maybe something like this ?

$files = get-childitem 'b:\PS_test\*'
    Foreach ($filename in $files){
    
    $SE = (($filename.name | select-string -pattern 'S(\d{1,2})E(\d{1,2})').Matches[0].Groups[0].value).ToString()
    
    $SeriesName = (($filename.name -split("$SE"))[0]).replace('.',' ').trim()
    
    $NewFileName = $SeriesName +'.' + $SE + $filename.Extension
    
    $SeriesDir = $($filename.Directory.fullName + "\$SeriesName\"  + "$($SE.split('E')[0])\")
    
        If(!(Test-Path $SeriesDir)){

        New-item -Path $SeriesDir -ItemType Directory

        }

        Move-Item -Path $filename -Destination "$SeriesDir$NewFileName" -force
        
}

GOOD!
It didn’t handle the last file right

Superman.and.Lois.S02E13.HDTV.x264-TORRENTGALAXY[TGx].mkv

A new directory and subdirectory was created but the file was untouched original! Maybe the brackets in the filename caused it. Can be strange letters in original filename like ( ) , .

Can I change the destination to an external HD like G:/_TEST ?

So you used DejvDzi’s solution. :man_shrugging:t4: Great … so I can go back to sleep. :sleeping:

Yes I did. Thanks for your effort

You can change destination to G:/_TEST, if you need support-- write, I’ll be available tonight.

I tried this but no

Move-Item -Path $filename -Destination 'G:\_TEST\*' "$SeriesDir$NewFileName" -force

I tried this but no

$SeriesName = (($filename.name -split("$SE"))[0]).replace('.',' ').replace('[',' ').replace(']',' ').trim()

Hi, try this:

$files = get-childitem 'G:\_test\*'
    Foreach ($filename in $files){
    
    $SE = (($filename.name | select-string -pattern 'S(\d{1,2})E(\d{1,2})').Matches[0].Groups[0].value).ToString()
    
    $SeriesName = (($filename.name -split("$SE"))[0]).replace('.',' ').trim()
    
    $NewFileName = $SeriesName +'.' + $SE + $filename.Extension
    
    $SeriesDir = $('G:\_test\' + "$SeriesName\"  + "$($SE.split('E')[0])\")
   
        If(!(Test-Path $SeriesDir)){

        New-item -Path $SeriesDir -ItemType Directory

        }

   Move-Item -Path $([WildcardPattern]::Escape($filename.fullname)) -Destination "$SeriesDir$NewFileName" -force
        
}

Easy fix. Change parameter ‘-Path’ to ‘-LiteralPath’ in:

$files = get-childitem ‘b:\PS_test*’

PERFECT!! 100%
Thank you very much !!
Inge

Continues trying to compile this ps1 file to an exe.file and get this error message.
Spelling is correct.
Both Invoke-ps2exe and ReplaceTEST.ps1 is on desktop.

Invoke-ps2exe : The term 'Invoke-ps2exe' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and
try again.
At line:1 char:1
+ Invoke-ps2exe -inputFile 'C:\Users\ingew\Desktop\ReplaceTPB.ps1' -out ...
+ ~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Invoke-ps2exe:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundExceptiontype or paste code here

Don’t understand what is wrong or missing