【发布时间】:2017-01-12 07:50:14
【问题描述】:
我正在尝试使用 Get-Childitem | Where-Object 的输出来重命名一组文件,但我不想用常量名重命名我想用一组变量重命名,我正在使用运算符 -replace找到字符串中我想要更改的位置。示例:
nane_01 -> name_23
name_02 -> name_24 they are not with the same final digit
这是我用于测试的代码:
$a = 0
$b = 1
$Na= 2
$Nb= 3
Get-Childitem | Where-Object {$_.name -match "_0[1-9]"} | rename-item -Newname { $_.name -replace "_{0}{1}","_{2}{3}" -f $a++,$b++,$Na++,$Nb++ } -Whatif
我找不到如何使用 -replace 运算符使增量变量工作
【问题讨论】:
-
明确指定范围:
$script:a等等。 -
这行得通,因为我现在没有得到错误,但增量的结果不起作用,比如变量 a=0 和 na = 0,所以它们不会更改名称。你能指出我有什么问题吗?抱歉,我是使用 Powershell 的新手
标签: powershell variables replace rename-item-cmdlet