1

I was trying to remove all the underscores, spaces and square brackets from many animation files name, which made a big mess.

I used the code below to replace/remove the characters with what I want.

Replace

Get-ChildItem -Filter "*One Piece_*" -Recurse |
  Rename-Item -NewName {$_.Name -replace 'One Piece_','One Piece '}

Or (to delete space)

Get-ChildItem -Filter "* *" -Recurse |
  Rename-Item -NewName {$_.Name -replace ' ',''}

Now all the file names look like this:

One Piece 031EEE7A115 → should be One Piece 031 EEE7A115
One Piece 032FFB90605 → should be One Piece 032 FFB90605
etc.

Is there a way to insert space to all the files after the episode number?

Ansgar Wiechers
  • 175,025
  • 22
  • 204
  • 278
Zezo
  • 13
  • 5

1 Answers1

2

Sure. Make a regular expression that matches the substring from the beginning of the name up to three consecutive digits, and replace that with itself and a space:

Get-ChildItem -Filter '*One Piece_*' -Recurse |
  Rename-Item -NewName {$_.Name -replace '^(.*?\d{3})', '$1 '}
Ansgar Wiechers
  • 175,025
  • 22
  • 204
  • 278
  • Can you please explain the code '^(.*?\d{3})', '$1 ' – Zezo Jan 02 '16 at 14:34
  • @Zezo It means match everything up until the first 3 numbers encountered. http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean – Matt Jan 02 '16 at 16:18
  • The ^ means to start matching at the beginning of the string. Also [see this program](http://www.joejoesoft.com/vcms/108/) which allows for file renaming with preview. – user4317867 Jan 02 '16 at 21:14