流程
- 读取配置文件
- 请求URL并获取内容
- 比较版本并进行备份
- 停止服务
- 下载更新文件
- 启动服务
- 更新版本号为最新版本
配置信息
url=http://example.com/api
compare_content=202305010000
file_to_backup=xxxx.exe.bak
backup_dir=.\
service_name=xxxx
download_url=http://xxxx.com/files/xxxx.exe
download_path=.\xxxx.exe
更新脚本
@echo off
setlocal enabledelayedexpansion
set "config=config.ini"
REM 读取配置文件
for /f "tokens=1,2 delims==" %%a in (%config%) do (
set "%%a=%%b"
)
REM 请求URL并获取内容
powershell -Command "(New-Object System.Net.WebClient).DownloadFile('%url%', 'temp.txt')"
set /p content=<temp.txt
REM 比较内容并进行备份
if "%content%" == "%compare_content%" (
copy %file_to_backup% %backup_dir%
)
REM 停止服务
sc stop %service_name%
REM 下载文件
powershell -Command "(New-Object System.Net.WebClient).DownloadFile('%download_url%', '%download_path%')"
if %errorlevel% neq 0 (
REM 还原备份文件
copy %backup_dir%\%file_to_backup% .
) else (
REM 删除备份文件
del %backup_dir%\%file_to_backup%
)
REM 启动服务
sc start %service_name%
REM 更新版本号为最新版本
findstr "%compare_content%" %config% | sed "s/%compare_content%/%content%/g" > tmpFile && move /y tmpFile %config%