• ARTICLES
SEARCH

How-To Geek

Build Your Own Automated File Update System

Every server administrator has a set of utility programs they like to use. Typically, these tools are kept on each machine to ensure availability and can come from a variety of sources such as Sysinternals and Nirsoft.

To keep these tools updated to the latest versions, manually updating can be quite tedious and time consuming, especially when there are multiple servers in the mix. So we have a solution for you: a simple to set up solution where you maintain a single set of tools and all your machines sync the versions of their tools to this location.

For example, suppose you maintain systems on 3 separate networks and like to have the same set of 10 tools available on all these machines for scripts and maintenance. You would have a master folder of tools (available via a web site) where you maintain the release versions you want distributed to all your systems. All your other systems then “phone home” to this master folder and download the respective version locally. This process provides an automated solution to always making sure your utility versions are consistent across all your systems.

Setting up the Environment

The environment for this to work is pretty easy to set up and, once done, is truly a “set it and forget it” solution.

  1. Create a folder inside of a web site you have access to which will server as the master folder (i.e. ToolsFolder).
  2. Upload all your common utilities to this folder.
  3. On the servers you want to sync to the master site, place the script below into the local folder which holds the tools (and WGet if you do not have it already).
  4. Update the URL to your master folder (i.e. http://my.site.com/ToolsFolder).
  5. Schedule the script to run periodically and all the local tools which exist on master site will be sync’ed.

Alternate Uses

While we have covered a practical use a system admin may have for this type of script, there are many other situations where this script can come in handy:

  • Use to update Sysinternals and Nirsoft utilities on local machines (URL’s are included in the script).
  • Use on a desktop system to keep a set of documents sync’ed with a master copy.

The script supports both hardcoding a URL as well as supplying one from the command line (i.e. UpdateFromWeb http://source.site.com/Stuff.zip), so use your imagination.

The Script

@ECHO OFF
TITLE Update From Web
ECHO Update From Web
ECHO Written by: Jason Faulkner
ECHO SysadminGeek.com
ECHO.
ECHO.

SETLOCAL EnableExtensions

REM Place this script in the folder which contains the utilities to update.
REM Requires WGet to be in the same folder as this script or in a location set in the PATH variable.

REM URL to look for updates on in the event none is specified as a parameter.
REM URL's of interest ---
REM  Sysinternals : http://live.sysinternals.com/tools
REM  NirSoft      : http://www.nirsoft.net/panel
SET URL=http://my.site.com/ToolsFolder

REM If a parameter is specified, use that as the URL.
IF NOT {%1}=={} SET URL=%~1

REM Set this value to 1 to restart tasks that are stopped during the update process.
SET RestartKilledTasks=0


ECHO Detected directory: %~dp0
%~d0
CD %~p0
ECHO.
ECHO.

SET NewFile=.NEW

FOR /F %%A IN ('DIR /B') DO WGet --output-document="%%A%NewFile%" "%URL%/%%A"
REM Delete empty (not found) files.
FORFILES /P . /C "CMD /C IF @fsize==0 DEL /F /Q @path"

SET StartWhenFinished="%TEMP%StartWhenFinished.tmp.txt"
ECHO ;Terminated tools > %StartWhenFinished%

FOR /F %%A IN ('DIR /B') DO (
   IF EXIST "%%A%NewFile%" (
      ECHO Found new version of: %%A
      REM If the file is running, kill it so it can be replaced.
      FOR /F %%B IN ('TASKLIST') DO (
         IF /I [%%A]==[%%B] (
            ECHO %%B is currently running, killing process.
            ECHO %%A >> %StartWhenFinished%
            TASKKILL /IM "%%B" /T /F
         )
      )
      REM Replace with the new version.
      DEL /F /Q "%%A"
      RENAME "%%A%NewFile%" "%%A"
   )
)

IF {%RestartKilledTasks%}=={1} (
   ECHO.
   ECHO Resuming killed tasks
   FOR /F "usebackq skip=1" %%A IN (%StartWhenFinished%) DO (
      ECHO Starting %%A
      START "Restarting %%A" "%%A"
   )
)

IF EXIST "%StartWhenFinished%" DEL "%StartWhenFinished%"

ENDLOCAL

Links

Download UpdateFromWeb Script from SysadminGeek.com

Download WGet

Jason Faulkner is a developer and IT professional who never has a hot cup of coffee far away. Interact with him on Google+

  • Published 08/11/10

Comments (1)

  1. Chris

    This script is awesome. I was wondering, how would you tweak it to work with folders? I have a directory with some exes and a few folders. It works great for the exes, but seems to stumble on the folders. Any help would be much appreciated.

Get Free Articles in Your Inbox!

Join 134,000 newsletter readers

Email:

Go check your email!