编程语言
首页 > 编程语言> > 使用BAT自动更新程序

使用BAT自动更新程序

作者:互联网

由于历史原因,原来写的C/S程序,客户端没有自动更新的功能,需要手动的去拿U盘更新客户端程序,且又无法增加这部分功能(由于网络、客户机都不属于我们维护),才想到使用批处理来实现自动更新的功能整体框架,具体的少部分功能需要根据自己去调整。这里只列出一个自动更新程序的框架。

主要是由实现的功能如下:

1)使用Web服务器发布新程序的压缩包和更新配置文件

2)更新配置文件使用文本文件,为键/值对配置。

3)业务流程:从指定服务器下载配置信息到本地 -->,读取配置文件,根据配置进行下载待更新文件压缩包到本地,解压,-->启动指定客户端程序

 

框架具体代码如下:

@echo off
::Mode con cols=47 lines=15
Title 批处理风险动作分析
cls
set V_main=20220225
echo.
echo.
echo  Version:%V_main%
echo.
echo                   正在初始化
echo.
echo.                 ...请稍等...
echo.
echo  本程序需要使用 【%cd%】 为工作目录。
echo.
echo  所有文件操作均在工作目录,请确认是否继续?[Y/N]
echo.
echo.
set confim=N
set /p confim=请选择 [Y/N]: 
if /i not "%confim%"=="y" echo,&echo 程序已退出&exit/b
echo.
echo.
echo  本程序正在执行中,请稍候......
echo.
echo.
ping 127.1>nul
call :main
exit/b


rem 主程序开始
:main
set urlPath="http://127.0.0.1/updateInfo.txt"
set locFile=""
::call :Web_DownloadFile "http://127.0.0.1/updateInfo.txt"
call :Web_DownloadFile %urlPath% "" locFile
echo locFile=%locFile%
echo ----------
call :getKeyVal %locFile% "url" valUrl
echo ----------
call :Web_DownloadFile %valUrl% "" zipFile
echo zipFile=%zipFile% ".\Temp\1"
echo ----------
::cls
::call :mkZip_Files "c:\tmp\Downloads\" 
::cls
call :unZip_Files %zipFile%
exit/b
rem 主程序结束



:mkZip_Files <ysPath> <zipFilePath>
title Zip 文件压缩  %date:~5,2%月%date:~8,2%日 %time:~0,2%时%time:~3,2%分%time:~6,2%秒
if /i "%~1."=="." (echo 待压缩包文件为空。&(if /i not %3.==. (set %3=null))&exit/b)
echo;
echo  正在压缩:
echo;
set ysPath=%~1
set zipFilePath=%~2
if /i "%ysPath:~-1%"=="\" (set ysPath=%ysPath:~0,-1%)
for /f "delims=" %%i in ("%ysPath%") do (
set ysFolder=%%~dpi
set ysName=%%~ni
set ysExtension=%%~xi
)
if /i "%~2."=="." (set zipFilePath=%ysFolder%%ysName%.zip)
echo  压缩路径:"%ysPath%"
echo  保存路径:"%zipFilePath%"
echo;
set zipVBS=MkZip.vbs
for %%a in (%zipVBS%) do (set localFolder=%%~dpa)
if not exist %localFolder% (md %localFolder%&echo 目录 %localFolder% 已创建 )
echo Compress "%ysPath%","%zipFilePath%" >%zipVBS%
echo rem 压缩文件>>%zipVBS%
echo Sub Compress^(ByVal mySourceDir,ByVal myZipFile^) >>%zipVBS%
echo     Const vOptions = 516 >>%zipVBS%
echo     Dim fso >>%zipVBS%
echo     Set fso = CreateObject^("Scripting.FileSystemObject"^) >>%zipVBS%
echo     If fso.GetExtensionName^(myZipFile^) ^<^> "zip" Then >>%zipVBS%
echo         Exit Sub >>%zipVBS%
echo     ElseIf fso.FolderExists^(mySourceDir^) Then >>%zipVBS%
echo         FType = "Folder" >>%zipVBS%
echo         FolderPath = mySourceDir >>%zipVBS%
echo     ElseIf fso.FileExists^(mySourceDir^) Then >>%zipVBS%
echo         FType = "File" >>%zipVBS%
echo         FileName = fso.GetFileName^(mySourceDir^) >>%zipVBS%
echo         FolderPath = Left^(mySourceDir, Len^(mySourceDir^) - Len^(FileName^)^) >>%zipVBS%
echo     Else >>%zipVBS%
echo         Exit Sub >>%zipVBS%
echo     End If >>%zipVBS%
echo     Set f = fso.CreateTextFile^(myZipFile, True^) >>%zipVBS%
echo     f.Write "PK" ^& Chr^(5^) ^& Chr^(6^) ^& String^(18, Chr^(0^)^) >>%zipVBS%
echo     f.Close >>%zipVBS%
echo     Set objShell = CreateObject^("Shell.Application"^) >>%zipVBS%
echo     Set objSource = objShell.NameSpace(FolderPath) >>%zipVBS%
echo     Set objTarget = objShell.NameSpace^(myZipFile^) >>%zipVBS%
echo     Select Case Ftype >>%zipVBS%
echo     Case "Folder" >>%zipVBS%
echo         Set objFolderItem = objSource.Items^(^) >>%zipVBS%
echo     Case "File" >>%zipVBS%
echo         Set objFolderItem = objSource.ParseName^(FileName^) >>%zipVBS%
echo     End Select >>%zipVBS%
echo     objTarget.CopyHere objFolderItem, vOptions >>%zipVBS%
echo     Do >>%zipVBS%
echo         WScript.Sleep 1000 >>%zipVBS%
echo     Loop Until objTarget.Items.Count ^> 0 >>%zipVBS%
echo End Sub >>%zipVBS%
::cscript //nologo %zipVBS% 2>nul
del /f /q %zipVBS% >nul 2>nul
echo;
echo  压缩完毕
echo;
echo  压缩路径:"%ysPath%"
echo  保存路径:"%zipFilePath%"
echo;
echo  任意键返回
pause>nul&(if /i not %3.==. (set %3="%zipFilePath%"))&exit/b


