Replace the local file with a VBS with a new version found on the file server _vbs

Source: Internet
Author: User
Tags constant
Ask:
Hello, Scripting Guy! How do I compare the modified date of a local file to a copy on a file server, and replace it with a version on the file server if the local file is older?
--DC
For:
Hello, DC. Let's think about it, you want to replace something old with a new one. I don't know why, it makes us feel uneasy.
Well, not because the Scripting Guys are getting older, it's a reminder that we don't. After all, the knees and back should be so squeaky, how else would you know they're playing a role?!?
Note: It is said that when the Eskimos are too old to use, they will be abandoned on a large block of ice to fend for themselves. Luckily, the Scripting Guys are not Eskimos, and we wear coats, gloves and hats every day (including summer). Case.
But you might be more concerned about outdated files than the outdated Scripting Guys, right? The following script replaces the local file C:\Scripts\Test.txt (if this file is just older than its copy on the server atl-fs-01):
Copy Code code as follows:

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 very brief little script. (We have to admit that it's really easy to get rid of obsolete old stuff that bothers us a little.) We first create a constant named OverwriteExisting and set its value to True. We will use this constant to tell FileSystemObject to overwrite an existing instance of the target file. By default, if a file already exists on drive 2, FileSystemObject does not copy it from drive 1 to drive 2.
Speaking of FileSystemObject, we create an instance of this object (Scripting.FileSystemObject) on the next line of this script. We then use the following two lines of code to bind to our first file (C:\Scripts\Test.txt) and store the last modified date (DateLastModified property) of this file in a variable named dtmlocaldate:
Set objlocalfile = Objfso.getfile ("C:\Scripts\Test.txt")
Dtmlocaldate = objlocalfile.datelastmodified
This procedure is then repeated by creating an object reference to the server version Test.txt. Note that the variable name we use here is different: the object reference for the local file is stored in Objlocalfile, and the object reference for the server file is stored in the objserverfile. Needless to say, we also used a different variable (dtmserverdate) to store this last modified date:
Set objserverfile = Objfso.getfile ("\\atl-fs-01\public\test.txt")
Dtmserverdate = objserverfile.datelastmodified
Are you still watching? Next we need to determine if the local file is older than the server file. The following line of code is used to solve this problem:
If Dtmlocaldate < Dtmserverdate Then
Don't get confused by the grammar. It is easy to assume that the date of the old file should be greater than the new file. But that's not how the date is actually handled. It is assumed that the modification date for "file A" was 2/1/2006, and that "document B" was modified by 2/15/2006. "File A" is older, which means that the modification date of "file a" is less than (that is, the resulting earlier than) "File B".
So how do I replace a local file with a copy found on the server if the local file is older than its server copy? The method is as follows:
Objfso.copyfile Objserverfile.path, Objlocalfile.path, overwriteexisting
As you can see, all we have to do is call the CopyFile method and pass three parameters to it:
? The path to the file (that is, the Test.txt version found on the server) that we want to copy.
? The path to which we want to copy this file. In this case, the path to the local file.
? Constant OverwriteExisting, which tells the script to proceed and replaces the local file with a version copied from the server.
At this point you have achieved your purpose. However, keep in mind that this script applies only to old files. You can't use it to replace, uh, the old Scripting Guy. (Of course, it's not someone who dreamed of replacing the Scripting Guys.) But just in case ... )

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.