我正在撰寫一個腳本來在播放器中播放某些檔案。我使用 Get-ChildItem 來獲取檔案名陣列。然后我想使用 Start-Process 來播放這些檔案。但是,如何將這些檔案名添加到播放器程式的引數中?
我用過,Start-Process -FilePath "C:\Program Files\DAUM\PotPlayer\PotPlayerMini64.exe" -ArgumentList $selected_items但似乎不起作用,檔案也沒有播放。
注意檔案名中有空格。
uj5u.com熱心網友回復:
語法-wise,你的方法應該作業,但沒有,因為一個不幸的錯誤,仍然存在于PowerShell的7.2 -見GitHub的問題#5576。
同時使一個陣列的引數
Start-Process的-ArgumentList引數確實引起所述陣列元件被作為通過個別的引數(其通常的CLI如何外部期望多檔案變數),必要的雙引述元素周圍用空格是未施加時在命令列最終用于呼叫的是在幕后構造的。此外,為了健壯性,您應該使用
.FullName存盤在 中的物件的屬性,$selected_items以確保傳遞完整路徑,因為 - 在Windows PowerShell 中,視情況而定 -Get-ChildItem的輸出物件可能僅字串化為檔案名- 請參閱此答案。
解決方法:將單個引數傳遞給-ArgumentList,在其中使用嵌入式雙引號對所有傳遞引數進行編碼。
Start-Process `
-FilePath "C:\Program Files\DAUM\PotPlayer\PotPlayerMini64.exe" `
-ArgumentList ($selected_items.ForEach({ '"{0}"' -f $_.FullName }) -join ' ')
退一步:
如果PotPlayerMini64.exe是Windows GUI(-subsystem)應用程式,你不需要做Start-Process所有,因為即使是直接呼叫將然后采取行動異步(即程式將啟動,并控制將回傳到PowerShell的馬上;反之,如果你想等待程式退出,使用Start-Process -Wait)。
& "C:\Program Files\DAUM\PotPlayer\PotPlayerMini64.exe" $selected_Items.FullName
請注意,在諸如此類的直接呼叫中,PowerShell確實會根據需要在幕后執行必要的雙引號。
注意:我不清楚將多個檔案路徑傳遞給PotPlayerMini64.exe 單獨是否也會開始播放- 下一節中的替代解決方案可以確保這一點。
替代的,基于剪貼板的解決方案:
根據PotPlayerMini64.exe的可用命令列選項[1]判斷,以下可能有效(我無法親自驗證):
/clipboard:將剪貼板中的內容附加到播放串列中并立即開始播放。
# Copy the full names of the files of interest to the clipboard.
Set-Clipboard -Value $selected_items.FullName
# Launch the player and tell it to start playback of the files on the clipboard.
# Parameters -FilePath and -ArgumentList are positionally implied.
Start-Process 'C:\Program Files\DAUM\PotPlayer\PotPlayerMini64.exe' /clipboard
有檔案的引數相關的選項,例如/new,/insert和/add,但它是我不清楚他們是否-或省略通通,如您嘗試-自動開始播放(可以依賴于應用程式的持久性配置)。
[1] 注意這不是官方檔案;我找不到后者。
uj5u.com熱心網友回復:
你可以ForEach-Object:
Get-ChildItem . | ForEach-Object {Start-Process -FilePath "C:\Program Files\DAUM\PotPlayer\PotPlayerMini64.exe" -ArgumentList $_.FullName}
uj5u.com熱心網友回復:
您不需要 start-process (另外, -argumentlist 不能輕松處理帶空格的檔案名)。
& "C:\Program Files\DAUM\PotPlayer\PotPlayerMini64.exe"
C:\Program` Files\DAUM\PotPlayer\PotPlayerMini64.exe
$env:path = ';C:\Program Files\DAUM\PotPlayer'; PotPlayerMini64
即使使用啟動行程,它也因程式而異。例如,Emacs 可以采用多個檔案引數,以空格分隔。如果檔案名有空格,則需要用雙引號引起來。外部程式不知道什么是陣列,start-process 將它們轉換為一個字串,每個元素之間都有空格。
start emacs file1,file2,'"my file"'
get-wmiobject win32_process | ? name -eq emacs.exe | % commandline
"c:\program files\emacs\bin\emacs.exe" file1 file2 "my file"
ps emacs | % commandline # ps 7
"c:\program files\emacs\bin\emacs.exe" file1 file2 "my file"
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/393498.html
標籤:电源外壳
