How does vbs copy only files that are newer than the target file?
This article will explain in detail how vbs can only copy files that are newer than the target file. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Because the website needs to update the home page frequently, sometimes the use of cdn often causes the home page to synchronize data when the content is being generated (it may be a conflict, often causing the home page to be empty), so think of Mr. into an index2.htm that is not the home page and then copy it as index.htm again, so that the frequent reading and writing of index2 is no problem. So copy only when index2.htm is not empty, and only when it is newer than index.htm. This requires some script support.
Let's start with a bat version.
The code is as follows:
@ echo off
:: copy the following home page every 5 minutes
For / f% I in ('dir / b c:\ index2.htm') do (
Set indexdx=%%~zi
)
If% indexdx% gtr 5120 (
Echo y | xcopy c:\ index2.htm / d / r / k c:\ index.htm
)
Where for / f% I in ('dir / b c:\ index2.htm') do (
Set indexdx=%%~zi
)
Gets the index2.htm file size in bat.
Then through if% indexdx% gtr 5120 (
Implement to determine whether it is greater than 5120 bytes
The main reason is that the following code is more powerful and simple.
The code is as follows:
Dim fso
Set fso = CreateObject ("Scripting.FileSystemObject")
Set fn2=fso.GetFile ("c:\ index2.htm")
Flsize2=fn2.size
Fldate2=fn2.datelastmodified
Set fn=fso.GetFile ("c:\ index.htm")
Flsize1=fn.size
Fldate1=fn.datelastmodified
If fso.FileExists ("c:\ index2.htm") and flsize2 > 50000 and fldate2 > fldate1 Then
Fso.getfile ("c:\ index2.htm") .copy ("c:\ index.htm")
If err.number=0 then WriteHistory "success" & now (), "log.txt"
End if
Sub WriteHistory (hisChars, path)
Const ForReading = 1, ForAppending = 8
Dim fso, f
Set fso = CreateObject ("Scripting.FileSystemObject")
Set f = fso.OpenTextFile (path, ForAppending, True)
F.WriteLine hisChars
F.Close
End Sub
There is also a log function, which is also available under the current bat. You can expand what you need.
This is the end of the article on "how to copy files that are only newer than the target file". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.