2

I'm having big troubles combining the rename with an if-condition. PS cannot rename when the file has the same name as before and won't do anything afterwards. Plus it's unable to rename recursively.

I wanna do sth like:

$entrys = Get-ChildItem -Recurse $myPath
foreach ($name in $entrys) {
    $newName=$name.FullName -replace('[\,\/\*\+\#\~\-\=\^\°\$\%\&\<\>\|]','')
    if (-not ($newName -like $name)) {
         Rename-Item $name -NewName $newName
    }
}

I found something similar here but they dont have any conditions there.

So how do I pipe into an if-condition before renaming, so I can pipe directly from get-childitem -recurse to if() then rename()? Or is there another possibility?

Thanks for help

Community
  • 1
  • 1
bootsector
  • 49
  • 1

2 Answers2

2

When I try your code I have the same issue that it reports it's unable to rename the file. However, I modified a bit of your code, and now it seems to work:

$entrys = Get-ChildItem -Recurse $myPath -File
foreach ($name in $entrys) {
    $newName = $name.Name -replace('[\,\/\*\+\#\~\-\=\^\°\$\%\&\<\>\|]','')
    if ($newName -ne $name.Name) {
         Rename-Item -LiteralPath $name.FullName -NewName $newName
    }
}

First you have to only check for files and not folders. Because once you renamed a folder the files are no longer found when you recurse into a non existing folder name (one that has changed).

If you need to rename folders also. It needs some more work as the folder names might change.

Hope this helps you out.

DarkLite1
  • 13,637
  • 40
  • 117
  • 214
  • it really seems to be working now. But I dont get why. Is it because of the `-LiteralPath` ? For testcases I named all folders without any special characters, so folders shouldn't be renamed anyways... – bootsector Sep 01 '15 at 12:09
  • The difference is that I only rename the file `Name` and not the `FullName`, which also contains the path (see variable `$newName`). If this gets renamed, it doesn't work. For example `\\Server++1\File+1+.txt` can not be renamed, because you want to rename the folder and the file at the same time... – DarkLite1 Sep 01 '15 at 12:14
  • You're welcome! Glad it helped you out :) Don't forget to mark it as solved. – DarkLite1 Sep 01 '15 at 12:58
1

My PowerShell doesn't throw any error on the Rename-Item cmdlet if the file has the same name. But you could try to use -ErrorAction 0.

However, this oneliner works for me:

gci $myPath -r | % { Rename-Item  $_.FullName  ($_.FullName -replace('[\,\/\*\+\#\~\-\=\^\°\$\%\&\<\>\|]','')) }
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
  • when I try that, my PS tells me the following: Rename-Item : Source and destination path must be different. – bootsector Sep 01 '15 at 11:41
  • can you try to use `Move-Item` instead of `rename-item` – Martin Brandl Sep 01 '15 at 11:43
  • I'm using PowerShell 4.0 and I don't get any error if I run: Rename-Item .\foo.txt .\foo.txt. What version are you using that's giving you an error? – campbell.rw Sep 01 '15 at 12:39
  • I'm running PS 3.0. Trying move instead of rename leads to another error, saying some files where "in use"... Anyways @DarkLite1 solved it and I'm happy now :-) – bootsector Sep 01 '15 at 12:51