const FOLDER1 = "c:\temp\f1"
const FOLDER2 = "c:\temp\f2"
const LOG_FILE = "c:\temp\output.log"
Set fsoLog = CreateObject("Scripting.FileSystemObject")
set objLog = fsoLog.CreateTextFile(LOG_FILE,2)
dim fileName,compFileName
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set FileList = objWMIService.ExecQuery("ASSOCIATORS OF {Win32_Directory.Name='" & FOLDER1 & "'} Where ResultClass = CIM_DataFile")
For Each objFile In FileList
If objFile.Extension = "csv" Then
fileName = objFile.Drive & objFile.Path & objFile.FileName & "." & "csv"
compFileName = FOLDER2 & "\" & objFile.FileName & "." & "csv"
if IsFileExists(compFileName) then
CompareCsvFile fileName, compFileName
end if
End If
Next
objLog.Close
set objLog = Nothing
set fsoLog = Nothing
function IsFileExists(fileName)
DIM fso
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists(fileName)) Then
IsFileExists=true
Else
IsFileExists=false
End If
End function
sub CompareCsvFile(fileName, compFileName)
dim fs,objTextFile,data1,data2
set fs = CreateObject("Scripting.FileSystemObject")
dim arrStr,col_values1,col_values2,arr1,arr2
set objTextFile = fs.OpenTextFile(fileName)
'read column 5 data from first csv file
Do while NOT objTextFile.AtEndOfStream
arrStr = split(objTextFile.ReadLine,",")
if UBound(arrStr) < 4 then
outputLogResult fileName, compFileName, false
exit sub
end if
if col_values1 = "" then
col_values1 = arrStr(4)
else
col_values1 = col_values1 & "," & arrStr(4)
end if
Loop
objTextFile.Close
'read column 5 data from second csv file
set objTextFile = fs.OpenTextFile(compFileName)
Do while NOT objTextFile.AtEndOfStream
arrStr = split(objTextFile.ReadLine,",")
if UBound(arrStr) < 4 then
outputLogResult fileName, compFileName, false
exit sub
end if
if col_values2 = "" then
col_values2 = arrStr(4)
else
col_values2 = col_values2 & "," & arrStr(4)
end if
Loop
objTextFile.Close
arr1 = split(col_values1,",")
arr2 = split(col_values2,",")
'check if has the same number of rows
if UBound(arr1) <> UBound(arr2) then
outputLogResult fileName, compFileName, false
exit sub
end if
For count = 0 to UBound(arr1)
'check if data is identical
data1=arr1(count)
data2=arr2(count)
if data1 <> data2 then
outputLogResult fileName, compFileName, false
exit sub
end if
Next
set objTextFile = Nothing
set fs = Nothing
outputLogResult fileName, compFileName, true
End sub
sub outputLogResult(fileName, compFileName, result)
if result = true then
objLog.WriteLine "Identical files:"
else
objLog.WriteLine "Unidentical files:"
end if
objLog.WriteLine fileName
objLog.WriteLine compFileName
objLog.WriteLine
end sub
|