Powershell- Getting a strange error when I try to replace characters in folder names

I often have .pdf files where I need to remove ‘- copy’, or replace the date on all of them, etc. So I’ve been using a script to do so. I’m now trying to expand upon my current script to do the same thing ONLY to folders in a given directory, but I’m getting an error on anything that doesn’t contain the characters I want to remove/change.

This is the script I currently use for .pdfs, in this example I just want to remove ABC from everything (i.e. replace it with nothing):

$path = "C:examplepath"
$filter = '*.pdf'
get-childitem -path $path -filter $filter | 
 rename-item -newname {$_.name -replace 'ABC',''}

And this is what I’ve come up with to do the same, but only on folders (including subfolders):

$FolderPath = "C:examplepath"

Get-ChildItem -Path $FolderPath -Directory -Recurse | rename-item -newname {$_.name -replace 'ABC',''}

It removes ABC from any folder names, but I get the following error for any folders that don’t contain ABC. In this example, the folder was just named ‘1’:

rename-item : Source and destination path must be different.
At line:3 char:55
+ ... Directory -Recurse | rename-item -newname {$_.name -replace 'ABC',''}
+                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (C:examplepath1:String) [Rename-Item], IOException
    + FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand

I kind of understand what it’s saying by ‘source and destination path must be different’- it can’t change a folder named ‘1’ to a folder named ‘1’. But I don’t understand why I’m getting that error now, but not when I run my .pdf script. It’s not the end of the world since the script does what I want, but I’d be more comfortable if it wasn’t returning errors.

Use a filter to ensure you’re renaming a folder that does have ABC in its name. You should also probably consider start renaming them from the deepest in hierarchy, so Sort-Object on FullName:

$FolderPath = 'C:examplepath'
Get-ChildItem -Path $FolderPath -Directory -Recurse |
    Where-Object Name -Like *ABC* |
    Sort-Object FullName -Descending |
    Rename-Item -NewName { $_.Name -replace 'ABC' }

As for the error, reason is that if you rename a file using its same name it isn’t an issue but for a directory it’s different, it must have a different name, thus the need for a filter, that’s how the underlying .NET APIs work when renaming.

2

I don’t understand why I’m getting that error now, but not when I run my .pdf script

That’s because Rename-Item calls different .NET methods based on whether the item is a directory or a file – and DirectoryInfo.MoveTo() happens to throw errors when you request a local move to the same path, whereas FileInfo.MoveTo() does not.

As to why the check is in place for directories but not files, you’d have to ask the developers who implemented those methods 🙂

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật