1

i found out about Powershell renaming some years ago by trying to remove the "-eng" part at the end of .srt files for tv series. Found this command which has been working perfectly when i needed to rename dozens of files.

get-childitem *.srt | foreach {rename-item $_ $_.name.replace("-eng","")}

This is the only command i know of. I just know nothing about powershell nor coding at all. Now for example i have these files.

  • Sword Art Online II - 01 [BD][FS].mp4
  • Sword Art Online II - 02 [BD][FS].mp4

24 of them. The idea is to get this result

  • 02 - 01.mp4
  • 02 - 02.mp4

etc.

So i need to replace.

Sword Art Online II --> 02

and

[BD][FS] --> ""

Thought it wouldnt be that hard. But im getting erros. For example i tried

get-childitem *.mp4 | foreach {rename-item $_ $_.name.replace("Sword Art Online II ","")}

But i get this error

rename-item : Cannot rename because item at 'C:\users\santo\downloads\02\Sword Art Online II - 01 [BD][FS].mp4' does
not exist.
At line:1 char:32
+ ...  | foreach {rename-item $_ $_.name.replace("Sword Art Online II ","") ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand

So i tried with the last part but i get the exact same error

rename-item : Cannot rename because item at 'C:\users\santo\downloads\02\Sword Art Online II - 01 [BD][FS].mp4' does not exist.
At line:1 char:32
+ ... item *.mp4 | foreach {rename-item $_ $_.name.replace(" [BD][FS]","")}
+                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand

I believe i am in the correct directory cause i enter dir command and it lists all 24 files by using "cd " and "cd.." Using Windows Powershell (admin) Tried looking for the error line. What does this mean " PSInvalidOperationException " I just dont know what im getting into.

What am i doing wrong? Any thoughts?

marsze
  • 15,079
  • 5
  • 45
  • 61

2 Answers2

0

First of all, note that you don't necessarily have to be in the right directory. You can specify the path for Get-Childitem directly via the -Path (or -LiteralPath) parameter. (See the documentation.)

Also, Rename-Item expects a file name. If you pass $_, which is of type FileInfo, it will be implicitly converted to a string, resulting in only the file name, not the full path. This can lead to errors, if you're not in the right directory. You can use $_.FullName. Or even better, you can pipeline the results directly into the command.

Lastly, it looks like your -Filter is wrong. Shouldn't it be *.mp4?

Here's my updated version of the command. Let me know if it works or if you have any questions

Get-ChildItem -Path "C:\users\santo\downloads" -Filter *.mp4 |
    Rename-Item -NewName {$_.Name.Replace("Sword Art Online II", "02").Replace(" [BD][FS]", "")}

Note: Always include the -WhatIf switch first, to check what the command will do, before you actually run it. This will help you troubleshoot and prevent you from making unwanted changes.

(It would look like this: ... | Rename-Item -WhatIf -NewName ...)

If you need help with any command, you can always use -? (e.g. Rename-Item -?) or use the Get-Help cmdlet, (e.g. Get-Help Rename-Item or Get-Help Rename-Item -Examples)

marsze
  • 15,079
  • 5
  • 45
  • 61
  • 1
    It worked astonishingly perfect. Thank u very much for improving the command. Found some guides on this web. https://stackoverflow.com/questions/33559/how-to-get-started-with-powershell Do u think are good for a begginer? – Nickie Sanx Nov 04 '20 at 09:31
  • @NickieSanx Glad it helped. Feel free to upvote + accept this answer. Also see my edit about using `Get-Help`. If you want to learn powershell, you can google "powershell tutorial" or similar, and you will find lots of good articles, for example [this one](https://www.guru99.com/powershell-tutorial.html). Also, you can also come back here if you're stuck. – marsze Nov 04 '20 at 09:34
0

Don't use ForEach, directly pipe like this to Rename-Item:

dir "C:\users\santo\downloads" -filter *.mp4 | 
  ren -NewName {$_.Name.Replace('Sword Art Online II', '02').Replace(' [BD][FS]', '')}

If you still wanna use Foreach:

dir -filter *.mp4 | 
  % {ren $_.fullname -newname {$_.name.replace("Sword Art Online II ","").Replace(' [BD][FS]', '')}}
Wasif
  • 14,755
  • 3
  • 14
  • 34
  • Thank u very much sir. Worked very very good. Do u know any guides for begginers to understand how powershell works? – Nickie Sanx Nov 04 '20 at 09:36