簡體   English   中英

powershell 從 cmd 運行

[英]powershell run from cmd

在文本文件List.txt我想刪除引號並保存在ListF.txt

我可以在 powershell 控制台中運行以下代碼:

(Get-Content List.txt) | ForEach-Object { $_ -replace '"','' } | Set-Content ListF.txt

但是我不能用 cmd 運行相同的命令:

powershell.exe  -command "{(Get-Content List.txt) | ForEach-Object { $_ -replace '"','' } | Set-Content ListF.txt}"

我非常感謝所有的幫助!

如此評論所說,您需要轉義一些特殊字符。

這就是 cmd.exe 的工作原理,但我無法解釋原因。

帶引號

powershell.exe -command "(Get-Content List.txt) | ForEach-Object { $_ -replace '""','' } | Set-Content ListF.txt"
powershell.exe -command "(Get-Content List.txt) | ForEach-Object { $_ -replace '\"', '' } ^| Set-Content ListF.txt"

關於| escaping

:: escape `|` until `"` has its pair
echo "1 | 2 " ^| 3 ^| " 4 | 5"

:: every `|` needs escaping because `"` does not have its pair
echo "1 | 2 " ^| 3 ^| 4 ^| 5"

:: no escape
echo "1 | 2 "" | 3 | 4 | 5"

不帶引號

powershell.exe -command (Get-Content List.txt) ^| ForEach-Object { $_ -replace '\"', '' } | Set-Content ListF.txt
powershell.exe -command (Get-Content List.txt) ^| ForEach-Object { $_ -replace '""""', '' } ^| Set-Content ListF.txt

看馬,沒有引號或管道。 “設置內容路徑值”。

powershell set-content listf.txt ((get-content list.txt) -replace [char]34)

[char]34 是雙引號:

[int][char]'"'

34

暫無
暫無

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

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