如何使用 Powershell 递归重命名文件夹?
使用 PS 递归重命名文件很简单(来自 Mike Ormond 的博客):
dir *_t*.gif -recurse
| foreach { move-item -literal $_ $_.Name.Replace("_thumb[1]", "")}
我正在尝试递归地重命名文件夹结构。
用例是我希望能够重命名整个 VS.NET 解决方案(例如从 Foo.Bar 到 Bar.Foo)。为此,有几个步骤:
- 重命名文件夹(例如 \Foo.Bar\Foo.Bar.Model => \Bar.Foo\Bar.Foo.Model)
- 重命名文件(例如 Foo.Bar.Model.csproj => Bar.Foo.Model.csproj)
- 在文件中查找并替换以更正命名空间更改(例如“命名空间 Foo.Bar”=>“命名空间 Bar.Foo”)
我目前正在执行此过程的第一步。
我发现这篇帖子,其中讨论了挑战,并声称提供了解决方案,但没有谈论这个解决方案是什么。
我总是遇到递归墙。如果我让 PS 使用标志来处理递归,则父文件夹会在子文件夹之前重命名,并且脚本会引发错误。如果我尝试自己实现递归,我的头会很痛,事情会变得非常错误 - 在我的一生中,我无法让事物在递归树的尾部开始重命名。
Recursive renaming files using PS is trivial (variation on example from Mike Ormond's blog):
dir *_t*.gif -recurse
| foreach { move-item -literal $_ $_.Name.Replace("_thumb[1]", "")}
I'm trying to recursively rename a folder structure.
The use case is I'd like to be able to rename a whole VS.NET Solution (e.g. from Foo.Bar to Bar.Foo). To do this there are several steps:
- Rename folders (e.g. \Foo.Bar\Foo.Bar.Model => \Bar.Foo\Bar.Foo.Model)
- Rename files (e.g. Foo.Bar.Model.csproj => Bar.Foo.Model.csproj)
- Find and Replace within files to correct for namespace changes (e.g. 'namespace Foo.Bar' => 'namespace Bar.Foo')
I'm currently working the first step in this process.
I found this posting, which talks about the challenges, and claims a solution but doesn't talk about what that solution is.
I keep running into the recursion wall. If I let PS deal with the recursion using a flag, the parent folder gets renamed before the children, and the script throws an error. If I try to implement the recursion myself, my head get's all achy and things go horribly wrong - for the life of me I cannot get things to start their renames at the tail of the recursion tree.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是 rbellamy 最终得到的解决方案:
Here's the solution rbellamy ended up with:
怎么样 - 做一个全名的递归列表,按全名的长度降序排序,然后通过您的重命名例程运行它。
例如
How about this - do a recursive list of the full names, sort it in descending order by the length of the full name, and then run that back through your rename routine.
e.g.
也许其中的某些内容很有用,这是一个递归并将“pre”添加到目录结构的片段
Maybe something in this is useful, here's a snippet that recurses and prepends "pre" to a directory structure