How Can I Replace a Local File with a Newer Version Found on a File Server?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I compare the modification date of the local file with its counterpart on a file server and then, if the local file is older, replace it with the version on the file server?

— DC

SpacerHey, Scripting Guy! AnswerScript Center

Hey, DC. Let’s see, you want to take something old and replace it with something new. For some reason, that sends a shiver up and down our spines.

Um, not because the Scripting Guys are getting old, mind you; we’re not. After all, knees and backs are supposed to creak like that; how else would you know that they’re working?!?

Note. According to legend, when Eskimos become old and useless they are abandoned on an ice floe and left to fend for themselves. None of the Scripting Guys happens to be an Eskimo, but we all tend to wear coats, gloves, and stocking hats to work every day, summer included. Just in case.

But you’re probably more concerned with out-of-date files than you are with out-of-date Scripting Guys, aren’t you? Here’s a script that will replace the local file C:\Scripts\Test.txt if that file happens to be older than its counterpart on the server atl-fs-01:

Const OverwriteExisting = TRUE

Set objFSO = CreateObject(“Scripting.FileSystemObject”)

Set objLocalFile = objFSO.GetFile(“c:\scripts\test.txt”) dtmLocalDate = objLocalFile.DateLastModified

Set objServerFile = objFSO.GetFile(“\\atl-fs-01\public\test.txt”) dtmServerDate = objServerFile.DateLastModified

If dtmLocalDate < dtmServerDate Then objFSO.CopyFile objServerFile.Path, objLocalFile.Path, OverwriteExisting End If

As you can see, this is a pretty simple little script. (We have to admit that the fact that it’s really easy to get rid of old, worn-out things worries us a little bit.) We start out by creating a constant named OverwriteExisting and set the value to True; we’ll use this constant to tell the FileSystemObject that it’s OK to overwrite an existing instance of our target file. By default, the FileSystemObject will not copy a file from drive 1 to drive 2 if that file already exists on drive 2.

Speaking of the FileSystemObject, we create an instance of this object (Scripting.FileSystemObject) on the next line of the script. We then use these two lines of code to bind to our first file (C:\Scripts\Test.txt) and store the last modified date of the file (the DateLastModified property) in a variable named dtmLocalDate:

Set objLocalFile = objFSO.GetFile(“c:\scripts\test.txt”)
dtmLocalDate = objLocalFile.DateLastModified

We then repeat this process by creating an object reference to the server-version of Test.txt. Notice that we use a different variable name here: the object reference to the local file is stored in objLocalFile and the object reference to the server file is stored in objServerFile. Needless to say, we also use a different variable (dtmServerDate) to store the last modification date:

Set objServerFile = objFSO.GetFile(“\\atl-fs-01\public\test.txt”)
dtmServerDate = objServerFile.DateLastModified

Still with us? Next we need to determine whether the local file is older than the server file. That’s what this line of code is for:

If dtmLocalDate < dtmServerDate Then

Don’t be confused by the syntax here. It’s easy to assume that the older file would have a date greater than the newer file. But that’s not how things work with dates. Suppose File A has a modification date of 2/1/2006 and File B has a modification date of 2/15/2006. File A is older, which means its modification date is less than (that is, occurred before) File B.

So suppose our local file is older than its server counterpart. How do we replace the local file with the copy found on the server? Here’s how:

objFSO.CopyFile objServerFile.Path, objLocalFile.Path, OverwriteExisting

As you can see, all we have to do is call the CopyFile method and pass it three parameters:

•

The path to the file we want to copy (that is, the version of Test.txt found on the server).

•

The path we want to copy this file to. In this case, that will be the path to the local file.

•

The constant OverwriteExisting, which tells the script to go ahead and replace the local file with the version copied over from the server.

And there you have it. Remember, though, this script works only with old files. You can’t use it to replace, say, old Scripting Guys. (Not that anyone would ever even dream of replacing the Scripting Guys, of course. But, just in case ….)

0 comments

Discussion is closed.

Feedback usabilla icon