Harshveer Singh
Harshveer Singh

Reputation: 4187

Command or Script to rename multiple files in windows

I have around 400 movies on my hard-disk, but names of those movies contains dots or underscore in between like "wrong_turn.mkv" or "wrong.turn.mkv". I just want to remove these dots or special characters from the file name and keep the extension as it as. Any Command Line command or Powershell/Python or any other script for windows? Thanks.

Upvotes: 1

Views: 1239

Answers (2)

Shay Levy
Shay Levy

Reputation: 126722

If the two files you have reside in the same directory then removing _ from the first file will work, but when you'll try to remove the dot from the second file it will fail cause it'll have the same name as the first one. Maybe that's an edge case, so here's the basic solution:

Get-ChildItem -Filter *.mkv | Rename-Item -NewName {($_.BaseName -replace '\.|_') + $_.Extension}

Upvotes: 3

OldFart
OldFart

Reputation: 2479

In PowerShell you can use Rename-Item with a script block that determines the new name.

dir -Recurse -Include *.mkv | Rename-Item -NewName { expression to determine new name }

The expression within the script block can use $_ to reference the current FileInfo object.

More concretely:

dir -Recurse -Include *.mkv | Rename-Item -NewName { $_.Name.Replace('_', ' ') }

Use Rename-Item -WhatIf until you get it right.

Upvotes: 2

Related Questions