So I am creating a batch file for our internal IT group to automate some functions. This file will be changing often to add new features so I need a way to update it. Rather than emailing everyone a new copy when it changes I think it would be really cool for it to update itself.
What I am thinking is when I have a new version available putting it on a public server like my DropBox account. The way it would update itself would be everytime the script starts it would check this specific folder on my drop box account to see if a new file is in there. If there is it would download it automatically to the users desktop and delete the old file.
To give you an example lets say my script is currently called SystemInfo.bat. Lets say I update the file and put the update in one of my DropBox folders. Each time someone runs the file from their desktop it would check to see if a new version is available from my DropBox and if so download it to their desktop and delete the old file. Each new version downloaded would do the same thing....Check to see if a new version is available and download it always replacing the old one.
The only way I can think to do this is with the FTP command but I don't think DropBox has FTP access.
Any Ideas on how to accomplish this?
How to make a batch file update itself
Moderator: DosItHelp
Re: How to make a batch file update itself
I would try to split it into two files.
The first file, always look if there is an update and do the update if it is required.
The second file is the worker.
The system is safe, even if an update failed or you have a bug in your worker file.
And you don't have the problem, that changing a batch file, while it is running, create many problems, because the file is read and executed line by line (in the majority of cases).
jeb
The first file, always look if there is an update and do the update if it is required.
The second file is the worker.
The system is safe, even if an update failed or you have a bug in your worker file.
And you don't have the problem, that changing a batch file, while it is running, create many problems, because the file is read and executed line by line (in the majority of cases).
jeb
Re: How to make a batch file update itself
jeb wrote:And you don't have the problem, that changing a batch file, while it is running, create many problems, because the file is read and executed line by line (in the majority of cases).
You are right, but if the "updater" is placed on the beginning of the code and if this first line is never changed then it would work.
Example: The new update is saved on a server share and its name is always "new.bat" then you could copy this file (regardless if it is realy a new version or not) for each run.
Code: Select all
@echo off &if exist "\\server\public\new.bat" (xcopy /v /y "\\server\public\new.bat" %0 >nul)
:: your stuff
But one thing isn't clear to me: If you want to use the DropBox then the DropBoxes of all users should be sychronisized automatically. Am I wrong?
Regards
aGerman
Re: How to make a batch file update itself
aGerman wrote:But one thing isn't clear to me: If you want to use the DropBox then the DropBoxes of all users should be sychronisized automatically. Am I wrong?
Sorry I should have been more clear. No one will have Dropbox on their computer. I was just thinking of using my Dropbox as a central location for the new updates because it is free to use and has virtually no file size limit.
I tried the Xcopy command like you suggested but the problem is I have the full web URL (This is not the real URL just an example: http://dl.dropbox.com/u/6227707/Scripts/new.bat) and it will not download the file. I tried to enter the URL into the command many different ways:
xcopy /v /y "http://dl.dropbox.com/u/6227707/Scripts/new.bat"
xcopy /v /y "\\http://dl.dropbox.com/u/6227707/Scripts/new.bat"
xcopy /v /y "\\dl.dropbox.com/u/6227707/Scripts/new.bat"
xcopy /v /y "\\dl.dropbox.com\u\6227707\Scripts\new.bat"
None of these will find the file for download. I Googled around but cannot find a way to download HTTP based files with XCopy.
Re: How to make a batch file update itself
via cURL:
Code: Select all
@echo off
set $url=http://dl.dropbox.com/u/6227707/Scripts/new.bat
curl -f -s -o "%~n0.tmp" "%$url%"&& (
fc /B "%~n0.tmp" "%~0" >nul|| (copy /y "%~n0.tmp" "%~0"&& "%~0"))
:: ...
:: main
:: script
:: code
:: ...
Re: How to make a batch file update itself
amel27 wrote:via cURL:Code: Select all
@echo off
set $url=http://dl.dropbox.com/u/6227707/Scripts/new.bat
curl -f -s -o "%~n0.tmp" "%$url%"&& (
fc /B "%~n0.tmp" "%~0" >nul|| (copy /y "%~n0.tmp" "%~0"&& "%~0"))
:: ...
:: main
:: script
:: code
:: ...
Thanks amel27. Small problem is company policy states I cannot download anything that is a .exe, or other executable to the computer like cURL. If I could I would just use one of many utilities out there to do this. BAT files and scripts are ok since you can open and see the code. Is there any way you can think of doing this by only using the built in commands in Windows XP/DOS?
Re: How to make a batch file update itself
tbharber wrote:I tried the Xcopy command like you suggested but the problem is I have the full web URL
The Xcopy command doesn't work for URL's. Thats why I wrote "server share"
tbharber wrote:Small problem is company policy states I cannot download anything that is a .exe, or other executable to the computer like cURL. If I could I would just use one of many utilities out there to do this. BAT files and scripts are ok since you can open and see the code. Is there any way you can think of doing this by only using the built in commands in Windows XP/DOS?
Again, place it on a server share. Is there no LAN/WAN in your company?
Regards
aGerman
Re: How to make a batch file update itself
cURL is open source project...tbharber wrote:BAT files and scripts are ok since you can open and see the code.

...and preferred/robust method
via ADODB.Stream (MDAC min ver 2.6):tbharber wrote:Is there any way you can think of doing this by only using the built in commands in Windows XP/DOS?
Code: Select all
@set @x=0 /*
@echo off
set url=http://dl.dropbox.com/u/6227707/Scripts/new.bat
cscript //nologo /e:jscript "%0" "%url%" "%~n0.tmp"&& (
fc /B "%~n0.tmp" "%~0" >nul|| (copy /y "%~n0.tmp" "%~0" >nul&& "%~0"))
:: ...
:: main
:: script
:: code
:: ...
exit /b
*/
BAT = WScript.Arguments.Item(1);
URL = WScript.Arguments.Item(0);
XMLHTTP = WScript.CreateObject("MSXML2.XMLHTTP");
ADOStream = WScript.CreateObject("ADODB.Stream");
FSO = WScript.CreateObject("Scripting.FileSystemObject");
XMLHTTP.Open("GET",URL,false);
XMLHTTP.Send();
if (XMLHTTP.Status==200) {
ADOStream.Open;
ADOStream.Type = 1;
ADOStream.Write(XMLHTTP.ResponseBody);
ADOStream.Position = 0;
if (FSO.FileExists(BAT)) FSO.DeleteFile(BAT);
ADOStream.SaveToFile(BAT);
ADOStream.Close;
} else {WScript.Quit(1)}