“漂亮打印"窗口 %PATH% 变量 - 如何在“;"上拆分在 CMD 外壳中 [英] 'Pretty print' windows %PATH% variable - how to split on ';' in CMD shell

查看:21
本文介绍了“漂亮打印"窗口 %PATH% 变量 - 如何在“;"上拆分在 CMD 外壳中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Windows CMD 提示符下运行一个简单的单行程序来打印我的 %PATH% 变量,每行一个条目.

I want to run a simple one-liner in the Windows CMD prompt to print my %PATH% variable, one entry per line.

我试过这个:for/f "delims=;"%a in ("%path%") do echo %a 但这只会打印第一个条目:

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.

从上面的输出中也可以看出,这也打印了 echo %a 命令和输出.有什么办法可以阻止吗?

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?

如果我尝试一个类似的命令,我会得到所有的条目,但仍然得到 echo %a 输出垃圾邮件结果.我不明白为什么下面会打印所有条目,但我对 %PATH% 的尝试没有.我怀疑我不理解 /F 开关.

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

推荐答案

简单的方法就是使用

for %a in ("%path:;=";"%") do @echo %~a

这适用于所有路径中没有 ; 并且没有 " 围绕单个元素
测试路径=C:qt4.6.3in;C:Program Files;C:documents &设置

This works for all without ; in the path and without " around a single element
Tested with path=C:qt4.6.3in;C:Program Files;C:documents & Settings

但是始终"的解决方案有点复杂
现在是一个工作变体

But a "always" solution is a bit complicated
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 did I do there?
I tried to solve the main problem: that the semicolons inside of quotes should be ignored, and only the normal semicolons should be replaced with ";"

我使用批处理解释器本身为我解决了这个问题.

I used the batch interpreter itself to solve this for me.

  • 首先我必须使字符串安全,转义所有特殊字符.
  • 然后所有的;都换成^;^;
  • 然后诀窍从线开始
    set var=%var:"=""%"(缺少的引号是关键!).
    这以某种方式扩展,使得所有转义字符都将丢失其转义插入符:
    var=foo &bar;;baz<>gak;;"semi^;^;冒号^;^;^&embedded";;再次foo!;;...
    但仅限于引号之外,所以现在引号 ;;^;^; 内的分号是有区别的.
    这就是关键.
  • First I have to make the string safe, escaping all special characters.
  • Then all ; are replaced with ^;^;
  • and then the trick begins with the line
    set var=%var:"=""%" (The missing quote is the key!).
    This expands in a way such that all escaped characters will 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.

这篇关于“漂亮打印"窗口 %PATH% 变量 - 如何在“;"上拆分在 CMD 外壳中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