How to get a Windows batch file for a secure update

eg. I want to run a Windows batch file, say upgrade.bat, which copies a bunch of files from the source directory to the directory where the batch file is located. The problem is that one of the copied files may be a newer version of the update. bat, so the batch file will overwrite itself while it is still running.

This seems to lead to some unpredictable batch file execution behavior, so I want to avoid copying over the batch file, which still works. Ideally, I want the existing version of upgrade.bat to run until it is complete, and next time launch the new version. Are there (simple) ways to achieve this?

+8
batch-file
source share
5 answers

You can start a copy as the last operation using the start command to start it from another terminal. Check out this example, especially the last lines.

@echo off set CUR_FILE=batman.bat set FOUND_EQUAL="FALSE" set FROM_DIR=c:\temp\galeria\ SETLOCAL DisableDelayedExpansion FOR /R %FROM_DIR% %%F IN (*) DO ( SET "p=%%F" SETLOCAL EnableDelayedExpansion SET ABC=!p:%FROM_DIR%=! IF NOT !ABC! == !CUR_FILE! ( echo copying %%F copy "%%F" . ) ENDLOCAL ) echo trying to copy file with the same name [last operation] start copy "%FROM_DIR%%CUR_FILE%" . 
+3
source share
 @ECHO OFF SETLOCAL IF /i NOT "%~dp0"=="%temp%\" ( COPY /y "%~dpnx0" "%temp%\%~nx0" >nul "%temp%\%~nx0" ) ECHO Now we run the rest of the original UPGRADE.BAT 

This sequence of lines at the beginning of upgrade.bat should work.

Let's see if we will work with the copy in %temp% . If not, then copy this file to temp and run it from there.

Therefore, the package actually runs from %temp% , and the original version can be overwritten.

+5
source share

To do this, the following requirements must be met:

  • Overwriting a batch file for a newer version of itself should be the last batch file command, so the next command after copy should be exit /B or exit .
  • Previous commands must be loaded into memory before they are executed. This is easy to do by enclosing them in parentheses.

I.e:

 @echo off rem Do program business here... echo Anything rem Parse/load following commands before execute they: ( rem Copy many files, probably a newer version of myself xcopy /Y *.* rem You may execute other commands here... echo Files copied! rem Terminate *this version* of the running Batch file exit /B ) 
+4
source share
 @echo off cmd /c copy "C:\somepath\upgrade.bat" "%0" & %0 

This will start a new cmd process and replace the current batch file with "C: \ somepath \ upgrade.bat" and restart the batch file

+3
source share

Is it possible to mark each new version of a batch file with a version suffix and use the second batch file as a launch?

For example, let's say that you are a batch payload file upgrade.bat , subsequent versions will be named as upgrade_001.bat, upgrade_002.bat, upgrade_003.bat .. or upgrade_201305122134 (the suffix is ​​yyyymmddHHMM), the new batch file launcher.bat will search for the latest batch update file, looking for the one that has the highest suffix, and execute it

+2
source share

All Articles