Windows下安装及卸载程序可用的添加和删除当前路径到环境变量的bat脚本以及如何和inno setup结合使用的实例

1,324 阅读3分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第20天,点击查看活动详情

1.安装bat脚本-install.bat(将当前路径添加到环境变量中)

可以和inno setup结合安装完成后执行该脚本添加当前路径到环境变量中:(errorlevel在管理员模式运行时会闪退)

cd /d %~dp0

::安装注册服务
start progressService.exe install

::添加当前工作目录到环境变量
@echo off
set "thispath=%~dp0"
set "thispath=%thispath:~0,-1%"
echo 当前文件所在的目录的绝对路径:%thispath%
set mypath=%path%
echo %mypath% > temp.txt

find "%thispath%" temp.txt
if %errorlevel% == 0 (
    echo path环境变量中 已经包含了 当前路径%thispath%
    echo 程序返回码:%errorlevel%
) else (
    echo path环境变量中 没有包含   当前路径%thispath%
    echo 程序返回码:%errorlevel%
    setx /m "path" "%thispath%;%path%"
)    

del temp.txt

::启动推拉流服务
start progressService.exe start

exit
::pause

由于上述脚本安装到C盘时运行会出现errorlevel调用闪退问题,所以最后做了妥协,不再判断环境变量是否存在而直接添加,这种添加需要卸载时进行删除环境变量路径:

cd /d %~dp0

::安装注册服务
start progressService.exe install


::添加当前工作目录到环境变量
set "thispath=%~dp0"
echo 当前文件所在的目录的绝对路径:%thispath%
::追加当前文件所在目录的路径到path环境变量中
setx /m "path" "%thispath%;%path%"

::启动服务
start progressService.exe start

timeout /T 3 /NOBREAK
exit
::pause

需要管理员权限运行该bat脚本

start progressService.exe install是运行win_sw进行服务注册的,注册后可以开机运行及后台以服务形式运行。

2.卸载bat脚本-uninstall.bat(搜索当前路径并删除)

上述安装脚本可以按照如下搜索路径方式修改,这个留下来给读者扩展:

::停止并注销服务
cd /d %~dp0

start progressService.exe stop
start progressService.exe uninstall

set pathStr=%path%
set cm_ser=%~dp0
set nPath=hello

setlocal enableDelayedExpansion

:Loop
for /f "delims=; tokens=1,*" %%a in ("%pathStr%") do (
  if not "%%a"=="%cm_ser%" (
    if "!nPath!"=="hello" (
      set nPath="%%a"
    ) else (
      set nPath=%nPath%;"%%a"
    )
  )
  set pathStr=%%b
  goto Loop
)

set nPath=%nPath:"=%
echo %nPath%
setx Path "%nPath%" /m >nul
rem pause>nul

timeout /T 5 /NOBREAK

exit
::pause

卸载时同步删除环境变量,否则多次安装会添加多个路径到环境变量

3.inno setup添加安装和卸载时执行上述bat脚本

Inno setup及设置安装完成后执行对应bat脚本(RUN和UninstallRun),并且增加了管理员权限运行,用于安装和卸载时方便管理员权限调用对应的install.bat和uninstall.bat:

; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

...

[Setup]
; NOTE: The value of AppId uniquely identifies this application. Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{27113DE0-961C-4343-B694-E49410E65D11}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={autopf}{#MyAppName}
DefaultGroupName={#MyAppName}
; Uncomment the following line to run in non administrative install mode (install for current user only.)
;PrivilegesRequired=lowest
PrivilegesRequired=admin
OutputBaseFilename=xxx
Compression=lzma
SolidCompression=yes
WizardStyle=modern

[Run]
Filename: "{app}\install.bat"; Description: "{cm:LaunchProgram,安装程序}";Flags: runhidden runasoriginaluser

[UninstallRun]
Filename: "{app}\uninstall.bat"; Flags: waituntilterminated

[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"

[Files]
Source: "D:\code\CMPushFlowWebService*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
; NOTE: Don't use "Flags: ignoreversion" on any shared system files

添加点:

4.安装到 C盘权限不足问题处理

Inno Setup打包的程序安装完成后运行失败,这个是因为权限不够,打包的应用需要管理员权限,我这里bat脚本添加环境变量时失败了,以管理员权限添加才可以

解决方案:

步骤一:

在[Setup]节点添加 PrivilegesRequired=admin

image.png

步骤二:

进入Inno Setup安装根目录,找到文件SetupLdr.e32,这是一个二进制配置文件,需要用到ResHacker.exe这个工具修改(www.angusj.com/resourcehac…

  找到,

修改为

步骤三:重新打包软件

打成exe包出现有小盾牌的标志就说明添加权限成功啦。

5. 最后

这样打包的exe程序在安装后就可以将当前路径添加到环境变量了,卸载时还可以自动删除环境变量中的路径以及删除对应的文件。