从批量添加/删除路径? [英] Add/Remove from Path using Batch?

查看:185
本文介绍了从批量添加/删除路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要有两个批处理文件 install.bat uninstall.bat 我的命令行程序 program.exe



我想要$ code install.bat 将当前位置 program.exe 添加到系统路径环境变量中。 p>

然后我想要 uninstall.bat 删除任何路径到 program.exe 从系统路径环境变量。



有关如何做的任何想法?

解决方案

也许此前的解决方案将会得到帮助。



修改后的文件可以根据您的情况进行定制

 SETnewpath =
:temploop
SET tempfile =%random %% running dom %% random%
IF EXIST%temp%\%tempfile%*GOTO temploop
SETtempfile =%temp%\%tempfile%
CALL:showpath> %tempfile%
::此部分从路径中删除当前目录
FOR / fdelims =%% p IN('type%tempfile%')DO(
CALL:addsegment%% p

DEL%tempfile%
IF / i%1==/ u(SETnewpath =%newpath:〜 1%)ELSE(SETnewpath =%batchdir %% newpath%)
CALL:getresp应用新的PATH =%newpath%[Y / N / Q]?
IF / i%response%==YECHO SETX PATH%newpath%
GOTO:EOF

:addsegment
SETsegment = %〜1
IF / i NOT%segment%==%batchdir%SETnewpath =%newpath%;%segment%
GOTO:eof

:getresp
SETresponse =
SET / presponse =%〜1
IF / i%response%==YGOTO:eof
IF / i%response%==QSETresponse =& GOTO:eof
IF / i NOT%response%==NECHO请回复YN或Q退出& GOTO getresp
GOTO:eof

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

基本上两个批次是一样的 - 唯一的区别是对于 INSTALL 版本,该目录被添加到该路径。



因此,我简单地确定它,以便 thisbatch 将安装文件, thisbatch / u 将卸载它。



自然地,调用例程要获得最后的确定来更改路径



我不知道setx需要哪些选项,所以命令只是ECHOed。您需要从SETX行中删除ECHO以激活路径变量的设置。



还请注意,SETX不会将目标变量设置为现有或当前的CMD实例 - 只有将来创建的CMD实例。



同样重要的是要记住,使用卸载功能此例程将从路径中删除目录,而不考虑任何其他软件的要求。


I want to have two batch files install.bat and uninstall.bat that are in the same folder as my command-line program program.exe.

I want the install.bat to add the current location of program.exe to the System Path environment variable.

Then I want the uninstall.bat to remove any paths to program.exe from the System Path environment variable.

Any ideas on how to do this?

解决方案

Perhaps This earlier solution would be of assistance.

A modified file to custom-fit your situation would be

@ECHO OFF
SETLOCAL
SET "batchdir=%~dp0"
SET "batchdir=%batchdir:~0,-1%"
SET "newpath="
:temploop
SET tempfile=%random%%random%%random%
IF EXIST "%temp%\%tempfile%*" GOTO temploop
SET "tempfile=%temp%\%tempfile%"
CALL :showpath >"%tempfile%"
:: This part removes the current directory from the path
FOR /f "delims=" %%p IN ('type "%tempfile%"') DO (
 CALL :addsegment "%%p"
)
DEL "%tempfile%"
IF /i "%1"=="/u" (SET "newpath=%newpath:~1%") ELSE (SET "newpath=%batchdir%%newpath%")
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 NOT "%segment%"=="%batchdir%" 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   

Essentially, the two batches are the same - the only difference is that for the INSTALL version, the directory is added into the path.

For this reason, I've simply desgned it so that thisbatch would install the file, and thisbatch /u would uninstall it.

Naturally, the calling of the routine to get a final OK to change the path is optional.

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.

It's also important to remember that using the uninstall feature in this routine would remove the directory from the path without regard to the requirements of any other software.

这篇关于从批量添加/删除路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