'pretty打印“窗口%PATH%变量 - 如何分割上”;“在CMD壳 [英] 'Pretty print' windows %PATH% variable - how to split on ';' in CMD shell
问题描述
我想运行在Windows CMD提示一个简单的一行打印我的%PATH%变量,每行一个条目。
我试过这样: FOR / Fdelims =;在(%PATH%)%一做回声%A
但这只是打印的第一项:
Z:\\> FOR / Fdelims =;在(%PATH%)做回声%A%一Z:\\>回声C:\\ python25 \\。
C:\\ python25 \\。
另外,你可以从上面的输出看,这也是印刷回声%,A
命令以及输出。有没有什么办法阻止呢?
如果我尝试了类似的命令,我让所有的条目,但仍获得了回声%,A
输出垃圾邮件的结果。我不明白为什么以下打印所有条目,但我对%PATH%的尝试没有。我怀疑我不明白/ F开关。
Z:\\>在(1 2 3)做回声%A%一Z:\\>回声1
1Z:\\>回声2
2Z:\\>回声3
3
最简单的方法是使用
在一个%(%路径:=,%)做@echo%〜一
这适用于所有无;
路径和无在一个单一的元素结果
C:\\ QT \\ 4.6.3 \\ BIN:\\ Program Files文件; C:与路径= C测试的\\文件和放大器;设置
但是,一个永远解决方案是一个有点复杂结果
修改:现在工作的变种
关闭@echo
SETLOCAL DisableDelayedExpansion
设置VAR =富&放大器;酒吧;巴兹<>珏;半;冒号; ^&安培;嵌入式再富;!掷(中)一些(括号内);无与伦比;-)(太设置VAR =%变种:=,%
设置VAR =%VAR:^ = ^^%
设置VAR =%VAR:放大器; = ^&安培;%
设置VAR =%VAR:| = ^ |%
设置VAR =%变种:其中= ^&下;%
设置VAR =%VAR:> = ^>%设置VAR =%VAR:= ^,^;%
REM **这是重点线,缺少的报价意
组VAR =%VAR:=%
设置VAR =%变种:=,%设置VAR =%VAR:;; =;%
设置VAR =%VAR:^; ^ =;%
设置VAR =%VAR:=%
设置VAR =%变种:=,%
设置VAR =%变种:,=,%
设置VAR =%VAR:=%SETLOCAL EnableDelayedExpansion
对于一个%%的(!VAR!)做(
ENDLOCAL
呼应%%〜一
SETLOCAL EnableDelayedExpansion
)
我到底做了什么呢?结果
我试图要解决的主要问题,即引号内的分号应被忽略,只有的正常的分号应改为,
我用的是批间preTER本身来解决这个我。结果
首先,我必须使该字符串的安全的,转义所有特殊字符。结果
然后,所有';'被替换为 ^; ^;
,然后招开始在该行组VAR =%VAR:=,%
(缺少的报价是关键!)。结果
这扩大的方式,所有的转义字符都失去了他们的逃命插入符号结果 VAR =富&安培;酒吧;;巴兹<>珏;;半^; ^;冒号^; ^; ^&安培;嵌入式!;;富再次;;
...结果
但是,只有外面的行情,所以现在有 ;;
和里面的 ^引号之外分号之间的差; ^;
。结果
这就是关键。
I want to run a simple one-liner in the Windows CMD prompt to print my %PATH% variable, one entry per line.
I tried this: for /f "delims=;" %a in ("%path%") do echo %a
but this only prints the first entry:
Z:\>for /f "delims=;" %a in ("%path%") do echo %a
Z:\>echo c:\python25\.
c:\python25\.
Also as you can see from the output above, this is also printing the echo %a
command as well as the output. Is there any way to stop this?
If I try a similar command, I get all the entries, but still get the echo %a
output spamming the results. I don't understand why the following prints all entries, but my attempt on %PATH% doesn't. I suspect I don't understand the /F switch.
Z:\>for %a in (1 2 3) do echo %a
Z:\>echo 1
1
Z:\>echo 2
2
Z:\>echo 3
3
The simple way is to use
for %a in ("%path:;=";"%") do @echo %~a
This works for all without ;
in the path and without "
around a single element
Tested with path=C:\qt\4.6.3\bin;C:\Program Files;C:\documents & Settings
But a "always" solution is a bit complicated
EDIT: Now a working variant
@echo off
setlocal DisableDelayedExpansion
set "var=foo & bar;baz<>gak;"semi;colons;^&embedded";foo again!;throw (in) some (parentheses);"unmatched ;-)";(too"
set "var=%var:"=""%"
set "var=%var:^=^^%"
set "var=%var:&=^&%"
set "var=%var:|=^|%"
set "var=%var:<=^<%"
set "var=%var:>=^>%"
set "var=%var:;=^;^;%"
rem ** This is the key line, the missing quote is intended
set var=%var:""="%
set "var=%var:"=""%"
set "var=%var:;;="";""%"
set "var=%var:^;^;=;%"
set "var=%var:""="%"
set "var=%var:"=""%"
set "var=%var:"";""=";"%"
set "var=%var:"""="%"
setlocal EnableDelayedExpansion
for %%a in ("!var!") do (
endlocal
echo %%~a
setlocal EnableDelayedExpansion
)
What the hell I do there?
I try to solve the main problem, that the semicolons inside of quotes should be ignored, and only the normal semicolons should be replaced with ";"
I use the batch interpreter itself to solve this for me.
First I have to make the string safe, escaping all special characters.
Then all ';' are replaced with ^;^;
and then the trick begins in the line set var=%var:"=""%"
(The missing quote is the key!).
This expands in a way that all escaped characters are lose their escape caret
var=foo & bar;;baz<>gak;;"semi^;^;colons^;^;^&embedded";;foo again!;;
...
But only outside of the quotes, so now there is a difference between semicolons outside of quotes ;;
and inside ^;^;
.
Thats the key.
这篇关于'pretty打印“窗口%PATH%变量 - 如何分割上”;“在CMD壳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!