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!

Importing / Exporting via Tab-Delimited Text files

Status
Not open for further replies.

StevenK

Programmer
Jan 5, 2001
1,294
GB
I need to establish a means of exporting and importing from a database to a file type that is easily transferred (be it via floppy disk or via e-mail).
For this reason my first thought has been to make use of tab-delimited text files.
I am comfortable (following a refresh of my memory) or reading / writing to a text file but have not dealt with the use of ‘Tab’ characters in text files.
This begs a few questions to get me off the ground with it :

- How do I insert a ‘Tab’ character in a text document (assuming I’d writing line by line).
- How do I recognise a ‘Tab’ character whilst reading the text file (as created above).
- Assuming I can write and read ‘Tab’ characters can I also update / edit this text file format such that I append a further ‘Tab’ character and a ‘Posted’ flag at the end of the appropriate lines (such that when re-reading I can establish those entries that have already been dealt with / posted to the target database).

Any thoughts on the above would be greatly appreciated.

Alternatively is a better option to make use of MS Excel to export out of my source database and then import into my target ?
Would this make my life easier ?

Thanks in advance

Steve
 
its better to use comma seperated (csv) its more common ..

"field1","field2","field3" ect..
or
field1,field2,field3 ect..
 
Use commas.

About tab characters, use #9, eg
s := queryfield.asstring +#9+ otherfield etc.

When it comes to editing its normally better to rewrite the whole data with the new edit in it.

If you want to append to a file, use Append(var F: Text).

Also, during writes to a file, I always stick a Flush after each writeln.

lou
 
I like csv format as aaronjme proposed.
You can then use the separator char defined by variable "ListParameter" inside unit SysUtils.
On my german Windows it's a comma, on an english Windows it should be a comma (you can set it up in Windows' regional settings).
This allows to create a csv file which can also be simply opened by MS-Excel. Be aware of the fact that different users may have different separators.

regards
Roderich
 
sorry for my mistake.
On my german Windows of course the separator is a semicolon, not a comma.

wbr
Rod
 
...also don't forget to remove commas from numbers if they have the thousand separator.
 
commas in tha data is ok as long as you have the quotes to seperate the fields.
"field1,stuff,stuff","field2,morestuff".
 
Roderich, semicolon separator is an (SDF) file.
Semicolon Delimited Format. Anywhere is walking distance if you have the time.
 
hi aaronjme,

using quotes to separate the fields is always a good idea.

I was wondering about the csv format ("Comma separated values"). Formerly my program exported data with a comma separator but then I noticed that Excel works with the separator defined in the regional settings.
This means that on my German Windows M$-Excel imports/exports a csv file with semicolon separators.
This causes problems interchanging the csv file to users with english installation. Anyway my program now uses the regional settings' separator so the same user can import the data with Excel.

best regards
Roderich
 
When using quoted strings in a CSV file, it is important to cater for strings which already have quotes within them.

Fortunately, Delphi provides two very useful (but little known) functions to handle these.

AnsiQuotedStr will add quotes to the start and end of a string and more significantly double up any quotes within the string.

AnsiExtractQuotedStr will remove the quotes from the start and end and reduces pairs of quotes to a single quote.

In both functions you specify the kind of Quote to use.

Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top