';漂亮的印花';windows%PATH%变量-如何在';上拆分';在cmdshell中

';漂亮的印花';windows%PATH%变量-如何在';上拆分';在cmdshell中,windows,batch-file,cmd,path,Windows,Batch File,Cmd,Path,我想在Windows CMD提示符中运行一个简单的一行程序来打印我的%PATH%变量,每行一个条目 我尝试过这样做:对于/f“delims=;”%a in(“%path%”)执行echo%a,但这只打印第一个条目: Z:\>for /f "delims=;" %a in ("%path%") do echo %a Z:\>echo c:\python25\. c:\python25\. 正如您从上面的输出中所看到的,这也打印了echo%a命令以及输出。有没有办法阻止这一切 如果我

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

我尝试过这样做:
对于/f“delims=;”%a in(“%path%”)执行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

简单的方法是使用

for %a in ("%path:;=";"%") do @echo %~a
这适用于所有没有
的用户且在单个元素周围没有

测试路径为C:\qt\4.6.3\bin;C:\Program Files;C:\documents&Settings

但“始终”解决方案有点复杂
编辑:现在是一个工作变量

@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
)
@echo关闭
setlocal DisableDelayedExpansion
设置“var=foo&bar;巴扎克;“分号;冒号;^&embedded”;又是福!;加一些括号;“无与伦比;-)”;(也是)
设置“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:”;“=”;“%”
设置“var=%var:”“=”%
setlocal EnableDelayedExpansion
对于(“!var!”)中的%%a,请执行以下操作(
端部
回声%%~a
setlocal EnableDelayedExpansion
)
我在那里做了什么?
我试图解决主要问题:引号中的分号应该被忽略,只有普通的分号应该被替换为
“;”

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

  • 首先,我必须使字符串安全,转义所有特殊字符
  • 然后所有的
    被替换为
    ^;^;
  • 然后,技巧从线条开始
    set var=%var:“=”“%”
    (缺少的引号是关键!)。
    这将以一种方式扩展,使所有转义字符都将丢失其转义插入符号:
    var=foo&bar;;bazgak;;“半^;^;冒号^;^;^;^;^;^嵌入”;;再次foo!;

    但是只在引号之外,所以现在引号之外的分号和引号之内的分号有区别了
    这是关键

