Programmatically update a folder in PATH from a batch file

≯℡__Kan透↙ 提交于 2020-01-15 10:55:10

问题


(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.


回答1:


@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.




回答2:


@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 !



来源:https://stackoverflow.com/questions/21090435/programmatically-update-a-folder-in-path-from-a-batch-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!