我正在尝试重命名文件,以便删除名称中的括号。
示例: [Example] Test File (Servers).xls重命名为Example Test File (Servers).xls
我的问题是,当我运行这个,它返回一个错误说
Rename-Item -Path '.\'[Example'] Test File (Servers).xls' -NewName "Example Test File (Servers).xls"Rename-Item:指定路径下的对象C:\ Users \ Documents \ PowerShell \ [示例]测试文件(服务器).xls不存在
另一个问题是,当我运行:
C:\Users\Documents\PowerShell> Get-ChildItem -Path "[Example] Test File (Servers).xls"这运行成功,没有错误,所以我知道PowerShell找到这个文件没有问题。 在这一点上,我不知道为什么这不起作用,因为它可以用一个命令找到文件,而不是另一个。
I'm trying to rename a file so I can remove the brackets within the name:
Example: [Example] Test File (Servers).xls renamed to Example Test File (Servers).xls
My issue is that when I run this, it returns an error saying
Rename-Item -Path '.\'[Example'] Test File (Servers).xls' -NewName "Example Test File (Servers).xls"Rename-Item : An object at the specified path C:\Users\Documents\PowerShell\[Example] Test File (Servers).xls does not exist
The other issue is that when I run:
C:\Users\Documents\PowerShell> Get-ChildItem -Path "[Example] Test File (Servers).xls"This runs successfully with no errors, so I know PowerShell is finding this file with no problems. At this point, I don't know why this isn't working because it can find the file with one command, and not with the other.
最满意答案
如果您使用现代版本的Powershell(v4 + iirc),则可以使用LiteralPath而不是Path 。
它可以处理像[]这样的特殊字符,而不必转义它们。
Rename-Item -LiteralPath ".\[Example] Test File (Servers).xls" -NewName "Example Test File (Servers).xls"You can use LiteralPath instead of Path if you're using a modern version of Powershell (v4+ iirc).
It can deal with special characters like [] without having to escape them.
Rename-Item -LiteralPath ".\[Example] Test File (Servers).xls" -NewName "Example Test File (Servers).xls"更多推荐
发布评论