2015-04-03 34 views
0

我想將最新創建或編輯的最新文件從一個目錄移動到兩臺不同服務器上的另一個文件夾。我怎樣才能將最新的文件從一個目錄移動到下一個目錄,而不是文件夾中的所有文件?只將最新的文件從一臺服務器移動到另一臺服務器

這是我用來移動文件的代碼。

My.Computer.FileSystem.CopyDirectory("\\172.16.1.42\s$\SQLBackup\FWP", "\\172.16.1.22\F$\BackupRestore", True) 

回答

1

確定哪個文件是最新的文件不應該太困難。一個簡單的方法是獲取有關目錄中所有文件的信息,然後循環查找最新的文件。

你可以做這樣的事情:

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 

希望這可以解決您的問題。如果沒有,請告訴我。這個例程檢測隱藏文件可能存在問題,所以如果你看到類似的東西,請回到這裏。例如,您還需要添加代碼來檢測是否找不到新文件。

+0

謝謝你。會試試看。, – 2015-04-03 17:11:37

相關問題