我怎样才能提取PATH环境变量的完整路径? [英] How can I extract a full path from the PATH environment variable?

查看:135
本文介绍了我怎样才能提取PATH环境变量的完整路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提取从 PATH 环境变量与本地CMD工具的完整路径。考虑下面的<​​code> PATH 内容:

I want to extract a full path from the PATH environment variable with native cmd tools. Consider the following PATH content:

C:\\ Program Files文件\\ Windows资源工具包\\工具\\; C:\\ Perl的\\网站\\ BIN; C:\\ Perl的\\ BIN; C:\\ WI
  NDOWS \\ SYSTEM32; C:\\ WINDOWS; C:\\ WINDOWS \\ SYSTEM32 \\ WBEM; C:\\ Program Files文件\\微软SQ
  l认证服务器\\ 90 \\ TOOLS \\ BINN \\; C:\\ WINDOWS \\ SYSTEM32 \\ WindowsPowerShell \\ V1.0; C:\\ PROGRAM网络
  莱\\ Microsoft SQL Server的\\ 80 \\ TOOLS \\ BINN \\; C:\\ Program Files文件\\ Microsoft SQL Server的\\ 10
  0 \\ DTS \\ BINN \\; C:\\ Program Files文件\\ Microsoft SQL Server的\\ 100 \\ TOOLS \\ BINN \\; C:\\程序FIL
  ES \\ NMAP \\; C:\\ Program Files文件\\ WinRAR的\\; C:\\ Program Files文件\\ QuickTime的\\ QTSystem \\; C:\\ PROGRA
  M档\\的Hydra-5.4-WIN \\; C:\\ Program Files文件\\ john1701 \\运行; C:\\挖;; C:\\ cygwin的; C:\\ WAMP \\ b
  在\\ MySQL的\\ mysql5.0.45 \\ BIN; C:\\ Program Files文件\\的MySQL \\ MySQL服务器5.0 \\ BIN; C:\\ PROGRAM
  文件\\ Tail4win; C:\\ Program Files文件\\眺望Fing头1.1 \\ BIN

C:\Program Files\Windows Resource Kits\Tools\;C:\Perl\site\bin;C:\Perl\bin;C:\WI NDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;c:\Program Files\Microsoft SQ L Server\90\Tools\binn\;C:\WINDOWS\system32\WindowsPowerShell\v1.0;C:\Program Fi les\Microsoft SQL Server\80\Tools\Binn\;C:\Program Files\Microsoft SQL Server\10 0\DTS\Binn\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\program fil es\nmap\;C:\Program Files\WinRAR\;C:\Program Files\QuickTime\QTSystem\;C:\Progra m Files\hydra-5.4-win\;C:\Program Files\john1701\run;C:\dig;;C:\cygwin;C:\wamp\b in\mysql\mysql5.0.45\bin;C:\Program Files\MySQL\MySQL Server 5.0\bin;C:\Program Files\Tail4win;C:\Program Files\Overlook Fing 1.1\bin

我想只提取以下路径:

C:\\ Program Files文件\\的MySQL \\ MySQL服务器5.0 \\ BIN;

C:\Program Files\MySQL\MySQL Server 5.0\bin;

能够这样的事情?

推荐答案

您可以使用来标记; 但你需要采取的具有路径护理; 在他们(因此需要引号)。总之,我会说你构建pretty多code在这一点上pretty脆弱的解决方案。

You can use for to tokenize at ; but you need to take care of paths that have a ; in them (and thus need quotes). All in all I'd say you'd build a pretty brittle solution with pretty much code at this point.

如果你想知道某个可执行文件,然后

If you want to know where a certain executable is, then

for %%i in ("mysql.exe") do @echo.%%~$PATH:i

会告诉你(或没有,如果它是无处在 PATH )。

更新:确定,我知道了。一个讨厌的小批处理文件如下:

UPDATE: Ok, I got it. One nasty little batch file follows:

@echo off
setlocal enabledelayedexpansion enableextensions
set p=%PATH%
:loop
for %%i in ("notepad.exe") do call :setvar "%%~$p:i"
if not :%x%:==:: (call :clearpath & goto loop)
goto :eof

:setvar
    set x=%~1
goto :eof

:clearpath
    echo %x%
    for %%x in ("!x!") do set d=%%~dpx
    set d=!d:~,-1!
    set p=!p:%d%=!
goto :eof

这将打印从 PATH ,其中的notepad.exe 被发现(第一个节目,我知道了所有的匹配路径我的头顶,将在这里两个地方)。相应地调整你的问题。

This will print all matching paths from PATH where notepad.exe was found (the first program I know off the top of my head to be in two places here). Adapt accordingly for your problem.

:的ClearPath 只需从变量删除找到的路径,然后我们再尝试匹配,直到没有匹配左

:clearpath simply deletes the found path from the variable and then we try matching again, until no match is left.

不过,这仍然是非常非pretty。

That said, this is still very un-pretty.

这篇关于我怎样才能提取PATH环境变量的完整路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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