我对jeb聪明的“始终”解决方案有一些小的改进。目前,jeb的解决方案存在以下问题:

  • 如果前导路径用引号括起,则第一个输出 以“”开头
  • 如果尾随路径用引号括起,则最后一个输出 以“”结尾
  • 如果任何路径包含无害但不起作用的连续“”, 然后,输出将保留“”
  • 如果var包含连续的;;分隔符,则输出ECHO关闭
  • 这个解决方案解决了一些小问题,另外它使用了2个替换。我还消除了循环中不必要的重复启用/禁用延迟扩展。(2011年10月30日编辑简化了ENDLOCAL逻辑)


    我知道这很老了,但我总是出于某种原因想要这个。不久前,我给自己写了一个脚本来做这个。我对它稍加润色,并将它发布在我的博客上

    请随意使用它

    它名为epath,文件位于inzi.com。它被编译为EXE,便于使用(使用vbsedit):

    你可以在那里下载exe。如果你想把它作为vbs脚本,这里是脚本的源代码

        scriptname = Wscript.ScriptName 'objFSO.GetFileName(WScript.FullName)
    
        Function BubbleSort(arrData,strSort)
        'borrowed from here: http://vbscripter.blogspot.com/2008/03/q-how-do-i-sort-data-in-array.html
    
        'Input: arrData = Array of data.  Text or numbers.
        'Input: strSort = Sort direction (ASC or ascending or DESC for descending)
        'Output: Array
        'Notes: Text comparison is CASE SENSITIVE
        '        strSort is checked for a match to ASC or DESC or else it defaults to Asc
    
    
            strSort = Trim(UCase(strSort))
            If Not strSort = "ASC" And Not strSort = "DESC" Then
                strSort = "ASC"
            End If 
    
            For i = LBound(arrData) to UBound(arrData)
              For j = LBound(arrData) to UBound(arrData)
                If j <> UBound(arrData) Then
                    If strSort = "ASC" Then
                      If UCase(arrData(j)) > UCase(arrData(j + 1)) Then
                         TempValue = arrData(j + 1)
                         arrData(j + 1) = arrData(j)
                         arrData(j) = TempValue
                      End If
                    End If
    
                    If strSort = "DESC" Then
                        If UCase(arrData(j)) < UCase(arrData(j + 1)) Then
                            TempValue = arrData(j + 1)
                            arrData(j + 1) = arrData(j)
                            arrData(j) = TempValue
                         End If        
                    End If 
                End If
              Next
            Next
    
            BubbleSort = arrData
    
        End Function
    
        If Wscript.Arguments.Count>0 Then
    
            Set args = Wscript.Arguments
    
            bInLines = False
            bInAlphabetical = False
            bReverseSort = False
            bShowHelp = False
    
            For Each arg In args
                Select Case arg
                    Case "-l"
                        bInLines = True
                    Case "-a"
                        bInAlphabetical = True
                    Case "-r"
                        bReverseSort = True
                    Case Else
                        bShowHelp=True
                End Select  
    
            Next
    
            If bInLines = False Then
                bShowHelp=True
            End if
    
            If bShowHelp Then
    
                        sTxt = sTxt + "" & vbCrLf
                        sTxt = sTxt +  scriptname  & " Displays the system path in optionally friendly formats." & vbCrLf
                        sTxt = sTxt +  "ePath is helpful when viewing the system path and easily identifying folders therein." & vbCrLf
                        sTxt = sTxt + "" & vbCrLf
                        sTxt = sTxt + "EPATH [-l] [-a] [-r]" & vbCrLf
                        sTxt = sTxt + "" & vbCrLf
                        sTxt = sTxt + "Switches:" & vbCrLf
                        sTxt = sTxt + vbTab + "[-l]" + vbtab + "Show the path broken out in lines" & vbCrLf
                        sTxt = sTxt + vbtab + "[-a]" + vbTab + "Sort the path broken out in lines sorted alphabetically" & vbCrLf
                        sTxt = sTxt + vbtab + "[-r]" + vbTab + "Reverse the alphabetic sort [asc default] (ignored without -a)" & vbCrLf
                        sTxt = sTxt + "" & vbCrLf
                        sTxt = sTxt + vbTab + "Examples:" & vbCrLf
                        sTxt = sTxt +  vbTab + vbTab + scriptname  & vbTab & "(Show %PATH% normally)" & vbCrLf
                        sTxt = sTxt +  vbTab + vbTab + scriptname  & " -l" & vbCrLf
                        sTxt = sTxt +  vbTab + vbTab + scriptname  & " -l -a" & vbCrLf
                        sTxt = sTxt +  vbTab + vbTab + scriptname  & " -l -a -r" & vbCrLf
                        sTxt = sTxt +  vbTab + vbTab + scriptname  & " -? Display help (what you are seeing now)" & vbCrLf
                        sTxt = sTxt + "" & vbCrLf
                        sTxt = sTxt + "More info or questions at http://inzi.com" & vbCrLf
    
    
                        Wscript.Echo sTxt
    
                        WScript.Quit
    
            Else
                Set wshShell = CreateObject( "WScript.Shell" )
                sPath = wshShell.ExpandEnvironmentStrings( "%PATH%" )
                thePath = Split(sPath,";")
    
                If bInAlphabetical Then
                    If bReverseSort Then
                        sDirection = "DESC"
                    End If
    
                    thePath = BubbleSort(thePath, sDirection)
                End if
    
    
                For Each item In thePath
                    WScript.Echo item
                Next
                Set wshShell = Nothing
            End if
        Else
            'Nothing, echo the path.
    
            Set wshShell = CreateObject( "WScript.Shell" )
            WScript.Echo wshShell.ExpandEnvironmentStrings( "%PATH%" )
            Set wshShell = Nothing
    
        End If
    
    scriptname=Wscript.scriptname'objFSO.GetFileName(Wscript.FullName)
    函数BubbleSort(arrData,strSort)
    "从这里借来:http://vbscripter.blogspot.com/2008/03/q-how-do-i-sort-data-in-array.html
    '输入:arrData=数据数组。文本或数字。
    '输入:strSort=排序方向(ASC或升序或DESC表示降序)
    '输出:数组
    '注意:文本比较区分大小写
    'strSort检查是否与ASC或DESC匹配,否则默认为ASC
    strSort=修剪(UCase(strSort))
    如果不是strSort=“ASC”也不是strSort=“DESC”,则
    strSort=“ASC”
    如果结束
    对于i=LBound(arrData)到UBound(arrData)
    对于j=LBound(arrData)到UBound(arrData)
    如果j UBound(arrData),则
    如果strSort=“ASC”,则
    如果UCase(arrData(j))>UCase(arrData(j+1)),那么
    TempValue=arrData(j+1)
    arrData(j+1)=arrData(j)
    arrData(j)=温度值
    如果结束
    如果结束
    如果strSort=“DESC”,则
    如果UCase(arrData(j))0,则
    Set args=Wscript.Arguments
    bInLines=False
    二进制参数=假
    bReverseSort=False
    bShowHelp=False
    对于args中的每个arg
    选择Case arg
    案例“-l”
    bInLines=True
    案例“-a”
    二进制参数=真
    案例“-r”
    bReverseSort=True
    其他情况
    bShowHelp=True
    结束选择
    下一个
    如果bInLines=False,则
    bShowHelp=True
    如果结束
    如果你需要帮助的话
    sTxt=sTxt
    
    jrepl "([^;\q]+|\q.*?(\q|$))+" $0 /x /jmatch /s path
    
        scriptname = Wscript.ScriptName 'objFSO.GetFileName(WScript.FullName)
    
        Function BubbleSort(arrData,strSort)
        'borrowed from here: http://vbscripter.blogspot.com/2008/03/q-how-do-i-sort-data-in-array.html
    
        'Input: arrData = Array of data.  Text or numbers.
        'Input: strSort = Sort direction (ASC or ascending or DESC for descending)
        'Output: Array
        'Notes: Text comparison is CASE SENSITIVE
        '        strSort is checked for a match to ASC or DESC or else it defaults to Asc
    
    
            strSort = Trim(UCase(strSort))
            If Not strSort = "ASC" And Not strSort = "DESC" Then
                strSort = "ASC"
            End If 
    
            For i = LBound(arrData) to UBound(arrData)
              For j = LBound(arrData) to UBound(arrData)
                If j <> UBound(arrData) Then
                    If strSort = "ASC" Then
                      If UCase(arrData(j)) > UCase(arrData(j + 1)) Then
                         TempValue = arrData(j + 1)
                         arrData(j + 1) = arrData(j)
                         arrData(j) = TempValue
                      End If
                    End If
    
                    If strSort = "DESC" Then
                        If UCase(arrData(j)) < UCase(arrData(j + 1)) Then
                            TempValue = arrData(j + 1)
                            arrData(j + 1) = arrData(j)
                            arrData(j) = TempValue
                         End If        
                    End If 
                End If
              Next
            Next
    
            BubbleSort = arrData
    
        End Function
    
        If Wscript.Arguments.Count>0 Then
    
            Set args = Wscript.Arguments
    
            bInLines = False
            bInAlphabetical = False
            bReverseSort = False
            bShowHelp = False
    
            For Each arg In args
                Select Case arg
                    Case "-l"
                        bInLines = True
                    Case "-a"
                        bInAlphabetical = True
                    Case "-r"
                        bReverseSort = True
                    Case Else
                        bShowHelp=True
                End Select  
    
            Next
    
            If bInLines = False Then
                bShowHelp=True
            End if
    
            If bShowHelp Then
    
                        sTxt = sTxt + "" & vbCrLf
                        sTxt = sTxt +  scriptname  & " Displays the system path in optionally friendly formats." & vbCrLf
                        sTxt = sTxt +  "ePath is helpful when viewing the system path and easily identifying folders therein." & vbCrLf
                        sTxt = sTxt + "" & vbCrLf
                        sTxt = sTxt + "EPATH [-l] [-a] [-r]" & vbCrLf
                        sTxt = sTxt + "" & vbCrLf
                        sTxt = sTxt + "Switches:" & vbCrLf
                        sTxt = sTxt + vbTab + "[-l]" + vbtab + "Show the path broken out in lines" & vbCrLf
                        sTxt = sTxt + vbtab + "[-a]" + vbTab + "Sort the path broken out in lines sorted alphabetically" & vbCrLf
                        sTxt = sTxt + vbtab + "[-r]" + vbTab + "Reverse the alphabetic sort [asc default] (ignored without -a)" & vbCrLf
                        sTxt = sTxt + "" & vbCrLf
                        sTxt = sTxt + vbTab + "Examples:" & vbCrLf
                        sTxt = sTxt +  vbTab + vbTab + scriptname  & vbTab & "(Show %PATH% normally)" & vbCrLf
                        sTxt = sTxt +  vbTab + vbTab + scriptname  & " -l" & vbCrLf
                        sTxt = sTxt +  vbTab + vbTab + scriptname  & " -l -a" & vbCrLf
                        sTxt = sTxt +  vbTab + vbTab + scriptname  & " -l -a -r" & vbCrLf
                        sTxt = sTxt +  vbTab + vbTab + scriptname  & " -? Display help (what you are seeing now)" & vbCrLf
                        sTxt = sTxt + "" & vbCrLf
                        sTxt = sTxt + "More info or questions at http://inzi.com" & vbCrLf
    
    
                        Wscript.Echo sTxt
    
                        WScript.Quit
    
            Else
                Set wshShell = CreateObject( "WScript.Shell" )
                sPath = wshShell.ExpandEnvironmentStrings( "%PATH%" )
                thePath = Split(sPath,";")
    
                If bInAlphabetical Then
                    If bReverseSort Then
                        sDirection = "DESC"
                    End If
    
                    thePath = BubbleSort(thePath, sDirection)
                End if
    
    
                For Each item In thePath
                    WScript.Echo item
                Next
                Set wshShell = Nothing
            End if
        Else
            'Nothing, echo the path.
    
            Set wshShell = CreateObject( "WScript.Shell" )
            WScript.Echo wshShell.ExpandEnvironmentStrings( "%PATH%" )
            Set wshShell = Nothing
    
        End If
    
    ECHO.%PATH:;= & ECHO.%
    
    ECHO %PATH:;= & ECHO.%
    
    python -c "import os; print os.environ['PATH'].replace(';', '\n');"
    
    @ECHO OFF
    SETLOCAL ENABLEDELAYEDEXPANSION
    
    :::: THIS SCRIPT WILL PARSE THE CURRENT WINDOWS PATH INTO AT TXT FILE ::::
    :::: EACH FOLDER FOUND IN PATH WILL BE ON A SEPARATE LINE ::::
    
    :::: SCRIPT INSTRUCTIONS ::::
    :: PLACE JREPL.bat IN C:\WINDOWS\SYSTEM32 FOLDER OR CUSTOM LOCATION OF YOUR CHOOSING ::
    :: IF PLACED IN CUSTOM FOLDER YOU MUST LINK IT TO WINDOWS PATH ::
    :: YOU CAN ACCESS WINDOWS PATH BY RUNNING THE BELOW COMMAND IN CMD.EXE ::
    :: Rundll32 sysdm.cpl,EditEnvironmentVariables ::
    :: DOWNLOAD JREPL.bat https://www.dostips.com/forum/viewtopic.php?t=6044 ::
    
    :: SET WORKING DIRECTORY ::
    CD /D "C:\WINDOWS\SYSTEM32"
    
    :: UNCOMMENT LINE BELOW AND SET YOUR JREPL.bat SAVED FOLDER PATH IF YOU HAVE A BACKUP COPY ::
    :: SET JOUT=<FOLDER PATH>
    
    :: SET OUTPUT FILE ::
        SET FOUT=%USERPROFILE%\DESKTOP\PARSE.TXT
    
    :: SET FILE TO SEARCH FOR ::
    :: THIS SEARCHES FOR JREPL.BAT IN THE CURRENT WORKING DIR ::
        SET I=JREPL.BAT
    
    :: SET CONTROL FILE TO CHECK AGAINST ::
    :: THIS IS FOR DEBUGGING PURPOSES AND SHOULD NOT BE CHANGED OTHERWISE ::
        SET J=JREPL.BAT
    
    :::: START SCRIPT ::::
        SET RESULT=
        FOR /F "DELIMS=" %%A IN ('DIR /B /O:N ^| FINDSTR /S /I "%I%" %TMP_RESULT_FILE%') DO (
        SET RESULT=%%A
        )
    
    IF /I !RESULT! EQU %J% (
    ECHO !RESULT! ^^!^^!EXISTS^^!^^!
        TIMEOUT 3 >NUL
        CALL :FOUND
        GOTO END
    ) ELSE (
        GOTO NOTFOUND
    )
        GOTO ERROR
    
    :FOUND
        FOR %%G IN (%I%) DO (
        %%G "([^;\Q]+|\Q.*?(\Q|$))+" $0 /X /JMATCH /S PATH>>%FOUT%
        CLS && ECHO.
    ECHO %I% ^^!^^!EXECUTED SUCCESSFULLY^^!^^!
        TIMEOUT /T 3 >NUL
        EXPLORER "%FOUT%"
        GOTO END
    ( ELSE )
        GOTO ERROR
    )
    
    :NOTFOUND
        ECHO %I% ^^!^^!NOT FOUND^^!^^!
        TIMEOUT 3 >NUL
        CLS && ECHO.
    :: UNCOMMENT THE LINES BELOW TO OPEN JREPL.BAT SAVE FOLDER IF AVAILABLE ::
        :: ECHO ^^!^^!OPENING^^!^^! %I%'S SAVED FOLDER
      :: TIMEOUT 3 >NUL
        :: EXPLORER "%JOUT%"
      GOTO END
        ( ELSE )
        GOTO ERROR
        )
    
    :ERROR
        CLS && ECHO.
        ECHO ^^!^^!ERROR RUNNING^^!^^! %I%
        TIMEOUT 3 >NUL
        CLS && ECHO.
        ECHO ^^!^^!PLEASE FIX SCRIPT^^!^^! ::::::::::::::::::
        TIMEOUT 3 >NUL
    
    :END
        ENDLOCAL && EXIT /B