1

(I do not think there is an exact similar match for this question)

I need to make a batch file for Windows (XP and 7) that will:

  • install Python
  • check for any instance of a previous python folder in PATH (typically C:/Python2x, C:/Python3x,C:/Python2x/Scripts, C:/Python3x/Scripts)
  • remove all these folders from PATHs permanently
  • add C:/Python/Scripts and C:/Python permanently in PATH in System/Environment variables

Is this something fairly doable using batch scripts ? I have read in a previous question that I can use setx to set the variable permanently but I am struggling with the matching part.

BlueTrin
  • 9,610
  • 12
  • 49
  • 78
  • 1
    `setx` is not available by default on XP.You should export the PATH with `regedit` and import it again for maximum compatibility. – npocmaka Jan 13 '14 at 13:36

2 Answers2

2
@ECHO OFF
SETLOCAL
SET "newpython=C:\Python;C:\Python\Scripts"
SET "newpath="
:temploop
SET tempfile=%random%%random%%random%
IF EXIST "%temp%\%tempfile%*" GOTO temploop
SET "tempfile=%temp%\%tempfile%"
CALL :showpath >"%tempfile%"
SET "response=x"
FOR /f "delims=" %%p IN ('type "%tempfile%"') DO (
 CALL :addsegment "%%p"
 IF NOT DEFINED response DEL "%tempfile%"&GOTO :EOF 
)
SET "newpath=%newpython%%newpath%
DEL "%tempfile%"
CALL :getresp "Apply new PATH=%newpath% [Y/N/Q]?"
IF /i "%response%"=="Y" ECHO SETX PATH "%newpath%"
GOTO :EOF

:addsegment
SET "segment=%~1"
IF /i "%segment%"=="%segment:python=%" SET response=N&GOTO nosdel
CALL :getresp "Delete %segment% from path [Y/N/Q]?"
:nosdel
IF /i "%response%"=="N" SET "newpath=%newpath%;%segment%"
GOTO :eof

:getresp
SET "response="
SET /p "response=%~1 "
IF /i "%response%"=="Y" GOTO :eof
IF /i "%response%"=="Q" SET "response="&GOTO :eof
IF /i NOT "%response%"=="N" ECHO Please respond Y N or Q to quit&GOTO getresp
GOTO :eof

:showpath
ECHO(%path:;=&ECHO(%
GOTO :eof

Installation of python - well, that's up to you. Don't know where it will be installed from, or to - yor job to find out.

Assuming the new directories are concatenated and separated by ; in variable newpython and noting that directory-segment separators are \ not / (which is used for switches) then the above:

establishes a temporary file
analyses the path, presenting each segment to be deleted or no
re-assembles the path and prefixes the newpython directories.
Asks whether to apply the changes.

I don't know which options you require for the setx, so the command is simply ECHOed. You'd need to remove the ECHO from the SETX line to activate the setting of the path variable.

Note also that SETX does not set the target variable in existing or the current CMD instances - only those created in the future.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Thanks, btw, you may want to use setx /m (the /m should be at the end) – BlueTrin Jan 13 '14 at 14:04
  • Developers should note that this code will merge the system and user paths together, and also replace any environment variable references in the path with the current value. That's not too bad if you're running it on your own system(s) but *please* don't use code like this in installers or anything else that you're going to be distributing to third parties. – Harry Johnston Jan 14 '14 at 01:51
  • **Please DO NOT USE SETX to modify Path**. As pointed out in many other places, it truncates the Path var to 1024 chars, potentially stuffing up your system. My suggestion is to use this command instead: `powershell -Command "[System.Environment]::SetEnvironmentVariable('path', '%newpath%',[System.EnvironmentVariableTarget]::User)"` to replace the SETX line – Dmytro Bugayev Dec 12 '22 at 00:52
1
@echo off
setlocal
setlocal enableDelayedExpansion
set "_PATH_=%PATH:;=";"%"
set "new_path="

for %%P in (%_PATH_%) do (
    set "path_element=%%~P"
    rem :: check if the path element contains "/Python"
    if "!path_element!" equ "!path_element:/Python=!" (
        set new_path="!path_element!";!new_path!
    ) 
)
rem :: add the new directories to path
set new_path=%new_path%;C:/Python/Scripts;C:/Python;
endlocal & new_path=%new_path%

rem :: set the new path
set path=%new_path%

rem :: import the path to the registry
(
echo  Windows Registry Editor Version 5.00
echo  [HKEY_CURRENT_USER\Environment]
echo  "PATH"=%new_path%
) >"%temp%\~path.reg"

REGEDIT /S  "%temp%\~path.reg"
del "%temp%\~path.reg" /q /f

endlocal
endlocal

may not work if there are special symbols in the path like !

npocmaka
  • 55,367
  • 18
  • 148
  • 187