在Powershell中重命名文件(Renaming File in Powershell)

编程入门 行业动态 更新时间:2024-10-28 12:27:02
在Powershell中重命名文件(Renaming File in Powershell)

我正在尝试重命名文件,以便删除名称中的括号。

示例: [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"

更多推荐

本文发布于:2023-08-01 14:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1360086.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重命名   文件   Powershell   Renaming   File

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!