Import text files

  • Thread starter Thread starter little_rascals
  • Start date Start date
L

little_rascals

Hi, i need to import data from 6 different text files into
6 different access tables.

The problem now is, there is a header at the top of each
text file, which includes the file name and date of the
file. I need to extract the date only and input it in one
of the fields created in the database table. I need the
data from all the text files together with the dates in
the database tables.

Can anyone help me to solve this?
Thanks

Cheers,
little_rascals
 
Hi,

I'd do this by munging the text files so as to omit the header and
insert the date at the beginning of each line. Here is a little VBScript
that should do the job, perhaps after a little modification.

Meanwhile, you little_rascals behave yourselves, or I'll tell your
mothers.

'Prepend.vbs
'VBScript
'Call from commandline as
' cscript Prepend.vbs GroupTag InFile OutFile
'where GroupTag is the string that begins a group.
'E.g. if the data is
'
'Group # 1
'First Line
'Second Line
'Group # 2
'First Line
'Second Line
'Third Line
'
'use a command line like
' cscript Prepend.vbs "Group # " Infile.txt Outfile.txt
'to get the output
'1,First Line
'1,Second Line
'2,First Line
'2,Second Line
'2,Third Line


Option Explicit

Dim fso 'As FileSystemObject
Dim fIn 'As TextStream
Dim fOut 'As TextStream
Dim strLine 'As String
Dim strTest 'As String
Dim strTag 'As String
Dim DELIM 'As String

DELIM = "," 'String to separate the "group" data
'from the rest of the line. Change this
'as needed: e.g. to Chr(9) for Tab

Set fso = CreateObject("Scripting.FileSystemObject")
strTest = WScript.Arguments(0)
Set fIn = fso.OpenTextFile(WScript.Arguments(1))
Set fOut = fso.CreateTextFile(WScript.Arguments(2))

Do Until fIn.AtEndOfStream
strLine = fIn.ReadLine
If Left(strLine, Len(strTest)) = strTest Then
'this line starts a group so store its value
strTag = Mid(strLine, Len(strTest)+1)
Else
fOut.Write strTag & DELIM & StrLine & Chr(13) & Chr(10)
End If
Loop

fIn.Close
fOut.Close
'script ends
 
Back
Top