0

I have to write vbscript to compare two csv files,

The both csv files contains the following data format, File1.csv

DBNane UserGroup Path                 Access
DB_1    Dev_II   DB/Source/Projects   Read/Write
DB_2    Test_I   DB/Source/Doc        Read

File2.csv

DBNane UserGroup Path                 Access
DB_1    Dev_II   DB/Source/Projects   Read
DB_2    Test_I   DB/Source/Doc        Read

I need to compare these files, the output format is like,

File3.csv

DBNane UserGroup Path                 Access
DB_1    Dev_II   DB/Source/Projects   Read/Write

I'm new to the vbscript. Any sample script to do this ? Thanks.

user1553605
  • 1,333
  • 5
  • 24
  • 42
  • see http://stackoverflow.com/a/6192244/603855 for an SQL strategy to solve problems like this; you should at least hint at the reason for *not* choosing the data from file2.csv. – Ekkehard.Horner Aug 07 '13 at 13:28
  • Is it required to be VBScript? It would be pretty easy with PowerShell. You could also look at using something like WinMerge if it doesn't have to be automated. – alroc Aug 07 '13 at 13:34
  • @alroc can u plz provide me the example code to compare two excel files using powershell – user1553605 Aug 07 '13 at 14:16
  • Excel files, or CSV files? It's 2 completely different solutions. – alroc Aug 07 '13 at 14:19
  • 2
    @user1553605 You need to explain *why* `File3.csv` has the content that it does. That are your rules/criteria? – alroc Aug 07 '13 at 14:21

1 Answers1

2

In PowerShell you could get differing lines from 2 text files like this:

$f1 = Get-Content 'C.\path\to\file1.csv'
$f2 = Get-Content 'C.\path\to\file2.csv'

Compare-Object $f1 $f2

If you only need to show what's different in the first file ($f1), you could filter the result like this:

Compare-Object $f1 $f2 | ? { $_.SideIndicator -eq '<=' } | % { $_.InputObject }
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328