只将最新的文件从一台服务器移动到另一台服务器
问题描述:
我想将最新创建或编辑的最新文件从一个目录移动到两台不同服务器上的另一个文件夹。我怎样才能将最新的文件从一个目录移动到下一个目录,而不是文件夹中的所有文件?只将最新的文件从一台服务器移动到另一台服务器
这是我用来移动文件的代码。
My.Computer.FileSystem.CopyDirectory("\\172.16.1.42\s$\SQLBackup\FWP", "\\172.16.1.22\F$\BackupRestore", True)
答
确定哪个文件是最新的文件不应该太困难。一个简单的方法是获取有关目录中所有文件的信息,然后循环查找最新的文件。
你可以做这样的事情:
Imports System.IO
Dim di As New IO.DirectoryInfo("c:\") ' Change this to match your directory
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo
Dim mostRecentFile As IO.FileInfo = Nothing
Dim mostRecentTimeStamp As DateTime = Nothing
DateTime.TryParse("01/01/1900 0:00:00", mostRecentTimeStamp) ' Set to early date
For Each dra In diar1 ' Cycle through each file in directory
If File.GetLastAccessTime(dra.FullName) > mostRecentTimeStamp Then
mostRecentTimeStamp = File.GetLastAccessTime(dra.FullName)
mostRecentFile = dra
End If
Next
Debug.Print(mostRecentFile.FullName) ' Will show you the result
' Use mostRecentFile.Copy to copy to new directory
希望这可以解决您的问题。如果没有,请告诉我。这个例程检测隐藏文件可能存在问题,所以如果你看到类似的东西,请回到这里。例如,您还需要添加代码来检测是否找不到新文件。
谢谢你。会试试看。, – 2015-04-03 17:11:37