如何使用 PowerShell 批量重命名文件? [英] How can I bulk rename files using PowerShell?

查看:113
本文介绍了如何使用 PowerShell 批量重命名文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 tf rename 递归地重命名一堆 TFS 文件夹PowerShell 和正则表达式,但我在使用 PowerShell 时遇到了一些问题没有花太多时间在它上面.这是我到目前为止用 2.3.2 替换前导 5 但它不起作用的方法:

目录 |foreach { tf 重命名 $_ { $_.Name -replace '^5', '2.3.2' } }

实际结果:

<前>无法识别的命令选项encodedCommand".无法识别的命令选项encodedCommand".无法识别的命令选项encodedCommand".无法识别的命令选项encodedCommand"....等等.

更新:

我通过以下方式接近了一点:

目录 |foreach { $newname = $_.Name -replace "^5", "2.3.2";tf 重命名 $_ $newname }

我的下一个目标是创建这个递归子目录,但这似乎更具挑战性(将其更改为 dir -recurse 使其由于某种原因在父文件夹之后退出).

解决方案

我将首先按 5* 进行过滤,因此您只能处理以 5 开头的名称.此外,在这种情况下,由于 tf.exe 不是 PowerShell cmdlet,您不想使用脚本块来确定新名称.只需使用像这样的分组表达式:

dir -filter 5* |foreach { tf 重命名 $_ ($_.Name -replace '^5', '2.3.2')}

顺便说一句,当您尝试调试像这样传递给本机 EXE 的参数时,使用 PowerShell 社区扩展.这是它告诉我的关于您的原始方法的信息:

6# dir -filter 5* |foreach { echoargs 重命名 $_ { $_.Name -replace '^5', '2.3.2' } }Arg 0 是<rename>Arg 1 是 <5foo.txt>Arg 2 是<-encodedCommand>Arg 3 是 <IAAkAF8ALgBOAGEAbQBlACAALQByAGUAcABsAGEAYwBlACAAJwBeADUAJwAsACAAJwAyAC4AMwAuADIAJwAgAA==>Arg 4 是 <-inputFormat>Arg 5 是 <xml>Arg 6 是<-outputFormat>Arg 7 是 <text>

I'm trying to recursively rename a bunch of TFS folders using tf rename, PowerShell and a regular expression but I'm having some issues with PowerShell as I haven't spent much time with it. This is what I've put together so far to replace a leading 5 with 2.3.2 but it isn't working:

dir | foreach { tf rename $_ { $_.Name -replace '^5', '2.3.2' } }

Actual result:

Unrecognized command option 'encodedCommand'.
Unrecognized command option 'encodedCommand'.
Unrecognized command option 'encodedCommand'.
Unrecognized command option 'encodedCommand'.
...etc.

Update:

I got a little closer by doing the following instead:

dir | foreach { $newname = $_.Name -replace "^5", "2.3.2"; tf rename $_ $newname }

My next goal is to make this recurse subdirectories but this seems a bit more challenging (changing it to dir -recurse makes it quit after the parent folders for some reason).

解决方案

I would first filter by 5* so you only process names that start with 5. Also, in this case since tf.exe isn't a PowerShell cmdlet, you don't want to use a scriptblock to determine a new name. Just use a grouping expression like so:

dir -filter 5* | foreach { tf rename $_ ($_.Name -replace '^5', '2.3.2')}

BTW, when you are trying to debug parameter passing to a native EXE like this it is immensely helpful to use the echoargs.exe utilty from the PowerShell Community Extensions. This is what it told me about your original approach:

6# dir -filter 5* | foreach { echoargs rename $_ { $_.Name -replace '^5', '2.3.2' } }
Arg 0 is <rename>
Arg 1 is <5foo.txt>
Arg 2 is <-encodedCommand>
Arg 3 is <IAAkAF8ALgBOAGEAbQBlACAALQByAGUAcABsAGEAYwBlACAAJwBeADUAJwAsACAAJwAyAC4AMwAuADIAJwAgAA==>
Arg 4 is <-inputFormat>
Arg 5 is <xml>
Arg 6 is <-outputFormat>
Arg 7 is <text>

这篇关于如何使用 PowerShell 批量重命名文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