0

I try to get a root of python executable from %PATH% with a Batch file when I print:

echo %PATH%

I get:

C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\dotnet\;C:\Program Files\Git\cmd;C:\Program Files (x86)\Common Files\Propellerhead Software\ReWire\;C:\Program Files\Common Files\Propellerhead Software\ReWire\;C:\Users\stackoverflow\AppData\Local\Programs\Python\Python37\Scripts\;C:\Users\stackoverflow\AppData\Local\Programs\Python\Python37\;C:\Users\stackoverflow\AppData\Local\Microsoft\WindowsApps;;C:\Users\stackoverflow\AppData\Local\Programs\Microsoft VS Code\bin

I need to filter that, because I want to get:

C:\Users\stackoverflow\AppData\Local\Programs\Python\Python37\

I tried to do this, but it doesn't work only print the same of echo %PATH%

for /f "tokens=* delims=;" %%a IN ("%PATH%") do (
    echo "%%~a"
)

In summary, How I can get the root of python from the string of %PATH% using batch file?

Sho_Lee
  • 149
  • 4
  • 1
    [duplicate](https://stackoverflow.com/questions/21289762/remove-unwanted-path-name-from-path-variable-via-batch), but I think [the accepted answer here](https://stackoverflow.com/questions/670224/how-can-i-extract-a-full-path-from-the-path-environment-variable) might be more useful for you. – Stephan Apr 24 '20 at 19:44
  • 1
    You specified the delimiter `;` but you also defined "return ALL tokens" by `tokens=*`; anyway, `for /F` won't help here, unless the desired path is always at the same position... – aschipfl Apr 25 '20 at 00:22

4 Answers4

1

You need to split up the path variable to one entry per line. That can be done with echo %path:;=&echo %. Catch the correct line with find and put a for loop around to capture the output into a variable:

for /f "delims=" %%a in ('"(echo %path:;=&echo %)|find /i "phyton""') do set "PythonPath=%%a"
echo %PythonPath%
Stephan
  • 53,940
  • 10
  • 58
  • 91
1

thanks to Stephan, I get the answer and it's:

for %%i in ("python.exe") do set root=%%~$PATH:i
set python_path=%root:~0,-11%
echo %python_path%

my output:

C:\Users\stackoverflow\AppData\Local\Programs\Python\Python37
Sho_Lee
  • 149
  • 4
  • 1
    You can combine modifiers, so using `%%~dp$PATH:i` would be preferable. Then you'd need only, optionally remove the trailing backslash, `set python_path=%root:~0,-1%`. If nothing else, it'll save you from having to count your executable file's name string length, every time you copy or modify your code. – Compo Apr 25 '20 at 00:23
1

I think the simplest way is this:

for /F "delims=" %%a in ('where phyton.exe') do echo %%~DPa

but I also wrote this Batch file that gets the path of all applications:

@echo off
setlocal EnableDelayedExpansion

for %%a in ("%PATH:;=" "%") do (
   set "name=%%~a"
   if "!name:~-1!" equ "\" set "name=!name:~0,-1!"
   for %%b in ("!name!") do set "pathOf[%%~Nb]=%%~a"
)

REM SET pathOf

rem For example:    
echo %pathOf[Python37]%
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • In this case, the `where.exe` version, isn't as useful, to the OP as `%~dp$PATH:I`, as `where` also checks the current directory, before locations in `%PATH%`, _and the current directory is not guaranteed to be defined within `%PATH%`, but may also carry a matching exectuable file_. – Compo Apr 25 '20 at 14:43
0

This way you can get what you need:

  for /f "usebackq  tokens=11 delims=;;" %%a in (`"echo %PATH%" ^| find /i "Python"`)    
   do (
         echo "%%~a"
      )
Jbeltran
  • 111
  • 5