How do I rename a dir full of files with powershell?

Shadowspawn

[H]ard|Gawd
Joined
Sep 17, 2002
Messages
1,870
Windows 7 Powershell.

I have a dir of files that look like this:

Code:
[DBNL]Dragonball_Z_-_001_-_The_new_Threat_[DR][x264][F26CC67E]

I want to make them look like this:

Code:
Dragonball_Z_-_001_-_The_new_Threat_[DR][x264][F26CC67E]

I want to do more to their names than that, but if I can do that much then I can figure out the rest.

I have tried this:

Code:
dir | rename-item -newname { $_.name -replace "[DBNL]","" }

I receive an error per file saying it doesn't exist.

I think it is the square brackets but I am not sure how to resolve that problem.

Thanks in advance.
 

ezno_matrix

Limp Gawd
Joined
Feb 9, 2012
Messages
134
Fire up a linux distro on your network or in a VM and share the directory you want to change the files. On linux you could:
Code:
rename '[DBNL]' '' "[DBNL]*"
This will remove the first occurrence of [DBNL] from each file.

A more flexible tool is mmv. You could remove all the crap in the filename with:
Code:
mmv '\[DBNL\]Dragonball_Z_-_???_-_*_\[DR\]\[x264\]\[*\]' 'Dragonball_Z_-_#1#2#3_-_#4'

This will produce Dragonball_Z_-_001_-_The_new_Threat. Don't forget to include the extension of the file if there is one.
 

Dogs

[H]ard|Gawd
Joined
Aug 7, 2012
Messages
1,141
Fire up a linux distro on your network or in a VM and share the directory you want to change the files. On linux you could:
Code:
rename '[DBNL]' '' "[DBNL]*"
This will remove the first occurrence of [DBNL] from each file.

A more flexible tool is mmv. You could remove all the crap in the filename with:
Code:
mmv '\[DBNL\]Dragonball_Z_-_???_-_*_\[DR\]\[x264\]\[*\]' 'Dragonball_Z_-_#1#2#3_-_#4'

This will produce Dragonball_Z_-_001_-_The_new_Threat. Don't forget to include the extension of the file if there is one.

Sorry, but throwing up a linux VM just to do some batch processing seems rather clumsy and impractical, if not downright insane.

If he's willing to do completely inelegant solutions to get this to work, he's much better off just mixing legacy batch with powershell. He could make a batch file that contains only "ren %1 %2" and then call that batch file from powershell with the first argument being the old filename and the second argument being the new file name. I'm not even convinced that setting up the VM so that it could complete the task would take less time than just editing every file by hand.
 

ezno_matrix

Limp Gawd
Joined
Feb 9, 2012
Messages
134
This recommendation is more from a repeat usage perspective. Another option for the OP would be to install cygwin and use the same commands. However, I was trying to avoid mocking around with his current OS.
 
Last edited:

heatlesssun

Extremely [H]
Joined
Nov 5, 2005
Messages
44,154
Code:
Function Rename-FileExtension($path)
{
 Get-ChildItem $path | 
 Foreach-Object { 
	  $baseName = $_.name.Replace("[DBNL]","")
 Rename-Item -literalPath $_.fullname -newname ($baseName) 
 }
} #end function

Rename-FileExtension -path "C:\Temp"

I tested this with the example you gave and it worked perfectly. It simply removes the substring "[DBNL]" from each file name assuming of course that the file name has that in its name, otherwise it does nothing. Your example would work as well like this:

Code:
dir | rename-item -literalPath -newname { $_.name -replace "[DBNL]","" }

Brackets in PowerShell are wildcard characters, that's what the parameter "-literalPath" is about.
 
Last edited:

heatlesssun

Extremely [H]
Joined
Nov 5, 2005
Messages
44,154
This recommendation is more from a repeat usage perspective. Another option for the OP would be to install cygwin and use the same commands. However, I was trying to avoid mocking around with his current OS.

That doesn't make a lot of sense as PowerShell is a scripting tool.
 

ezno_matrix

Limp Gawd
Joined
Feb 9, 2012
Messages
134
Code:
Function Rename-FileExtension($path)
{
 Get-ChildItem $path | 
 Foreach-Object { 
	  $baseName = $_.name.Replace("[DBNL]","")
 Rename-Item -literalPath $_.fullname -newname ($baseName) 
 }
} #end function

Rename-FileExtension -path "C:\Temp"

I tested this with the example you gave and it worked perfectly. It simply removes the substring "[DBNL]" from each file name assuming of course that the file name has that in its name, otherwise it does nothing. Your example would work as well like this:

How would you expand this to remove any unwanted character from filenames?
 

heatlesssun

Extremely [H]
Joined
Nov 5, 2005
Messages
44,154
How would you expand this to remove any unwanted character from filenames?

The first thing to do would be to parameterize the literal string "[DBNL]":

Code:
Function Rename-FileExtension($path, $charactersToRemove)
{
 Get-ChildItem $path | 
 Foreach-Object { 
	  $baseName = $_.name.Replace($charactersToRemove,"")
 Rename-Item -literalPath $_.fullname -newname ($baseName) 
 }
} #end function

Rename-FileExtension -path "C:\Temp" -charactersToRemove "[DBNL]"
 

Demon10000

Supreme [H]ardness
Joined
Aug 20, 2006
Messages
4,502
How would you expand this to remove any unwanted character from filenames?

Just adjust the $basename variable for each modification you want.

Code:
$baseName = $basename.Replace("<text_you_want_to_match>","<text_to_replace_it_with>")

or

Code:
Function Rename-FileExtension($path)
{
 Get-ChildItem $path | 
 Foreach-Object { 
	  $baseName = $_.name.Replace("[DBNL]","")
	  $baseName = $_.name.Replace("1234","4321")
	  $baseName = $_.name.Replace("4444","")

 Rename-Item -literalPath $_.fullname -newname ($baseName) 
 }
} #end function

Rename-FileExtension -path "C:\Temp"

In the example, you'd just adjust $basename until it gives the desired result. Just make sure you're done with it before the rename-item command executes.
 

Shadowspawn

[H]ard|Gawd
Joined
Sep 17, 2002
Messages
1,870
Okay, tried both of these. I'm a noob to the Powershell so I failed at running the script. It appeared to take the function but when I tried to call it Powershell reported the function didn't exist. I'm sure it is something I am doing.

The rename command doesn't support the -literalpath switch.

I ended up downloading the Batch Renaming Utility and was successful with that.


Code:
Function Rename-FileExtension($path)
{
 Get-ChildItem $path | 
 Foreach-Object { 
	  $baseName = $_.name.Replace("[DBNL]","")
 Rename-Item -literalPath $_.fullname -newname ($baseName) 
 }
} #end function

Rename-FileExtension -path "C:\Temp"

I tested this with the example you gave and it worked perfectly. It simply removes the substring "[DBNL]" from each file name assuming of course that the file name has that in its name, otherwise it does nothing. Your example would work as well like this:

Code:
dir | rename-item -literalPath -newname { $_.name -replace "[DBNL]","" }

Brackets in PowerShell are wildcard characters, that's what the parameter "-literalPath" is about.
 

heatlesssun

Extremely [H]
Joined
Nov 5, 2005
Messages
44,154
Cool, as long as you solved your problem. The ps script should have worked though, not sure what's going on there.
 
Top