簡體   English   中英

帶空格的文件路徑在批處理文件中不起作用

[英]File path with spaces not working in batch file

我正在嘗試將包含 PowerShell 腳本的文本文件從 a.bat 文件導入到在命令行中運行的 PowerShell 中。

在沒有系統權限的情況下在啟動時啟動 PowerShell 腳本是一種變通方法。

在我嘗試帶空格的路徑之前,它工作得很好。 有人知道如何進行這項工作嗎?

我搜索並嘗試了許多解決方案(請參閱 3、4、5、6),但要么這是一個非常具體的案例,要么我做錯了什么。 總的來說,我對腳本的了解還不夠,但我正在盡力而為。 請在您的回答中使用簡單的語言。

  1. 這有效

    @echo off powershell "cat -raw C:\examplenospace\test.txt | invoke-expression"
  2. 這有效

    @echo off set "file=C:\examplenospace\test.txt" powershell "cat -raw %file% | invoke-expression"
  3. 不工作

    @echo off set "file=C:\example with space\test.txt" powershell "cat -raw %file% | invoke-expression"
  4. 這當然也行不通

    @echo off powershell "cat -raw "C:\example with space\test.txt" | invoke-expression"
  5. 也不行

    @echo off powershell ""cat -raw "C:\example with space\test.txt" | invoke-expression"
  6. 我嘗試了很多變體,似乎沒有任何效果

    @echo off powershell "cat -raw \"\"C:\example with space\test.txt" | invoke-expression"

我怎樣才能讓它發揮作用?

@Compo 感謝您的回答,這兩個都立即起作用了!

@%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command "Get-Content -LiteralPath 'C:\example with space\test.txt' -Raw | Invoke-Expression"

@%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command "Get-Content -LiteralPath "C:\example with space\test.txt" -Raw | Invoke-Expression"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM