我正在尝试递归地重命名一堆 TFS使用tf rename
的文件夹,PowerShell和正则表达式,但我在使用 PowerShell 时遇到了一些问题,因为我没有花太多时间使用它。这是我到目前为止所整理的,用 2.3.2
替换前导 5
,但它不起作用:
dir | foreach { tf rename $_ { $_.Name -replace '^5', '2.3.2' } }
实际结果:
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 }
我的下一个目标是创建此递归子目录,但这似乎更具挑战性(将其更改为 dir -recurse
会使其因某种原因在父文件夹之后退出)。
最佳答案
我会首先按 5* 进行过滤,这样您只处理以 5 开头的名称。此外,在这种情况下,由于 tf.exe 不是 PowerShell cmdlet,因此您不想使用脚本 block 来确定新名称。只需使用像这样的分组表达式:
dir -filter 5* | foreach { tf rename $_ ($_.Name -replace '^5', '2.3.2')}
顺便说一句,当您尝试像这样调试传递到 native EXE 的参数时,使用 PowerShell Community Extensions 中的 echoargs.exe 实用程序非常有帮助。 。这就是它告诉我的关于你最初的方法的信息:
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>
关于tfs - 如何使用 PowerShell 批量重命名文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1267967/