[問題] PowerShell工作目錄路徑含中括號字元問題

作者: falcon (falken)   2020-10-22 18:28:46
作業系統: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命令也會發生發生錯誤...
我想請問,除了使用絕對路徑以外有什麼解決方法?
作者: cancelpc (阿吉)   2020-10-22 18:55:00
一般是替換成2個雙引號,就會當成一個雙引號(溢出字元處理)看是那種語言,就配合該語言的溢出字元處理。加上看是誰執行路徑,也要依那種方式處理
作者: LPH66 (-6.2598534e+18f)   2020-10-22 19:03:00
不對, 原 PO 的問題是 powershell 好像對工作目錄裡有 []的時候使用相對路徑指定檔案會有問題因為是相對路徑所以指令裡根本也就沒有能跳脫的字元目前找得到的解決法都是繞個圈指定絕對路徑, 例如用 $(pwd)展開成工作目錄, 再丟給指令去做
作者: falcon (falken)   2020-10-22 19:47:00
L大的意思是先($pwd+相對路徑)組成完整路徑在處理嗎?但我用Start-Process執行$env:Path下的程式也會出錯除非我放棄指定工作目錄
作者: kobe8112 (小B)   2020-10-23 16:04:00
這篇的解答是你要的嗎? https://tinyurl.com/y5gnwnw5
作者: falcon (falken)   2020-10-23 16:55:00
我可以正常處裡含中掛號的檔案路徑我遇到的問題是工作目錄中含有中括號會出錯

Links booklink

Contact Us: admin [ a t ] ucptt.com