windows - powershell get-childitem 找不到包含 [ ] 的文件

标签 windows powershell

我使用此代码向某些文件添加文本:

gci C:\Users\Documents\SAMPLE\* -in *.mp4,*.mkv -Recurse | % { rename-item –path $_.Fullname –Newname ( $_.basename +  ' (TEST)' + $_.extension) }

它可以按照我想要的方式工作,但它不适用于名称包含“[]”的文件 并给我这个错误:

无法重命名,因为“C:\Users\Documents\SAMPLE\AAAAAAAAA[B].mp4”中的项目不存在。

最佳答案

调试提示:当您遇到代码问题并且正在使用管道时,请重写代码以不使用管道,将问题分解为步骤,并插入调试辅助工具来帮助排除故障。例如,它可以是Write-Host,保存到临时变量等。

-文字路径

使用-LiteralPath参数指定路径:

[PS]> gci -LiteralPath '.\Music\Artist - Name\Album Name [Disc 1]\'

To improve the ability of Windows PowerShell 3.0 to interpret and correctly handle special characters, the LiteralPath parameter, which handles special characters in paths, is valid on almost all cmdlets that have a Path parameter

有关方括号和转义的更多信息,请参阅以下问答:

关于windows - powershell get-childitem 找不到包含 [ ] 的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73128510/

相关文章:

windows - 纯数据卡住并且不输出音频

windows - 没有图标的 UWP 控制台应用程序,只有执行别名

string - 如何用内部@""@封装脚本,用外部@""@

bash - 如何在 PowerShell 脚本中使用 shebang?

c# - 如何让我的服务停止自动发布到 Windows 应用程序事件日志中

c - 内存分配和释放

python - 我应该在哪里保存应用程序的数据?

regex - 使用 powershell 验证模式后获取下一个字符串

string - PowerShell - 从字符串中选择特定行

c# - 从 C# 运行 Powershell 会出现错误 : `running scripts is disabled on this system`