weda
weda

Reputation: 11

PowerShell Rename-Item is looping until file reaches character limit

I have to rename literally thousands of folders and want to use a script which adds "Old" in front of the old Name.

I tried this script

Get-ChildItem | rename-item -NewName { "Old+ $_.Name }

It even works when I try it in some small test folders to prevent messing up my main folder but unfortunally it's working there but not in my main folder. In my main folder this command will loop until it hits the character limit and stops.

Looks like this:

Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Zimmer.

Online i found this one too but it produces the same result

get-childitem | % { rename-item $_ "Old $_"}

Is it a bug or am I just stupid?

Upvotes: 1

Views: 96

Answers (1)

Theo
Theo

Reputation: 61013

To rename the folders in a given path by prefixing them with "Old", this works for me:

$path = "<PATH TO YOUR MAIN FOLDER>"
Get-ChildItem -Path $path -Directory | Rename-Item -NewName { "Old$($_.Name)" }

# For PowerShell version less than 3.0
# Get-ChildItem -Path $path | Where-Object { $_.PSIsContainer} | Rename-Item -NewName { "Old$($_.Name)" }

(by not setting the -Path parameter, the Get-ChildItem cmdlet uses the default location which is the current directory . or $pwd)

Upvotes: 1

Related Questions