编程从一个批处理文件更新PATH的文件夹 [英] Programmatically update a folder in PATH from a batch file
问题描述
(我不认为这是一个确切的相似匹配这个问题)
我需要为Windows的批处理文件(XP和7),将:
- 安装Python
- 为您在PATH环境变量的previous蟒蛇文件夹中的任何实例(通常是C:/ Python2x,C:/ Python3x,C:/ Python2x /脚本,C:/ Python3x /脚本)
- 从如果PATH中删除所有这些文件夹永久
- 添加C:/ Python的/脚本和C:/ Python的永久在系统/环境变量PATH
这是不是很可行使用批处理脚本?我已经在previous问题看,我可以使用 SETX
来永久设置变量,但我与匹配的部分努力。
@ECHO OFF
SETLOCAL
SETnewpython = C:\\的Python,C:\\ Python的\\脚本
SET的新路径=
:temploop
SET临时文件=%随机%%随机%%随机%
如果不存在%temp%\\%临时文件%*GOTO temploop
SET临时文件=%TEMP%\\%临时文件%
CALL:showpath>中%临时文件%
SET响应= X
FOR / Fdelims =%% IN('键入%临时文件%')DO P(
CALL:addsegment%% P
如果没有定义响应DEL%临时文件%&放大器; GOTO:EOF
)
SET的新路径=%newpython %%的新路径%
DEL%临时文件%
CALL:getresp应用新的PATH =%的新路径%[Y / N / Q]?
IF / I%的响应%==YECHO SETX PATH%的新路径%
GOTO:EOF:addsegment
SET段=%〜1
IF / I%段%==%段:蟒蛇=%SET响应= N&放大器; GOTO nosdel
CALL:getresp从路径删除%段%[Y / N / Q]?
:nosdel
IF / I%的响应%==NSET的新路径=%的新路径%;%段%
GOTO:EOF:getresp
SET响应=
SET / p的响应=%〜1
IF / I%的响应%==YGOTO:EOF
IF / I%的响应%==QSET响应=&放大器; GOTO:EOF
IF / NOT%响应%==NECHO请回答y N或Q退出&安培; GOTO getresp
GOTO:EOF:showpath
ECHO(%路径:=&安培; ECHO(%
GOTO:EOF
蟒安装 - 嗯,这是给你的。不知道它会从被安装,或者 - 尤尔的工作,找出
假设新的目录被连接起来,并通过<大骨节病>分开; 骨节病>变量 newpython
并注意到目录段分隔符是<大骨节病> \\ 大骨节病>不是<大骨节病> / 骨节病>(这是用于开关),那么上面的:
建立一个临时文件搜索
分析了路径
,presenting要删除的每个段或无结果
重新组装的路径和prefixes的 newpython
目录。结果
询问是否应用更改。
我不知道你需要为 SETX
的选项,因此命令仅仅是 ECHO
编辑。你需要删除 ECHO
从 SETX
在线激活的设置路径
变量。
还要注意的是 SETX
做的不可以设置在现有的或当前的目标变量 CMD
实例 - 只有那些在将来创建
(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.
@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 ECHO
ed. 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.
这篇关于编程从一个批处理文件更新PATH的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!