|
strFolderPath1 = "C:\test1" 'The first folder you want search from
strFolderPath2 = "C:\test2" 'The second folder you want search from
strSameFiles = ""
Set FSO = CreateObject("scripting.FileSystemObject")
'If the first folder doesnot exists, input the folder path
If FSO.FolderExists(strFolderPath1) Then
Set FS1 = FSO.GetFolder(strFolderPath1)
Else
MsgBox "Folder " & strFolderPath1 & " does not exist!"
strFolderPath1 = InputBox("Input first folder you wanna search", "Input the folder path")
Set FS1 = FSO.GetFolder(strFolderPath1)
End If
'If the second folder doesnot exists, input the folder path
If FSO.FolderExists(strFolderPath2) Then
Set FS2 = FSO.GetFolder(strFolderPath2)
Else
MsgBox "Folder " & strFolderPath2 & " does not exist!"
strFolderPath2 = InputBox("Input second folder you wanna search", "Input the folder path")
Set FS2 = FSO.GetFolder(strFolderPath2)
End If
SearchSame FS1, FS2
Function SearchSame(objFolder1, objFolder2)
If objFolder1.SubFolders.Count <> 0 Then
For Each subFolder1 In objFolder1.SubFolders
strFolderName1 = subFolder1.Name
For Each subFolder2 In objFolder2.SubFolders
If strFolderName1 = subFolder2.Name Then
SearchSame subFolder1, subFolder2
End If
Next
Next
End If
SearchSameFile objFolder1, objFolder2
End Function
Function OutputTheResult(strFolderPath1, strFolderPath2, strResult)
If strResult <> "" Then
MsgBox "There is same files in folder: " & strFolderPath1 & " and folder: " & strFolderPath2
For Each strFileName In Split(strResult, "|")
If strFileName <> "" Then
MsgBox strFileName
End If
Next
Else
MsgBox "There is no same file in folder: " & strFolderPath1 & " and folder: " & strFolderPath2
End If
End Function
Function SearchSameFile (objFolder1, objFolder2)
strSameFiles = ""
For Each file1 In objFolder1.Files
strFileName1 = file1.Name
For Each file2 In objFolder2.Files
If strFileName1 = file2.Name Then
strSameFiles = strFileName1 & "|" & strSameFiles
Exit For
End If
Next
Next
OutputTheResult objFolder1.Path, objFolder2.Path, strSameFiles
End Function
Set FS1 = Nothing
Set FS2 = Nothing
Set FSO = Nothing
昨天时间有点紧,没有写比较子文件夹的,这个基本比较功能实现了,比较文件名字相同的. |
|