merge two csv files

I have two sample .csv files (attached).  I would like to see if there is a way I can merge them together.  The challenge is the data fields are not exactly the same in both files. The only data I want to include in the new merge file is State, Zip and Email address.  Any idea on how this can be done in either Windows Batch or VBScript?   Thanks for any info.
file1.csv
file2.csv
LVL 3
fjkaykr11Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ltlbearand3Commented:
It depends on a few items on how we proceed.  I am assuming that you just want all the lines from the first file combined with all the lines from the second file with just the data from the appropriate columns.  If so, then give this script a try.  Copy the text into notepad and save as a .vbs file.  Update the correct fields and then you can run the script.

' ExpertExchange Question ID 28488990
' http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28488990.html
' Expert: ltlbearand3 [http://www.experts-exchange.com/M_2469312.html]
'
Dim objFSO, objReadFile, ObjNewFile, arrLine

' Create the File System Object for Reading and Writing Files.Count
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Create the New Merged File
' ********  UPDATED WITH THE CORRECT FILE NAME AND PATH *********
Set ObjNewFile = objFSO.CreateTextFile("W:\EE Test\Q_28488990 Merge two CSV Files\newfile.csv")


' Read from the first file
' ********  UPDATED WITH THE CORRECT FILE NAME AND PATH *********
Set objReadFile = objFSO.OpenTextFile ("W:\EE Test\Q_28488990 Merge two CSV Files\file1.csv", 1) ' 1 is for Reading

' Loop Through the file to find all the data
Do Until objReadFile.AtEndOfStream
	' Split this data into an array to read each column
    arrLine = Split(objReadFile.Readline, ",")
    
    ' Array is zero based.  Write out columns 4-6 into the new file
    objNewFile.WriteLine arrLine(3) & "," & arrLine(4) &  "," & arrLine(5)
Loop
objReadFile.Close

' Read from the first file
' ********  UPDATED WITH THE CORRECT FILE NAME AND PATH *********
Set objReadFile = objFSO.OpenTextFile ("W:\EE Test\Q_28488990 Merge two CSV Files\file2.csv", 1) ' 1 is for Reading

' We are skipping the first line as we don't want the header line again.
If not objReadFile.AtEndOfStream then objReadFile.Readline

' Loop Through the file to find all the data
Do Until objReadFile.AtEndOfStream
	' Split this data into an array to read each column
    arrLine = Split(objReadFile.Readline, ",")

    ' Array is zero based.  Write out columns 2-4 into the new file
    objNewFile.WriteLine arrLine(1) & "," & arrLine(2) &  "," & arrLine(3)
Loop

' Clean Up
objReadFile.Close
objNewFile.Close
Set objReadFile = nothing
Set ObjNewFile = nothing
Set objFSO = nothing

msgbox "Done"

wscript.quit

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fjkaykr11Author Commented:
@ltlbearand3, brilliant, it works perfect.  I have some other adjustments that I need to try to figure out. I will post another question if I get stuck.  Thanks so much.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.