Replace the local file with the new version found on the file server with vbs

Source: Internet
Author: User

Q:
Hello, script expert! How can I compare the modification date of a local file with the copy on the file server, and replace it with the version on the file server if the version of the local file is older?
-- DC
A:
Hello, DC. Let's think about it. You want to replace some old things with new ones. We don't know why. This makes us feel uneasy.
Well, it's not because the scripting experts are getting older. We have to remind you that we didn't. After all, the knees and back should be so squeaking; otherwise, how can we know that they are working ?!?
Note: It is said that when the eskimoans are old and useless, they will be abandoned to take over from themselves in the big ice float. Fortunately, none of the scripting experts are Eskimo, And we wear our coats, gloves, and long hats every day (including summer ). Just in case.
However, you may be more concerned about outdated files than outdated scripting experts, right? The script below replaces the local file c: \ scripts \ test.txt (if the file is older than its copy on the server atl-fs-01 ): CopyCode The Code is 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 short script. (We must admit that it is really easy to remove old things that are somewhat confusing to us .) First, create a constant named overwriteexisting and set its value to true. We will use this constant to tell FileSystemObject to overwrite existing instances of the target file. By default, if a file already exists on drive 2, FileSystemObject will not copy it from drive 1 to drive 2.
Speaking of FileSystemObject, we create an instance of this object (scripting. FileSystemObject) in the next line of this script. Then, we use the following two lines of code to bind to our first file (C: \ scripts \ test.txt), and the last modification date (datelastmodified attribute) of this file) stored in the variable named dtmlocaldate:
Set objlocalfile = objfso. GetFile ("C: \ scripts \ test.txt ")
Dtmlocaldate = objlocalfile. datelastmodified
Then, repeat this process by creating an object reference to the server version test.txt. Note that the variables used here have different names: Object references of local files are stored in objlocalfile, and object references of server files are stored in objserverfile. Needless to say, we also use different variables (dtmserverdate) to store the last modification date:
Set objserverfile = objfso. GetFile ("\\atl-fs-01 \ public \ test.txt ")
Dtmserverdate = objserverfile. datelastmodified
Are you still watching? Next, determine whether 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 this syntax. It is easy to assume that the date of the old file should be greater than that of the new file. But in fact, the date is not handled in this way. Assume that "file a" is modified on 2/1/2006, and "file B" is modified on 2/15/2006. "File a" is old, which means that the modification date of "file a" is smaller than (that is, the generated date is earlier than) "file B ".
If the local file is older than the server copy, how can I replace the local file with the copy found on the server? The method is as follows:
Objfso. copyfile objserverfile. Path, objlocalfile. Path, overwriteexisting
As you can see, all we need to do is call the copyfile method and pass three parameters to it:
? The path of the file we want to copy (test.txt version found on the server.
? We want to copy this file to the path. In this example, It is the path of the local file.
? Constant overwriteexisting, which is used to tell the script to continue and replace the local file with the version copied from the server.
So far, you have achieved your goal. However, remember that this script only applies to old files. You cannot use it to replace it. Well, it's an old scripting expert. (Of course, no one has ever dreamed of replacing the scripting experts. However, 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.