You are not logged in.

#1 15 Jan 2015 09:19

jetelina
Member
Registered: 30 Aug 2013
Posts: 5

Update machanism for BAT file

Hi,
I'm solving interesting thing about batch file. My requirement is following...
- I would like to have BAT file that when executed will check if it is the latest.
- - If yes, it will continue execution
- - If no, it will download latest version and execute it

The solution doesn't seems to be complicated...
- Share latest BAT file version to some network share
- Copy file from network path every time
- File compare - if not different leave, if different copy
- Execute latest copy

My questions are:
1) Where to put the file?
- Network share would require credentials, before anything can happen - not so comfortable.
- Web would be fine, but need wget to download.
- FTP looks like best option to me

2) Any better latest version check instead of downloaded every time and file compare?

3) Can I overwrite current-outdated-running-BAT with latest-BAT or should I save it to different file

4) How to execute updated version form within BAT
- I'm not fan of lite-downloader-BAT + main-BAT
- I would like that it works even without update - just older version

I'm looking for optimizations and experiences with this perocedure.
Any advice greatly appreciated.

Thank you :-)

Offline

#2 15 Jan 2015 10:57

foxidrive
Member
Registered: 04 Apr 2013
Posts: 339

Re: Update machanism for BAT file

jetelina wrote:

I'm solving interesting thing about batch file. My requirement is following...
- I would like to have BAT file that when executed will check if it is the latest.
- - If yes, it will continue execution
- - If no, it will download latest version and execute it

- Web would be fine, but need wget to download.

A VBS script and Powershell can access web based files.

2) Any better latest version check instead of downloaded every time and file compare?

You can download a text file with only the version number in it to compare, and then get the updated file if it is needed.

It's only going to be better if your batch file is particularly fat.

3) Can I overwrite current-outdated-running-BAT with latest-BAT or should I save it to different file

When you overwrite a running batch file there are ways that it can break the byte counter that keeps track of where it is currently up to - so it's best not to overwrite a running file.

4) How to execute updated version form within BAT
- I'm not fan of lite-downloader-BAT + main-BAT
- I would like that it works even without update - just older version


It depends on how you are using it. 

The batch file can tell the user that an update will occur and to wait -
and it then creates a secondary stub batch script in the %temp% folder that will get the new version, overwriting the old one, and then restart the new version.

The secondary stub batch script and the new version would be launched without using the call keyword

Offline

#3 22 Jan 2015 06:01

Oatworm
Member
From: Nevada
Registered: 22 Jan 2015
Posts: 5

Re: Update machanism for BAT file

You can do a wget-style pull via CMD. Look up BITSADMIN - namely:

bitsadmin /transfer GETBACKUP /download

As for matching contents, you could use something like FCIV (first link on Google if you search for it - it's a Microsoft product; I'd post the link myself but I'm not allowed to yet) to generate an MD5 hash of the source and destination file to see if it matches and then act on it.

Offline

#4 23 Jan 2015 17:53

Oatworm
Member
From: Nevada
Registered: 22 Jan 2015
Posts: 5

Re: Update machanism for BAT file

Turns out we're all overthinking this - there's a COMP command.
http://ss64.com/nt/comp.html

So, with that in mind, we have a possible control flow:

1. Download possibly updated script into %TMP%.
2. Run COMP and check the ERRORLEVEL after it's done.
|--a. If the files match, continue running the script.
|--b. If the files don't match, run the version in %TMP% with instructions at the end to move to its rightful location as soon as possible.

Fleshing that flow out a bit further, we're going to end up with something like this:
1. Is this the updated script?
|--a. If so, copy this script over the production script, then perform script actions.
|--b. If not, download possibly updated script into %TMP%
|--c. Run COMP
|----1. If the files match, perform script actions.
|----2. If files don't match, run the version in %TMP%, update the script, then perform script actions.

With that in mind...

@ECHO OFF
:: Get and set script locations
SET _ProductionScript="%~f0%"
SET _UpdatedScript="%TMP%\scriptname.cmd"

:: Is this the updated script? If so, update the script, then run it.
IF %_ProductionScript%==%_UpdatedScript% GOTO UpdateScript

:: Check for updated script
SET _UpdateServer=http://url.to.hosted.script.location.com
BITSADMIN /transfer UPDATESCRIPT /download %_UpdateServer% %_UpdatedScript%

:: Check if updated script matches current script.
:: If it does (ERR=0), keep going.
:: If it doesn't (ERR=1), open the other script.
:: If something went wrong (ERR=2), do something.

ECHO n|COMP %_ProductionScript% %_UpdatedScript%
IF %ERRORLEVEL% EQU 0 GOTO RunScript
IF %ERRORLEVEL% EQU 1 GOTO RunUpdateScript
IF %ERRORLEVEL% EQU 2 GOTO ScriptErr
GOTO :EOF

:: We're in the updated script!
:: Overwrite the original script, then do script things.
:UpdateScript
SET _ScriptDestination="C:\Path\To\Production\Script.cmd"
COPY /Y %_UpdatedScript% %_ScriptDestination%

:: ********** DO SCRIPT THINGS HERE **********
:RunScript



:: ********** END SCRIPT THINGS **********
GOTO :EOF

:: We're not in the updated script - let's fix that.
:RunUpdateScript
CMD /C "%_UpdatedScript%"
GOTO :EOF

:ScriptErr
ECHO:Something has gone horribly wrong!
PAUSE
GOTO :EOF

Offline

#5 23 Jan 2015 19:31

npocmaka
Member
From: Bulgaria
Registered: 03 Dec 2009
Posts: 446

Re: Update machanism for BAT file

Are you sure this will download a file:

Oatworm wrote:
BITSADMIN /transfer UPDATESCRIPT /download %_UpdateServer% %_UpdatedScript%

Last edited by npocmaka (23 Jan 2015 19:32)

Offline

#6 26 Jan 2015 18:46

Oatworm
Member
From: Nevada
Registered: 22 Jan 2015
Posts: 5

Re: Update machanism for BAT file

npocmaka wrote:

Are you sure this will download a file:

Oatworm wrote:
BITSADMIN /transfer UPDATESCRIPT /download %_UpdateServer% %_UpdatedScript%

Sure am. Try setting _UpdateServer to http://google.com/robots.txt, set _UpdatedScript to a local location (say, C:\Users\npocmaka\robots.txt), then feed it into BITSADMIN using the syntax shown above. You'll end up with a quick window that looks sort of like:

DISPLAY: 'UPDATESCRIPT' TYPE: DOWNLOAD STATE: TRANSFERRED
PRIORITY: NORMAL FILES: 1 / 1 BYTES: 8044 / 8044 (100%)
Transfer complete.

And then you'll end up with a robots.txt file in your directory:

C:\Users\npocmaka>dir robots.txt
 Volume in drive C has no label.
 Volume Serial Number is 1234-5678

 Directory of C:\Users\npocmaka

01/09/2015  11:04 AM             8,044 robots.txt
               1 File(s)          8,044 bytes
               0 Dir(s)  259,546,918,912 bytes free

Offline

Board footer

Powered by