.net - Strange "A positional parameter cannot be found" with Rename-Item
问题描述
Using Powershell 7, this works perfectly:
dir foo* | Rename-Item -Path {$_.Name} -NewName{$_.Name -replace 'bar-(.*)', 'bar-$1'}
This fails:
dir foo* | Rename-Item {$_.Name} {$_.Name -replace 'bar-(.*)', 'bar-$1'}
With the following error:
Rename-Item: A positional parameter cannot be found that accepts argument '$_.Name'.
Based on the docs here -Path
is the first position of Rename-Item
. Also, the following command works: Rename-Item 'foo-one' 'foo-one-one'
. Why does the above failure happen?
解决方案
尽管您的示例代码实际上并未重命名任何文件(-replace 'bar-(.*)', 'bar-$1'
将导致完全相同的名称。),但这里的主要问题是您{$_.Name}
直接在Rename-Item
cmdlet 之后添加,这不是必需的,因为您已经通过管道传递 fileInfo 对象, 所以 now{$_.Name}
被视为参数名称。
接下来,您忘记为 parameter 添加名称-NewName
。
foo*
通过像你一样遍历一条路径-Path 'foo' -Recurse
,其中有一个陷阱..
特别是在遍历许多文件并通过管道传递它们时,已经重命名的文件有可能再次被Get-ChildItem
(alias dir
) 拾取,然后将被重命名两次。
为了克服这个问题,将收集文件的部分放在括号之间,这样收集所有 FileInfo 对象就完成了,然后将其通过管道传递到下一个 cmdlet,如下所示:
(Get-ChildItem -Path 'foo' -File -Recurse) |
Rename-Item -NewName {$_.Name -replace 'bar-(.*)', 'baz-$1'}
为清楚起见,这确实将名称的一部分从更改bar
为baz
推荐阅读
- django - 无需重新加载且无需 javascript 即可更新 django 模板
- css - 在伪元素上重新加载每个页面后,Z-index 会导致奇怪的重影效果
- spring-boot - 是否可以阻止创建 bean?
- sql - 过滤 MariaDB SQL 表中按 dept_name 分组的列中的值的总和
- javascript - Dymo 在 Windows 中打印空白(在 mac 中工作正常)
- sql - 如何在sql中找到小于最大的数?
- python - 用于选择目的地的简单 Python 假期脚本
- firebase - Firebase Firestore 多条件子句或云函数,带有 Flutter
- blazor-webassembly - 托管的 Blazor WASM WebRootPath 为空
- airflow - Apache Airflow 中的自定义操作员状态(排队、成功等)?