更新当前正在运行的批处理脚本

时间:2018-06-07 00:03:45

标签: batch-file updates

再次使用我的批处理脚本。我注意到我的更新例程无法正常工作。迭代1将导致正在删除正在运行的脚本,而不会从我选择的文件夹中复制新版本。迭代2现在删除旧脚本并复制新脚本,但每行代码改变后,我最终关闭当前正在运行的脚本,并且新的更新脚本没有启动。

以下是Update例程的片段:

:Options
REM I'll set the variable here to what is set elsewhere in the script.
SET VERSION=Version 1.7
CLS
ECHO.
ECHO.
ECHO %VERSION%
ECHO. 
ECHO. 
ECHO ==================== OPTIONS =====================
ECHO.
ECHO     1. Update Script WIP
ECHO     2. Install entire script to Statup Folder
ECHO     3. Install individual items for auto-launch
ECHO     4. Install Windows Logout feature
ECHO     5. Uninstall
ECHO     6. Change Colors
ECHO     7. Changelog
ECHO.
ECHO =======PRESS 'Q' TO QUIT OR 'B' TO GO BACK========
ECHO.

SET OPTION=
SET /P OPTION=Select an Option:
IF /I '%OPTION%'=='1' GOTO Update
IF /I '%OPTION%'=='2' GOTO Install
IF /I '%OPTION%'=='3' GOTO I_Install
IF /I '%OPTION%'=='4' GOTO I_Logout
IF /I '%OPTION%'=='5' GOTO Uinstall
IF /I '%OPTION%'=='6' GOTO Colors
IF /I '%OPTION%'=='7' GOTO Changelog
IF /I '%OPTION%'=='q' GOTO quit
IF /I '%OPTION%'=='quit' GOTO quit
IF /I '%OPTION%'=='b' GOTO menu
IF /I '%OPTION%'=='back' GOTO menu

ECHO.
ECHO ============INVALID INPUT============
ECHO Please select a number
ECHO or select 'Q' or 'B'.
ECHO ======PRESS ANY KEY TO CONTINUE======

PAUSE > NUL
GOTO Options

:Update
ECHO.
IF EXIST "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup\Shortcut Browser *.bat" (
  GOTO Update_1
  ) ELSE (
    ECHO You have not installed the script yet. Please install the script first.
  )    
PAUSE
GOTO Options

:Update_1
  IF EXIST "%userprofile%\AppData\Local\Temp\Update.bat" (
    GOTO Update_2
  )  ELSE  (
REM Here the main script writes an update script to a batch file
     ECHO DEL "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup\Shortcut Browser *.bat" > "%userprofile%\AppData\Local\Temp\Update.bat"
REM I replaced the original file location with one on the desktop. The original location was on a network drive, location is confidential.
     ECHO COPY "%userprofile\Desktop\Shortcut Browser *.bat" "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup" >> "%userprofile%\AppData\Local\Temp\Update.bat"
     ECHO CALL "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup\Shortcut Browser *.bat" >> "%userprofile%\AppData\Local\Temp\Update.bat"
     ECHO EXIT >> "%userprofile%\AppData\Local\Temp\Update.bat"
  )
GOTO Update_2

REM Here the main script calls upon the update script
:Update_2
  CALL "%userprofile%\AppData\Local\Temp\Update.bat"
REM The script seems to hang here. Either both the scripts will remain open or close or a combination depending on where CALL and START were used.
  PAUSE
GOTO Options

我在其他地方看到过我可以使用FC命令。在解决此问题之前,我不知道它。我已经走到了这一步,我觉得我只是错过了一些小技巧来启动新的更新脚本。

如果您需要更多信息,请告诉我们。我想我已经涵盖了一切。

1 个答案:

答案 0 :(得分:0)

@ECHO OFF
SETLOCAL
TITLE Super Script 5000

:Options
REM I'll set the variable here to what is set elsewhere in the script.
SET VERSION=Version 1.7
CLS
ECHO.
ECHO.
ECHO %VERSION%
ECHO.
ECHO.
ECHO ==================== OPTIONS =====================
ECHO.
ECHO     1. Update Script WIP
ECHO     2. Install entire script to Statup Folder
ECHO     3. Install individual items for auto-launch
ECHO     4. Install Windows Logout feature
ECHO     5. Uninstall
ECHO     6. Change Colors
ECHO     7. Changelog
ECHO.
ECHO =======PRESS 'Q' TO QUIT OR 'B' TO GO BACK========
ECHO.

SET "OPTION="
SET /P "OPTION=Select an Option: "
IF /I "%OPTION%"=="1" GOTO Update
IF /I "%OPTION%"=="2" GOTO Install
IF /I "%OPTION%"=="3" GOTO I_Install
IF /I "%OPTION%"=="4" GOTO I_Logout
IF /I "%OPTION%"=="5" GOTO Uinstall
IF /I "%OPTION%"=="6" GOTO Colors
IF /I "%OPTION%"=="7" GOTO Changelog
IF /I "%OPTION%"=="q" GOTO quit
IF /I "%OPTION%"=="quit" GOTO quit
IF /I "%OPTION%"=="b" GOTO menu
IF /I "%OPTION%"=="back" GOTO menu

ECHO.
ECHO ============INVALID INPUT============
ECHO Please select a number
ECHO or select 'Q' or 'B'.
ECHO ======PRESS ANY KEY TO CONTINUE======

