Search code examples
cmdpath

How to extract the elements of the path variable that contain a specific word


I want to come up with a command that searches my path variable and outputs the folders that include the search keyword. For example: I want to output all paths included in my path variable that include the word 'python' (case insensitive).

My path looks like this:

C:\Program Files\ConEmu\ConEmu\Scripts;C:\Program Files\ConEmu;C:\Program Files\ConEmu\ConEmu;C:\Program Files\AdoptOpenJDK\jre-11.0.4.11-hotspot\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Microsoft VS Code\bin;C:\Program Files(x86)\PDFtk\bin\;C:\Program Files\ffmpeg\bin;C:\Program Files\ffmpeg\avconv\bin;C:\Program Files\ffmpeg\curl\bin;C:\Program Files\nodejs\;C:\Program Files\Git\cmd;C:\Program Files(x86)\Gpg4win\..\GnuPG\bin;C;C:\Users\jc\AppData\Local\Programs\Python\Python38-32\Scripts\;C:\Users\jc\AppData\Local\Programs\Python\Python38-32\;C:\Users\jc\AppData\Roaming\npm;C:\Program Files\OpenSSH;C:\Users\jc\.ssh;C:\Program Files(x86)\Nmap;C:/Users/jc/AppData/Local/Programs/Python/Python38-32/python.exe;

Expected result would be:

C:\Users\jc\AppData\Local\Programs\Python\Python38-32\Scripts\;C:\Users\jc\AppData\Local\Programs\Python\Python38-32\;C:/Users/jc/AppData/Local/Programs/Python/Python38-32/python.exe;

So far, I have a regex formula that selects every path (see example here). The regex I have is:

C.*?;\gmi

If I try path | findstr "C.*?;"

Nothing really comes out (shouldn't it output all lines that match that regex?). Also, how would I go about outputting the paths that include the intended search keyword? Thanks


Solution

  • This will do the needful:

    for %A IN ("%PATH:;=" "%") DO @ECHO=%~A | FINd /I "Python"