添加/使用批量从路径删除? [英] Add/Remove from Path using Batch?
问题描述
我想有两个批处理文件 INSTALL.BAT
和 uninstall.bat
是在同一文件夹中我的命令行程序的Program.exe
。
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
.
我要的 INSTALL.BAT
到的Program.exe
的当前位置添加到系统路径
环境变量。
I want the install.bat
to add the current location of program.exe
to the System Path
environment variable.
然后我想在 uninstall.bat
来从系统中删除,以的Program.exe
的任何路径路径
环境变量。
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?
推荐答案
早期解决方案会有所帮助。
一个修改过的文件,以定制适合您的情况是
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
从本质上讲,两批都是相同的 - 唯一的区别是,对于 INSTALL
版本,该目录被添加到路径
Essentially, the two batches are the same - the only difference is that for the INSTALL
version, the directory is added into the path.
由于这个原因,我只是desgned它让 thisbatch
将安装文件和 thisbatch / U
将其卸载。
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.
我不知道你需要哪些选项的SETX,所以命令只是回显。你需要从SETX线拆下ECHO激活路径变量的设置。
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.
还要注意SETX不设置在现有的或当前CMD实例目标变量 - 只有那些在将来创建
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屋!