所以,我现在是如何做到的,它调用另一个bat文件来更新它,然后该批处理文件更新,并设置%ERRORLEVEL%为1。在原始程序开始时,它会检查errorlevel是否为1,如果是,它会转到主菜单,但现在,它不会调用更新文件,它只会转到菜单。这是我的代码
主程序
IF %errorlevel% EQU 1 goto begin
call updater.bat
:begin
echo MENU
更新器
set=errorlevel 1
wget (updatelink here)
call mainprogram.bat
现在,它有时工作,有时不工作,这使我相信某些命令以某种方式增加了错误级别,但在errorlevel检查之前的唯一代码是
@echo off
color 0f
cls
set currentver=v0.5.6
(check code)IF %errorlevel% EQU 1 goto begin
https://code.google.com/p/flashcart-helper/source/browse/trunk/0.6/FlashcartHelperRobocopy.bat这是我现在的资料
不要设置errorlevel。它是一个内变量。在批处理开始时,errorlevel将为0,因为您所做的只是设置了一个局部变量。这几乎总是(永远不要说不可能)成功。此外,如果errorlevel是1,我没看错你似乎也有一个无限循环?根据我对你所说的理解,你的批次是这样的:
主要@echo off
color 0f
cls
set currentver=v0.5.6
IF %errorlevel% EQU 1 goto begin
call updater.bat
:begin
echo MENU
更新
set=errorlevel 1
wget (updatelink here)
call mainprogram.bat
当你每次做任何自找麻烦的事情时,errorlevel都会被覆盖。把%errorlevel%
改成%error%
,问题就解决了。因为它是一个本地环境变量,它也应该在批处理文件之间传递。只是要注意不要在其他地方使用error
这是一个使用Dropbox公共文件夹而不使用wget的解决方案。它使用Win7+机器上的PowerShell。用您自己的url更新下面的https://dl.dropboxusercontent.com/u/12345678/。
它会自动创建一个.conf文件用于配置。将dropbox上文件的__deploy_mode设置为1,以便版本文件可以更新,但脚本不会被意外执行。
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET time_start=%time%
SET time_choice_wait=20
SET script_ver=1.00
SET script_name=%~n0
SET server_url=https://dl.dropboxusercontent.com/u/12345678/
SET script_name_bat=%~dp0%script_name%.bat
SET script_name_cfg=%~dp0%script_name%.conf
SET script_name_latest_ver=%~dp0%script_name%.latest.ver
ECHO %script_name% v%script_ver%
ECHO %script_ver% > %script_name%.current.ver
IF NOT EXIST "%script_name_cfg%" CALL :SCRIPT_MISSING_CFG
FOR /f "delims=" %%x IN (%script_name%.conf) DO (SET "%%x")
IF %__deploy_mode% EQU 1 GOTO :EOF
IF %auto_update_compare% EQU 1 CALL :SCRIPT_COMPARE_VER
:SCRIPT_MAIN
REM =======================================
REM === EDIT BELOW THIS LINE ==
REM TODO Add main content
ECHO.
ECHO Waiting for content...
REM === EDIT ABOVE THIS LINE ==
REM =======================================
GOTO END
:SCRIPT_MISSING_CFG
ECHO Creating new %script_name%.conf file...
ECHO __deploy_mode=0 > "%script_name_cfg%"
ECHO repository_base_url=%server_url% >> "%script_name_cfg%"
ECHO auto_update_compare=1 >> "%script_name_cfg%"
ECHO auto_update_download=1 >> "%script_name_cfg%"
ECHO Update %script_name%.conf as needed, then save and close to continue.
ECHO Waiting for notepad to close...
NOTEPAD "%script_name_cfg%"
GOTO :EOF
:SCRIPT_COMPARE_VER
ECHO Please wait while script versions are compared...
Powershell -command "& { (New-Object Net.WebClient).DownloadFile('%server_url%%script_name%.current.ver', '%script_name_latest_ver%') }"
IF NOT EXIST "%script_name_latest_ver%" GOTO END
SET /p script_latest_ver= < "%script_name_latest_ver%"
IF %script_ver% EQU %script_latest_ver% CALL :SCRIPT_COMPARE_VER_SAME
IF %script_ver% NEQ %script_latest_ver% CALL :SCRIPT_COMPARE_VER_DIFF
GOTO :EOF
:SCRIPT_COMPARE_VER_SAME
ECHO Versions are both %script_name% v%script_ver%
GOTO :EOF
:SCRIPT_COMPARE_VER_DIFF
ECHO Current Version:%script_ver% ^| Server Version:%script_latest_ver%
IF %auto_update_download% EQU 1 GOTO SCRIPT_DOWNLOAD_SCRIPT
ECHO.
ECHO Would you like to download the latest %script_name% v%script_latest_ver%?
ECHO Defaulting to N in %time_choice_wait% seconds...
CHOICE /C YN /T %time_choice_wait% /D N
IF ERRORLEVEL 2 GOTO SCRIPT_DOWNLOAD_NOTHING
IF ERRORLEVEL 1 GOTO SCRIPT_DOWNLOAD_SCRIPT
IF ERRORLEVEL 0 GOTO SCRIPT_DOWNLOAD_NOTHING
:SCRIPT_DOWNLOAD_SCRIPT
ECHO Please wait while script downloads...
Powershell -command "& { (New-Object Net.WebClient).DownloadFile('%server_url%%script_name%.bat', '%script_name_bat%') }"
ECHO Script Updated to v%script_latest_ver%^^!
REM User must exit script. Current batch is stale.
GOTO :END
:SCRIPT_DOWNLOAD_NOTHING
GOTO :EOF
:END
SET time_end=%time%
ECHO.
ECHO Script started:%time_start%
ECHO Script ended :%time_end%
:END_AGAIN
pause
ECHO.
ECHO Please close this window
ECHO.
GOTO END_AGAIN
您可以通过以下步骤完成:
1。在服务器上放两个文件,一个配置文件,一个需要更新的更高版本的bat文件;在配置文件中设置最新版本号。
2.每次启动时都应该检查和更新客户端代码。您可以在服务器配置文件中读取新版本,然后与本地bat文件版本进行比较。如果不相等,则执行update,否则反之。