作者falcon (falken)
看板Windows
标题[问题] PowerShell工作目录路径含中括号字元问题
时间Thu Oct 22 18:28:46 2020
作业系统:Windows 10
Service Pack:20H2
发生问题频率:100%
是否有做Windows Update:有
问题内容:PowerShell工作目录路径中带有中括号导致结果不如预期
PS D:\> Test-Path -LiteralPath "D:\[test] videos\test video 1.mp4"
True
PS D:\> Test-Path -LiteralPath "D:\test videos\test video 1.mp4"
True
PS D:\> Set-Location -LiteralPath "D:\test videos"
PS D:\test videos> Test-Path -LiteralPath "test video 1.mp4"
True
PS D:\test videos> Set-Location -LiteralPath "D:\[test] videos"
PS D:\[test] videos> Test-Path -LiteralPath "test video 1.mp4"
False
PS D:\[test] videos> $env:Path = "C:\ffmpeg\bin;" + $env:Path
PS D:\[test] videos> Start-Process -FilePath "ffmpeg" -ArgumentList '-i "test
video 1.mp4" -c copy output.mkv' -NoNewWindow -PassThru -Wait
Start-Process : 无法执行作业,因为万用字元路径 D:\[test] videos 无法解析成档案
。
位於 线路:1 字元:1
+ Start-Process -FilePath "ffmpeg" -ArgumentList '-i "test video 1.mp4" ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (D:\[test] videos:String) [Start-
Process], FileNotFoundException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.
StartProcessCommand
如上所示若工作目录中带有中括号时用Test-Path判断相对路径皆回传False
Start-Process命令也会发生发生错误...
我想请问,除了使用绝对路径以外有什麽解决方法?
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 27.52.102.237 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Windows/M.1603362528.A.2A7.html
1F:推 cancelpc: 一般是替换成2个双引号,就会当成一个双引号(溢出字元处10/22 18:55
2F:→ cancelpc: 理)10/22 18:55
3F:→ cancelpc: 看是那种语言,就配合该语言的溢出字元处理。加上看是谁10/22 18:57
4F:→ cancelpc: 执行路径,也要依那种方式处理 10/22 18:57
5F:推 LPH66: 不对, 原 PO 的问题是 powershell 好像对工作目录里有 []10/22 19:03
6F:→ LPH66: 的时候使用相对路径指定档案会有问题 10/22 19:04
7F:→ LPH66: 因为是相对路径所以指令里根本也就没有能跳脱的字元10/22 19:04
8F:→ LPH66: 目前找得到的解决法都是绕个圈指定绝对路径, 例如用 $(pwd)10/22 19:05
9F:→ LPH66: 展开成工作目录, 再丢给指令去做 10/22 19:05
10F:→ falcon: L大的意思是先($pwd+相对路径)组成完整路径在处理吗? 10/22 19:47
11F:→ falcon: 但我用Start-Process执行$env:Path下的程式也会出错10/22 19:47
12F:→ falcon: 除非我放弃指定工作目录10/22 19:47
将相对路径转成完整路径再处里可以避免Test-Path出问题
if (-not($Path -match '^[a-z]\:.+$'))
{
$Path = $pwd.Path + '\' + $Path
}
而Start-Process只要工作目录有中括号就会出现问题
我这边想到的方法是改用 .NET 物件来处理
$p = New-Object System.Diagnostics.Process
$p.StartInfo.FileName = "ffmpeg"
$p.StartInfo.UseShellExecute = $false
$p.StartInfo.RedirectStandardError = $true
$p.StartInfo.Arguments = $Arguments
$p.StartInfo.WorkingDirectory = $pwd
$p.Start()
while (-not($p.StandardError.EndOfStream))
{
Write-Host $p.StandardError.ReadLine()
}
$p.WaitForExit()
但这样感觉越来越麻烦了
希望有人能提供用PowerShell指令的解法
※ 编辑: falcon (27.52.102.237 台湾), 10/23/2020 01:14:13
※ 编辑: falcon (27.52.102.237 台湾), 10/23/2020 15:45:51
14F:→ falcon: 我可以正常处里含中挂号的档案路径 10/23 16:55
15F:→ falcon: 我遇到的问题是工作目录中含有中括号会出错 10/23 16:55
我找到方法解决 Start-Process 的问题了
不要让 Start-Process 自己取得 PowerShell 工作目录
用 -WorkingDirectory 指定工作并以正规表示处理跳脱字元
$wd = ($pwd.path -replace '([\`])', '`$1') -replace '([\[\]])', '`$1'
Start-Process -FilePath "ffmpeg" -WorkingDirectory $wd
※ 编辑: falcon (27.52.102.237 台湾), 10/24/2020 03:20:44
16F:→ falcon: 不过Test-Path无法自己指定工作目录就不适用这方法 10/24 03:32
17F:→ falcon: 目前也只能将相对路径转成完整路径来避开工作目录问题 10/24 03:36
※ 编辑: falcon (27.52.102.237 台湾), 10/24/2020 19:34:27