看板 Windows 關於我們 聯絡資訊
作業系統: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://www.ptt.cc/bbs/Windows/M.1603362528.A.2A7.html
cancelpc: 一般是替換成2個雙引號,就會當成一個雙引號(溢出字元處10/22 18:55
cancelpc: 理)10/22 18:55
cancelpc: 看是那種語言,就配合該語言的溢出字元處理。加上看是誰10/22 18:57
cancelpc: 執行路徑,也要依那種方式處理 10/22 18:57
LPH66: 不對, 原 PO 的問題是 powershell 好像對工作目錄裡有 []10/22 19:03
LPH66: 的時候使用相對路徑指定檔案會有問題 10/22 19:04
LPH66: 因為是相對路徑所以指令裡根本也就沒有能跳脫的字元10/22 19:04
LPH66: 目前找得到的解決法都是繞個圈指定絕對路徑, 例如用 $(pwd)10/22 19:05
LPH66: 展開成工作目錄, 再丟給指令去做 10/22 19:05
falcon: L大的意思是先($pwd+相對路徑)組成完整路徑在處理嗎? 10/22 19:47
falcon: 但我用Start-Process執行$env:Path下的程式也會出錯10/22 19:47
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
kobe8112: 這篇的解答是你要的嗎? https://tinyurl.com/y5gnwnw5 10/23 16:04
falcon: 我可以正常處裡含中掛號的檔案路徑 10/23 16:55
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
falcon: 不過Test-Path無法自己指定工作目錄就不適用這方法 10/24 03:32
falcon: 目前也只能將相對路徑轉成完整路徑來避開工作目錄問題 10/24 03:36
※ 編輯: falcon (27.52.102.237 臺灣), 10/24/2020 19:34:27