使用重命名项目 cmdlet 时出现脚本块错误
Script block error when using rename-item cmdlet
执行以下代码时:
$n = 1
$array = "L1", "L2", "L3", "L4", "L5", "L6", "L7", "L8", "L9"
ls *.pdf | sort lastwritetime | foreach-object {
if ($_.name.substring(0,2) -cnotin $array) {
ren -newname { "L$global:n " + $_.name -f $global:n++ }
} else {
$global:n++
}}
$n = 0
我遇到以下错误:
Rename-Item : Cannot evaluate parameter 'NewName' because its argument
is specified as a script block and there is no input. A script block
cannot be evaluated without input. At line:3 char:14
- ren -newname { "L$global:n " + $_.name -f $global:n++ }
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : MetadataError: (:) [Rename-Item], ParameterBindingException
- FullyQualifiedErrorId : ScriptBlockArgumentNoInput,Microsoft.PowerShell.Commands.RenameItemCommand
我哪里错了?管道具有当前目录中的一个文件的名称,但仍然表示它没有接收到任何输入。
The pipeline has the name of a file which is in the current directory but still says it receives no input.
enclosing 管道有,但嵌套管道 (ren -newname { ... }
) 没有任何管道输入,因为您永远不会向它传输任何内容。
更改为:
$n = 1
$array = "L1", "L2", "L3", "L4", "L5", "L6", "L7", "L8", "L9"
ls *.pdf | sort lastwritetime | foreach-object {
if ($_.name.substring(0,2) -cnotin $array) {
$_ |ren -newname { "L{0}{1} " -f $global:n++,$_.name }
} else {
$global:n++
}
}
$n = 0
通过从父管道管道现有项目 ($_
),我们知道给定 ren
/Rename-Item
一些东西可以绑定 $_
到嵌套管道
请注意,作为 ,如果目标目录包含超过 9 个文件,您的脚本可能会开始出现一些潜在的意外行为(尽管您没有明确说明您要实现的目标,所以也许这就是你想要的)
执行以下代码时:
$n = 1
$array = "L1", "L2", "L3", "L4", "L5", "L6", "L7", "L8", "L9"
ls *.pdf | sort lastwritetime | foreach-object {
if ($_.name.substring(0,2) -cnotin $array) {
ren -newname { "L$global:n " + $_.name -f $global:n++ }
} else {
$global:n++
}}
$n = 0
我遇到以下错误:
Rename-Item : Cannot evaluate parameter 'NewName' because its argument is specified as a script block and there is no input. A script block cannot be evaluated without input. At line:3 char:14
- ren -newname { "L$global:n " + $_.name -f $global:n++ }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : MetadataError: (:) [Rename-Item], ParameterBindingException
- FullyQualifiedErrorId : ScriptBlockArgumentNoInput,Microsoft.PowerShell.Commands.RenameItemCommand
我哪里错了?管道具有当前目录中的一个文件的名称,但仍然表示它没有接收到任何输入。
The pipeline has the name of a file which is in the current directory but still says it receives no input.
enclosing 管道有,但嵌套管道 (ren -newname { ... }
) 没有任何管道输入,因为您永远不会向它传输任何内容。
更改为:
$n = 1
$array = "L1", "L2", "L3", "L4", "L5", "L6", "L7", "L8", "L9"
ls *.pdf | sort lastwritetime | foreach-object {
if ($_.name.substring(0,2) -cnotin $array) {
$_ |ren -newname { "L{0}{1} " -f $global:n++,$_.name }
} else {
$global:n++
}
}
$n = 0
通过从父管道管道现有项目 ($_
),我们知道给定 ren
/Rename-Item
一些东西可以绑定 $_
到嵌套管道
请注意,作为