“漂亮的印刷品” windows %PATH% 变量 - 如何在“;”上拆分在 CMD 外壳中
我想在 Windows CMD 提示符下运行一个简单的一行来打印我的 %PATH%
变量,每行一个条目。
我尝试了这个: for /f "delims=;" %a in ("%path%") do echo %a
但这仅打印第一个条目:
Z:\>for /f "delims=;" %a in ("%path%") do echo %a
Z:\>echo c:\python25\.
c:\python25\.
另外,正如您从上面的输出中看到的,这也打印了 echo %a
> 命令以及输出。有什么办法可以阻止这种情况吗?
如果我尝试类似的命令,我会得到所有条目,但仍然会得到 echo %a
输出垃圾邮件结果。我不明白为什么以下内容会打印所有条目,但我对 %PATH%
的尝试却没有。我怀疑我不明白 /F
开关。
Z:\>for %a in (1 2 3) do echo %a
Z:\>echo 1
1
Z:\>echo 2
2
Z:\>echo 3
3
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
简单的方法是使用
This Works for all without
;
in the path and without"
around a single element测试路径=C:\qt\4.6.3\bin;C:\Program Files;C:\documents &设置
但是“始终”的解决方案有点复杂
编辑:现在是一个工作变体
我在那里做了什么?
我试图解决主要问题:引号内的分号应该被忽略,并且只有 正常 分号应该替换为
";"
我使用了批处理解释器本身为我解决这个问题。
;
都替换为^;^;
set var=%var:"=""%"
(缺少的引号是关键!)。其扩展方式使得所有转义字符都将失去其转义插入符:
var=foo &; bar;;baz<>gak;;“半^;^;冒号^;^;^&嵌入”;;又是foo!;;
...但仅限于引号之外,因此现在引号
;;
之外的分号和^;^;
内部的分号之间存在差异。这就是关键。
The simple way is to use
This works for all without
;
in the path and without"
around a single elementTested 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
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.
;
are replaced with^;^;
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
环境变量:如果您的
PATH
等于A;B;C
上面的字符串替换将会改变这到ECHO.A &回声.B& ECHO.C
并一次性执行它。句号可防止出现“ECHO is on”消息。A simple one liner to prettying printing the
PATH
environment variable:If your
PATH
was equal toA;B;C
the above string substitution will change this toECHO.A & ECHO.B & ECHO.C
and execute it all in one go. The full stop prevents the "ECHO is on" messages from appearing.Stephan Quan 非常聪明的单行解决方案的更新:我遇到的问题是尾随分号 - (可能还有两个连续的分号,即空路径元素)会导致出现“ECHO is on”消息。我通过在第二个 ECHO 语句之后立即插入一个句点(这是抑制 ECHO is on/off 消息的语法)解决了这个问题。但是,这会导致额外的空行:
An update to Stephan Quan's very clever one-liner solution: The problem I encountered was that a trailing semi-colon - (and maybe two successive semi-colons, i.e. empty path element) would cause the message "ECHO is on" to appear. I solved this by inserting a period immediately after the second ECHO statement (which is the syntax to suppress ECHO is on/off messages). However, it will result in an extra empty line:
我对 Jeb 聪明的“始终”解决方案进行了一些小改进。目前jeb的解决方案存在以下问题:
以 "" 开头
以“”结尾
那么输出保留“”
此解决方案修复了小问题,并且减少了 2 个替换。此外,我还消除了循环内不必要的重复启用/禁用延迟扩展。 (编辑于2011-10-30简化了ENDLOCAL逻辑)
如果您想看到连续 ;; 产生的每个空路径都有一个空行分隔符,那么 FOR 循环的最后一行可以简单地读取
echo(%%~a
)。或者,使用以下方法将空路径显示为“”可能会更明显:
if %%a=="" (echo "") else echo %%~a
各种空路径修复也适用于 jeb 的简单解决方案。
更新:这是一个使用 JREPL.BAT 的简单单行代码
您可以使用我的 JREPL.BAT 正则表达式文本处理实用程序实现简单、非常强大的解决方案。 JREPL.BAT 是纯脚本(混合 JScript/批处理),可以在 XP 及以上版本的任何 Windows 计算机上本机运行。
I have minor improvements to jeb's clever "always" solution. Currently jeb's solution has the following issues:
starts with ""
ends with ""
then the output preserves the ""
This solution fixes the minor issues, plus it uses 2 fewer substitutions. Also I eliminated the unnecessary repeated enabling/disabling delayed expansion within the loop. (Edit on 2011-10-30 simplified the ENDLOCAL logic)
If you want to see a blank line for each empty path resulting from consecutive ;; delimiters, then the last line of the FOR loop can simply read
echo(%%~a
instead.Or perhaps it would be more obvious to display empty paths as "" using:
if %%a=="" (echo "") else echo %%~a
The various empty path fixes work for jeb's simple solution as well.
UPDATE: Here is a simple one-liner using JREPL.BAT
You can use my JREPL.BAT regular expression text processing utility to achieve a simple, very robust solution. JREPL.BAT is pure script (hybrid JScript/batch) that runs natively on any Windows machine from XP onward.
Stephen Quan 的答案更短更好,但这里有一个 Python 解决方案:
将
;
分号转换为 < code>\n 换行符。Stephen Quan's answer is shorter and better, but here's a Python solution:
Turning
;
semicolons into\n
newlines.这可以在 Windows 上使用 Git Bash 在 cmd 窗口中运行:
echo -e ${PATH//:/\\n}
您还可以在
.bash_profile
中创建一个方便的别名:alias showpath='echo -e ${PATH//:/\\n}'
This works in cmd window using Git Bash on Windows:
echo -e ${PATH//:/\\n}
You can also make a handy alias in your
.bash_profile
:alias showpath='echo -e ${PATH//:/\\n}'
@ROMANIA_engineer 在评论中提出了 PowerShell 解决方案。由于问题要求在 CMD shell 中工作的命令,因此这里有一种从 OP 所需环境中使用优雅代码的方法:
powershell -Command ($env:Path).split(';')powershell -Command ($env :
为了使其更具可读性,您可以添加排序:
Path).split(';') |排序
信用:https://stackoverflow.com/a/34920014/704808
@ROMANIA_engineer proposed a PowerShell solution in a comment. Since the question asks for a command that works in the CMD shell, here is a way to use that elegant code from the OP's desired environment:
powershell -Command ($env:Path).split(';')
To make it still more readable, you can add sorting:
powershell -Command ($env:Path).split(';') | sort
Credit: https://stackoverflow.com/a/34920014/704808
我知道这已经很旧了,但是FWIW;我总是因为某种原因想要这个。前段时间,我给自己写了一个脚本来做到这一点。我对其进行了一些润色并将其发布到我的博客上。
请随意使用它。
它称为 epath,该文件位于 inzi.com。它被编译为 EXE 以便于使用(使用 vbsedit): 此处
您可以在那里下载 exe。如果您希望将其作为 vbs 脚本,请查看该脚本的源代码。
I know this is old, but FWIW; I always run into wanting this for some reason or another. Some time ago, I wrote myself a script to do this. I put a little polish on it and posted it on my blog.
Feel free to use it.
It's called epath, and the file is at inzi.com. It's compiled as an EXE for easy use (using vbsedit): here
You can download the exe there. Here's the source code to the script if you want it as a vbs script.
原始版本
这是一种仅执行多个字符替换的方法,它可以正确处理以分号分隔的可选引用项目的列表,这些项目在引用时甚至可能自己包含分号。空列表项也能正确处理。成功的关键是能够区分引号内的分号(属于列表项的一部分)和引号内的分号(构成分隔符):
改进版本
这是一种执行较少替换的改进方法:
Original Version
This is an approach that only performs several character substitutions, which can properly handle a semicolon-separated list of optionally quoted items, which, when quoted, may even contain semicolons on their own. Empty list items are also dealt with correctly. The key to success is to become able to distinguish between semicolons within quotes (which are part of a list item) and those not (which constitute the separators):
Improved Version
Here is an improved approach that performs less substitutions:
这是一个注释良好的脚本,它解析以分号分隔的项目列表,关于每个项目的可选引用,甚至允许其中包含分号的项目(引用时)。由于它使用了 goto 循环,因此它可能不是最快的方法,但它应该可以安全地防止特殊字符:
Here is a well commented script that parses a semicolon-separated list of items, regarding optional quotation of each item and allowing even items with semicolons in them (when quoted). Since it utilises a
goto
loop it might not be the fastest approach though, but it should be safe against special characters:回到这一点,如果您有
node.js
并受到 Bob Stein 回答 的启发python
单行代码。或者更长的方式:
我需要找到安装了 git 的行,因此:
Returning to this, in the case you have
node.js
and inspired by Bob Stein answer forpython
one-liner.Or longer way:
I needed to find lines where
git
is installed so:另一种实现几乎包含所有控制字符句柄。
警告:
似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了
\x01
或\x04
等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat
echo_path_var.bat:
echo_pathglob_var.bat:
echo_var.bat:
encode_pathlist_chars.bat:
encode_pathlist_chars_glob.bat:
decode_pathlist_chars.bat:
decode_pathlist_chars_glob:
encode_asterisk_char。 bat:
encode_equal_char.bat:
示例:
1.
优点:
for %%i in (...)
语句解析路径列表值以解析VALUE;VALUE
与"VALUE";"VALUE"
相同。;
序列(例如:;;;
)并将其视为单个字符。*
和?
字符)。缺点:
Yet another implementation with almost all control characters handle.
CAUTION:
Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like
\x01
or\x04
) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat
echo_path_var.bat:
echo_pathglob_var.bat:
echo_var.bat:
encode_pathlist_chars.bat:
encode_pathlist_chars_glob.bat:
decode_pathlist_chars.bat:
decode_pathlist_chars_glob:
encode_asterisk_char.bat:
encode_equal_char.bat:
Examples:
1.
Pros:
for %%i in (...)
statement to parse a path list value to parseVALUE;VALUE
the same way as"VALUE";"VALUE"
.;
(ex:;;;
) and treats it a single character.*
and?
characters).Cons:
该脚本将使用 JREPL.bat 将当前 Windows 路径解析为 txt 文件。
它的灵感来自 dbenham 的上述帖子。
This script will parse the current windows path into at txt file using JREPL.bat.
It was inspired by the above post by dbenham.