anatp_123
anatp_123

Reputation: 1205

How to remove character from files in folder in command?

I have windows 7 I have a folder name employeephotos about 100 pictures that all have name_last. I would like to remove all the "_" from the files in the folder and make the files namelast.jpg.

I have try this command

PS U:\desktop\employeephotos> Dir | Rename-Item -NewName { $_.name replace"_",""}

Rename-Item : Source and destination path must be different.
At line:1 char:18
+ Dir | Rename-Item <<<<  -NewName { $_.name -replace"_",""}
    + CategoryInfo          : WriteError: (U:\desktop\employeephotos\New folder:String) [Rename-
    + FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand

Upvotes: 0

Views: 772

Answers (2)

PSGuy
PSGuy

Reputation: 749

You might try something like this:

Get-ChildItem -Filter "*_*" | Foreach-Object { Rename-Item -Path $_.Name -NewName $_.Name.Replace("_", "") -WhatIf }

If it produces the output you're looking for, then just pull the -WhatIf parameter off.

Upvotes: 0

TessellatingHeckler
TessellatingHeckler

Reputation: 29003

(From my comments)

  1. Rename-Item is a PowerShell command, so you need to be working in PowerShell instead of the cmd.exe command prompt.

  2. It might be picking up files with no _ in the name, the rename does not change the name, and they cannot be given the same name, so it fails. Try dir *_* at the start to only find and rename files with _ in the name.

Upvotes: 2

Related Questions