Renaming Files with PowerShell

*爱你&永不变心* 提交于 2019-11-29 16:43:04

You need to specify the -Recurse parameter on Dir to get it to recurse e.g.:

Dir -recurse | Rename-Item -NewName {$_.Name -replace ' ','_'}

BTW this may run into a problem because you're renaming the folder (FOLDERB) that contains the document first but the item being piped that corresponds to the file in FOLDERB still has the old name. In this case, you want to rename from the bottom up. One very crude but effective (I think) way to do this is to sort the file items on their path length descending e.g.:

Dir -recurse | Sort {$_.FullName.Length} -Desc | Rename-Item {$_.Name -replace ' ','_'}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!