Question : Vbs script that can check 2 folders that has identical csv files and compare colum "C" and get just the file names that has the exact data in the colum in both files.

Hi,
Vbs script that can check 2 folders that has identical csv files and compare colum "C" and get just the file names that has the exact data in the colum in both files.

Regards
Sharath

Answer : Vbs script that can check 2 folders that has identical csv files and compare colum "C" and get just the file names that has the exact data in the colum in both files.

check it:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
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
Random Solutions  
 
programming4us programming4us