PAUSE > NUL
GOTO Options

:Update
  ECHO.
  IF EXIST "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup\Shortcut Browser *.bat" (
    GOTO Update_1
  ) ELSE (
    ECHO You have not installed the script yet. Please install the script first.
  )
  PAUSE
GOTO Options

:Update_1
  IF EXIST "%tmp%\update.bat" (
    2>NUL DEL "%tmp%\update.bat" || GOTO Options
  )

  SETLOCAL

  FOR %%A IN (
    "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup\Shortcut Browser *.bat"
  ) DO (
    SET "old_version=%%~A"
    SET "old_dir=%%~dpA"
    SET "old_file=%%~nxA"
  )

  FOR %%A IN (
    "%userprofile%\Desktop\Shortcut Browser *.bat"
  ) DO (
    SET "new_version=%%~A"
    SET "new_dir=%%~dpA"
    SET "new_file=%%~nxA"
  )

  IF NOT DEFINED old_version (
    ENDLOCAL
    GOTO Options
  ) ELSE IF NOT DEFINED new_version (
    ENDLOCAL
    GOTO Options
  )

  (
    ECHO @ECHO OFF
    ECHO SETLOCAL
    ECHO.
    ECHO ECHO Update and restarting...
    ECHO ^>NUL PING localhost -n 3
    ECHO.
    ECHO SET "old_version=%old_version%"
    ECHO SET "new_version=%new_version%"
    ECHO SET "updated_version=%old_dir%\%new_file%"
    ECHO.
    ECHO CALL :log /clear_log
    ECHO.
    ECHO IF NOT EXIST "%%new_version%%" (
    ECHO   CALL :log ERROR: Not exist "%%new_version%%".
    ECHO   EXIT /B 1
    ECHO ^)
    ECHO.
    ECHO CALL :log Move old_version to old_version.bak.
    ECHO MOVE "%%old_version%%" "%%old_version%%.bak" ^|^| (
    ECHO   CALL :log ERROR: Move failed to backup old version.
    ECHO   EXIT /B 2
    ECHO ^)
    ECHO.
    ECHO CALL :log Copy new_version to old_version.
    ECHO COPY "%%new_version%%" "%%appdata%%\Microsoft\Windows\Start Menu\Programs\Startup\" ^|^| (
    ECHO   CALL :log ERROR: Copy failed. Restore old version.
    ECHO   MOVE "%%old_version%%.bak" "%%old_version%%" ^|^| (
    ECHO     CALL :log ERROR: Restore old version failed.
    ECHO     EXIT /B 3
    ECHO   ^)
    ECHO   EXIT /B 4
    ECHO ^)
    ECHO.
    ECHO CALL :log Delete old_version.bak.
    ECHO DEL "%%old_version%%.bak" ^|^| (
    ECHO   CALL :log WARNING: Delete backup file failed.
    ECHO ^)
    ECHO.
    ECHO IF NOT EXIST "%%updated_version%%" (
    ECHO   CALL :log ERROR: Not exist: "%%updated_version%%"
    ECHO   EXIT /B 5
    ECHO ^)
    ECHO.
    ECHO ENDLOCAL
    ECHO.
    ECHO ECHO Ready to restart updated version
    ECHO PAUSE
    ECHO CALL "%old_dir%\%new_file%"
    ECHO EXIT /B
    ECHO.
    ECHO :log
    ECHO   SETLOCAL
    ECHO   SET "log=%%tmp%%\update.log"
    ECHO   IF /I "%%~1"=="/clear_log" TYPE NUL ^> "%%log%%" ^& EXIT /B
    ECHO   IF /I "%%~1"=="/delete_log" 2^>NUL DEL "%%log%%" ^& EXIT /B
    ECHO   SET args=%%*
    ECHO   IF NOT DEFINED args EXIT /B
    ECHO   ECHO %%*
    ECHO   ^>^> "%%log%%" ECHO %%*
    ECHO EXIT /B
  ) > "%tmp%\update.bat"
  ENDLOCAL

  START "Super Script 5000" "cmd /c "%tmp%\update.bat""
EXIT

:Quit

Win7 VM上测试。脚本需要在更新时重新启动 可能会出现奇怪的问题。 CMD不喜欢脚本随之改变 正在阅读和解释它们。甚至重复使用相同的控制台 将START/B一起使用可能会导致关键输入问题。

每次更新都会覆盖

update.bat,因为它存储绝对路径 所以每次更新都将是不同的路径,文件名会发生变化。 使用带有CALL的通配符可能效果不佳。

update.bat将旧版本移动到.bak文件,复制新版本 版本,如果成功,删除.bak文件,否则移动.bak文件 .bak文件到旧版本。这只是为了确保电流 即使更新失败,版本也始终存在。

:Options标签中的引号从'更改为"。行情不是 可以像其他语言一样互换。示例:"%OPTION%" 如果包含空格,'%OPTION%'可能会导致错误。

我注意到完整脚本顶部没有SETLOCAL。建议你添加 它除非你有充分的理由不这样做。

我将标签:Update_1:Update_2合并为一个。

如果时间多或少,可能会调整Ping localhost -n个数字 需要的。

如果出现问题,请查看%tmp%\update.log。虽然期待 将错误显示在控制台中作为标签:log update.bat回调控制台并写入日志。