Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use vbs to replace the local file with the new version found on the file server

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use vbs to replace the local file with the new version found on the file server, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.

Q:

How do I compare the modification date of a local file with a copy on the file server and, if the version of the local file is older, replace it with the version on the file server?

A:

Let's see, you want to replace something old with something new. I don't know why, it makes us feel very uneasy.

Well, it's not because script experts are getting older, which reminds you, we don't. After all, that's how the knees and back are supposed to squeak; how else would you know they're working?

Note: it is said that when Eskimos are too old to be useful, they are left to fend for themselves on a large piece of ice floe. Fortunately, script experts are not Eskimos, and we wear coats, gloves and long hats to work every day (including summer). Just in case.

But you probably care more about outdated files than outdated scripting experts, right? The following script replaces the local file C:\ Scripts\ Test.txt (if it happens to be older than its copy on the server atl-fs-01):

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 little script. We have to admit that it's really easy to get rid of the outdated stuff that bothers us a little bit. We first create a constant named OverwriteExisting and set its value to True. We will use this constant to tell FileSystemObject that the existing instance of the target file can be overwritten. By default, FileSystemObject does not copy a file from drive 1 to drive 2 if it already exists on drive 2.

Speaking of FileSystemObject, we create an instance of this object (Scripting.FileSystemObject) on the next line of this script. Then we bind to our first file (C:\ Scripts\ Test.txt) using the following two lines of code, and store the last modified date of this file (the DateLastModified property) in a 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 of Test.txt. Note that the variable names we use here are different: the object reference for the local file is stored in objLocalFile, and the object reference for the server file is stored in objServerFile. Needless to say, we also used different variables (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 whether the local file is older than the server file. The following code is used to solve this problem:

If dtmLocalDate < dtmServerDate Then

Don't be confused by the grammar here. It is easy to assume that the date of the old file should be greater than that of the new one. But that's not how dates are actually handled. It is assumed that the modification date of "file A" is 2max 1max 2006, and that of "file B" is 2pm 15max 2006. "file A" is relatively old, which means that the modification date of "file A" is less than (that is, generated earlier than) "file B".

So if the local file is older than its server copy, how do I replace the local file with the copy found on the server? The methods are 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 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 to which we want to copy this file. In this case, the path to the local file.

? The constant OverwriteExisting, which is used to tell the script to proceed and replace the local file with the version copied from the server.

So far you have achieved your goal. Keep in mind, however, that this script applies only to old files. You cannot use it to replace it.

Thank you for reading this article carefully. I hope the article "how to use vbs to replace local files with the new version found on the file server" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and follow the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report