2011年3月3日 星期四

比對A,B目錄內的檔案,並做更新或刪除...

Option Explicit

Private Sub Command1_Click()
Dim FSO As Object
Dim Files1 As Object
Dim Files2 As Object
'使用檔案系統物件開檔
Set FSO = CreateObject("scripting.FileSystemObject")
Dim strFolder1 As String
Dim strFolder2 As String
Dim blnHave As Boolean

strFolder1 = "c:\temp\1" '目錄1可改成TextBox 輸入
strFolder2 = "c:\temp\2" '目錄2可改成TextBox 輸入
Set Files1 = FSO.GetFolder(strFolder1).Files
Set Files2 = FSO.GetFolder(strFolder2).Files
ChkFile Files1, Files2
ChkFile Files2, Files1

End Sub

Private Sub ChkFile(ByVal Files1 As Files, ByVal Files2 As Files)
Dim File1 As File
Dim File2 As File
Dim blnHave As Boolean

For Each File1 In Files1
blnHave = False
For Each File2 In Files2
'檢查檔案名稱一不一樣
If File1.Name = File2.Name Then
blnHave = True
Exit For
End If
Next
If blnHave Then
'檢查修改日期一不一樣
If File1.DateLastModified > File2.DateLastModified Then
FileCopy File1.Path, File2.Path
End If
Else
Kill File1.Path
End If
Next

End Sub

沒有留言:

張貼留言