:unZip_Files <zipFile>,<unZipPath>
title Zip 文件解压缩  %date:~5,2%月%date:~8,2%日 %time:~0,2%时%time:~3,2%分%time:~6,2%秒
if /i "%~1."=="." (echo 压缩包文件为空。&(if /i not %3.==. (set %3=null))&exit/b)
echo;
echo  正在解压缩:
echo;
set zipFile=%~1
set unZipPath=%~2
for /f "delims=" %%i in ("%zipFile%") do (
set zipFolder=%%~dpi
set zipName=%%~ni
set zipExtension=%%~xi
)
if /i "%~2."=="." (set unZipPath=%zipFolder%%zipName%)
echo  压缩文件:"%zipFile%"
echo  释放路径:"%unZipPath%"
echo;
if /i not "%zipExtension%"==".zip" (
echo;
echo  解压缩失败
echo;
echo  文件 "%zipFile%" 不是 Zip 压缩文件
echo;
echo  任意键返回
pause>nul&(if /i not %3.==. (set %3=null))&exit/b
)
set unZipVBS=UnZip.vbs
for %%a in (%unZipVBS%) do (set localFolder=%%~dpa)
if not exist %localFolder% (md %localFolder%&echo 目录 %localFolder% 已创建 )
echo DeCompress "%zipFile%","%unZipPath%",True >%unZipVBS%
echo rem 解压缩文件,压缩包路径和解压目标文件夹请使用绝对路径>>%unZipVBS%
echo Sub DeCompress^(Source,Target,OpFlag^) >>%unZipVBS%
echo     Const vOptions=532 >>%unZipVBS%
echo     Dim ShApp,objSour,objTarg,oItem >>%unZipVBS%
echo     Set ShApp=CreateObject^("Shell.Application"^) >>%unZipVBS%
echo     zipFile = Source >>%unZipVBS%
echo     If left(zipFile,1) = "." Then >>%unZipVBS% 
echo         zipFile = Replace(wscript.ScriptFullName,wscript.ScriptName,"") ^& zipFile >>%unZipVBS%
echo     End If >>%unZipVBS%
echo     zipFile=Replace(Replace(Replace(Replace(zipFile,"\\","\"),"//","\"),"/","\"),"\.\","\") >>%unZipVBS%
echo     tagFolder = Target >>%unZipVBS%
echo     If left(tagFolder,1) = "." Then >>%unZipVBS%
echo         tagFolder = Replace(wscript.ScriptFullName,wscript.ScriptName,"") ^& tagFolder >>%unZipVBS%
echo     End If >>%unZipVBS%
echo     tagFolder=Replace(Replace(Replace(Replace(tagFolder,"\\","\"),"//","\"),"/","\"),"\.\","\") >>%unZipVBS%
echo     Set objTarg=ShApp.NameSpace(tagFolder) >>%unZipVBS%
echo     If (objTarg is nothing) And OpFlag Then >>%unZipVBS%
echo         ShApp.NameSpace(left(tagFolder,2)^&"\").NewFolder(right(tagFolder,len(tagFolder)-3)) >>%unZipVBS%
echo     End If  >>%unZipVBS%
echo     Set objSour=ShApp.NameSpace^(zipFile^) >>%unZipVBS%
echo     Set objTarg=ShApp.NameSpace^(tagFolder^) >>%unZipVBS%
echo     For Each oItem In objSour.Items >>%unZipVBS%
echo         objTarg.CopyHere oItem,vOptions >>%unZipVBS%
echo     Next >>%unZipVBS%
echo     Set ShApp=Nothing >>%unZipVBS%
echo End Sub >>%unZipVBS%
cscript //nologo %unZipVBS% 2>nul
del /f /q %unZipVBS% >nul 2>nul
echo;
echo  解压缩完毕
echo;
echo  压缩文件:"%zipFile%"
echo  释放路径:"%unZipPath%"
echo;
echo  任意键返回
pause>nul&(if /i not %3.==. (set %3="%unZipPath%"))&exit/b


:getKeyVal <localConfig> <key> [ret]
title 读取更新配置文件  %date:~5,2%月%date:~8,2%日 %time:~0,2%时%time:~3,2%分%time:~6,2%秒
echo %0
if /i "%~1."=="." (echo 配置文件路径为空。&(if /i not %3.==. (set %3=null))&exit/b)
if /i "%~2."=="." (echo 查找关键字为空。&(if /i not %3.==. (set %3=null))&exit/b)

for /f %%a in (%~1) do (
    for /f "tokens=1,* delims=:=" %%i in ("%%a") do (
        if /i %~2==%%i ((if /i not %3.==. (set %3=%%j))&exit/b)
    )
)
(if /i not %2.==. (set %2=""))&exit/b
goto :eof


:Web_DownloadFile <url> <saveFilePath> [ret]
title 下载文件  %date:~5,2%月%date:~8,2%日 %time:~0,2%时%time:~3,2%分%time:~6,2%秒
echo %0
if /i "%~1."=="." (echo 下载的URL路径为空。& goto :eof)
set url=%~1
set urlFileName=%~nx1
set saveFilePath=%~2
if /i "%saveFilePath%"=="" (set saveFilePath=.\Downloads\%urlFileName%)
for %%a in ("%saveFilePath%") do (set localFolder=%%~dpa)
if not exist %localFolder% (md %localFolder%&echo 目录 %localFolder% 已创建 )
set vbDownFile="%windir%\System32\Web_Download.vbs"
set vbDownFile=Web_Download.vbs
echo rem The usage is as follow (CMD):cscript //NoLogo /e:vbscript Web_Download.vbs "Web download's URL" "your save file's path" >%vbDownFile%
echo Set Web_Download = CreateObject("Microsoft.XMLHTTP") >>%vbDownFile%
echo     Web_Download.Open "GET",Wscript.Arguments.Item(0),0 >>%vbDownFile%
echo     Web_Download.Send() >>%vbDownFile%
echo Set Web_Save = CreateObject("ADODB.Stream") >>%vbDownFile%
echo     Web_Save.Mode = 3 >>%vbDownFile%
echo     Web_Save.Type = 1 >>%vbDownFile%
echo     Web_Save.Open() >>%vbDownFile%
echo     Web_Save.Write(Web_Download.responseBody) >>%vbDownFile%
echo     Web_Save.SaveToFile Wscript.Arguments.Item(1),2 >>%vbDownFile%
echo     Web_Save.Close() >>%vbDownFile%
::cscript //NoLogo /e:vbscript %vbDownFile% %url% %saveFilePath% 2>nul
cscript //NoLogo /e:vbscript %vbDownFile% "%url%" "%saveFilePath%" 2>nul
del /f /q %vbDownFile% 2>nul
(if /i not %3.==. (set %3="%saveFilePath%"))&exit/b
goto :eof

 

 

=

标签:Web,BAT,set,unZipVBS%,程序,echo,2%,自动更新,zipVBS%
来源: https://www.cnblogs.com/mq0036/p/15936449.html