Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combine three csv files 2

Status
Not open for further replies.

malaygal

IS-IT--Management
Feb 22, 2006
192
US
I need help in combining three cvs files.
I am new with vbscripting and I do not know where to begin.

Any help will be greatly appreciated.
 
csv or cvs

csv is a spreadsheet format
cvs is a source code control system
 
Sorry, it should be csv (Comma-separated). They are all in the same format.
 
The gist of it is

1) Create a new file
2) Open file 1,
3) Copy all of file 1 to new file
4) Open file 2
5) Copy all of file to to new file starting from position after last position used by file 1
6) Open file 3
7) Copy all of file to new file starting from position after last position used by file 2.
8) Save file created in 1 under a new name
9) close all open files

Have a look at on how to open excel files.

Alternatively, in dos

copy file1.csv+file2.csv+file3.csv combined.csv

but you could be a masochist and do it the vbscript way.
 
Actually, I need to write a vbscript code for this.
I have to combine all these csv files, manipulate the newly-created file (vlookup) and load this file to our Oracle database.

The script to manipulate and load the file is in place.
 
You might try something like this...


Code:
Option Explicit

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

' define csv files that will be read
Dim arrCSVFiles : arrCSVFiles = Array("c:\temp\csv_file1.csv", _
									  "c:\temp\csv_file2.csv", _
									  "c:\temp\csv_file3.csv")
' define combined csv file
Dim outCSVFile : outCSVFile = "c:\temp\combined.csv"

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
' open output csv file
Dim objOutFile : Set objOutFile = objFSO.OpenTextFile(outCSVFile, ForWriting, True)

Dim csvFile, objFile, strTemp
' loop through the list of csv files to read
For Each csvFile In arrCSVFiles
	' open file for reading
	Set objFile = objFSO.OpenTextFile(csvFile, ForReading)
	Do Until objFile.AtEndOfStream
		strTemp = objFile.ReadLine
		If Not strTemp = "" Then
			' write line to combined csv file
			objOutFile.WriteLine strTemp
		End If
	Loop
	' close current opened csv file
	objFile.Close
Next

' close combined csv file
objOutFile.Close

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Alternatively if the files are not very big, replace the inner do loop with
Code:
strTemp = objFile.ReadAll
objOutFile.Write strTemp
 
having the problems while opening a .csv file thru excel. Its saying that it cannot open it properly as the .csv file got more than 65K of lines in it. If anyone know please letme know. Thanks
 
you can do it in access. your best bet would be to use ado and sql to read the file, rather than fso.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top