Board index » delphi » Changing Tab-delimited file to Comma-delimited

Changing Tab-delimited file to Comma-delimited

Hi again.

Thanks everyone for the help. I'm gradually getting on top of Object Pascal,
but be patient please.

I am using Topaz components to access dBase-format files. I need to import
from a couple of other proprietory packages which export in Tab-delimited or
Comma-delimited ASCII formats. The Topaz function AppendFromDelimited for
some reason seems not to handle Tab-delimited formats (the corresponding
Clipper / dBase function can specify either).

What I need is a bit of Delphi code which will run through a Tab-delimited
ASCII file and replace the tabs with commas.

I know I could sit down for a couple of days and learn enough more to figure
it out myself but the clock is ticking on this project and I don't really
have a couple of days. Any help would be greatly appreciated.

Sincerely

Peter Lawrance
POSability Software
Email: p...@seol.net.au
PO Box 3353 Mount Gambier SA 5290
Australia

Bite off more than you can chew - then chew like hell!

 

Re:Changing Tab-delimited file to Comma-delimited


procedure ConvertTabDelimitedToCSV (aFileName : string);

var    srcList,
        dstList,
        cnvList        : tStringList;
        i                : integer;

begin
srcList := tStringList.Create;
dstList := tStringList.Create;
cnvList := tStringList.Create;
try
    srcList.LoadFromFile (aFileName);
    for i := 0 to (srcList.Count - 1) do
        begin
        cnvList.CommaText := srcList [i];
        dstList.Add (cnvList.CommaText);
        end;
    dstList.SaveToFile (ChangeFileExt (aFileName, '.CSV');
finally
    srcList.Free;
    dstList.Free;
    cnvList.Free;
    end;

Quote
"Peter Lawrance" <p...@seol.net.au> wrote in message

news:pg6L4.3$mP5.512@vic.nntp.telstra.net...
Quote
> Hi again.

> Thanks everyone for the help. I'm gradually getting on top of Object
Pascal,
> but be patient please.

> I am using Topaz components to access dBase-format files. I need to import
> from a couple of other proprietory packages which export in Tab-delimited
or
> Comma-delimited ASCII formats. The Topaz function AppendFromDelimited for
> some reason seems not to handle Tab-delimited formats (the corresponding
> Clipper / dBase function can specify either).

> What I need is a bit of Delphi code which will run through a Tab-delimited
> ASCII file and replace the tabs with commas.

> I know I could sit down for a couple of days and learn enough more to
figure
> it out myself but the clock is ticking on this project and I don't really
> have a couple of days. Any help would be greatly appreciated.

> Sincerely

> Peter Lawrance
> POSability Software
> Email: p...@seol.net.au
> PO Box 3353 Mount Gambier SA 5290
> Australia

> Bite off more than you can chew - then chew like hell!

Re:Changing Tab-delimited file to Comma-delimited


Thanks again Bruce. Just in case nobody answered, I cobbled together a
routine that loaded the file into a Memo, then used some code from a Search
& Replace to do it, but your code is more elegant.

Once I get this project on the road and make a *truckload* of money <g> I'll
buy a really good book on the subject. Anyone recommend one?

Peter Lawrance

-------------------------------------------------

Quote
Bruce Roberts wrote in message ...
>procedure ConvertTabDelimitedToCSV (aFileName : string);
>etc...

Re:Changing Tab-delimited file to Comma-delimited


"Peter Lawrance" <p...@seol.net.au> skrev i en meddelelse
news:pg6L4.3$mP5.512@vic.nntp.telstra.net...

Quote
> What I need is a bit of Delphi code which will run through a Tab-delimited
> ASCII file and replace the tabs with commas.

Look in the comp.lang.pascal.delphi.misc group for a thread called "Help
Reading CSV files".
That will tell you what you need.

Finn Tolderlund

Re:Changing Tab-delimited file to Comma-delimited


Oh, oh...

I whacked this code into my app with great anticipation. Worked fine, with
one major drawback. It seems that CommaText replaces not just Tabs but
spaces, periods, etc. with commas. This is a problem because many of the
fields in the source file contain spaces, etc. as legitimate parts of the
text, eg. 'Ext. door w/o hinges'.

I have tried unsuccessfully to make the code ignore everything but Tabs. I
have tried to adapt it using a version of my Memo Search & Replace routine,
but to no avail.

Surely Delphi / Pascal must have some simple function that either populates
a .dbf file with data from a Tab-Delimited text file or converts
Tab-Delimited text data to Comma-Delimited. Unfortunately I don't yet know
enough to write a file-level routine that simply seeks Chr(9) and replaces
it with Chr(34)+Chr(44)+Chr(34) or something like that.

Any further help will be greatly appreciated.

Sincerely

Peter Lawrance
POSability Software
Email: p...@seol.net.au
PO Box 3353 Mount Gambier SA 5290
Australia

Bite off more than you can chew - then chew like hell!

---------------------------------------------------------------

Quote
Bruce Roberts wrote in message ...
>procedure ConvertTabDelimitedToCSV (aFileName : string);

>var    srcList,
>        dstList,
>        cnvList        : tStringList;
>        i                : integer;

>begin
>srcList := tStringList.Create;
>dstList := tStringList.Create;
>cnvList := tStringList.Create;
>try
>    srcList.LoadFromFile (aFileName);
>    for i := 0 to (srcList.Count - 1) do
>        begin
>        cnvList.CommaText := srcList [i];
>        dstList.Add (cnvList.CommaText);
>        end;
>    dstList.SaveToFile (ChangeFileExt (aFileName, '.CSV');
>finally
>    srcList.Free;
>    dstList.Free;
>    cnvList.Free;
>    end;

Re:Changing Tab-delimited file to Comma-delimited


OK, just read an email with another bit of code, but my post above
complaining about Bruce's CommaText code had already been posted. I'll try
the new routine and see how it goes.

BTW, Thanks also to Finn Tolderlund for the pointer to an answer in another
newsgroup.

Peter Lawrance

Re:Changing Tab-delimited file to Comma-delimited


Quote
"Peter Lawrance" <p...@seol.net.au> wrote in message

news:pcaN4.14$7r6.1584@vic.nntp.telstra.net...

Quote
> Oh, oh...

> I whacked this code into my app with great anticipation. Worked fine, with
> one major drawback. It seems that CommaText replaces not just Tabs but
> spaces, periods, etc. with commas. This is a problem because many of the
> fields in the source file contain spaces, etc. as legitimate parts of the
> text, eg. 'Ext. door w/o hinges'.

> I have tried unsuccessfully to make the code ignore everything but Tabs. I
> have tried to adapt it using a version of my Memo Search & Replace
routine,
> but to no avail.

> Surely Delphi / Pascal must have some simple function that either
populates
> a .dbf file with data from a Tab-Delimited text file or converts
> Tab-Delimited text data to Comma-Delimited. Unfortunately I don't yet know
> enough to write a file-level routine that simply seeks Chr(9) and replaces
> it with Chr(34)+Chr(44)+Chr(34) or something like that.

procedure ReplaceTabChars (srcFileName : string);

var    srcFile,
        dstFile    : TextFile;
        ch          : char;

begin
if FileExists (srcFileName)
then begin
    AssignFile (srcFile, srcFileName);
    Reset (srcFile);
    try
        AssignFile (dstFile, ChangeFileExt (srcFileName, '.TAB'));
        Rewrite (dstFile);
        try
            while not EOF (srcFile) do
                begin
                Read (srcFile, ch);
                if ch = Char (9)
                then Write (dstFile, Chr(34)+Chr(44)+Chr(34))
                else Write (dstFile, ch);
                end;
        finally
            CloseFile (dstFile);
            end;
    finally
        CloseFile (srcFile);
        end;
end;

- Show quoted text -

Quote

> Any further help will be greatly appreciated.

> Sincerely

> Peter Lawrance
> POSability Software
> Email: p...@seol.net.au
> PO Box 3353 Mount Gambier SA 5290
> Australia

> Bite off more than you can chew - then chew like hell!

> ---------------------------------------------------------------

> Bruce Roberts wrote in message ...
> >procedure ConvertTabDelimitedToCSV (aFileName : string);

> >var    srcList,
> >        dstList,
> >        cnvList        : tStringList;
> >        i                : integer;

> >begin
> >srcList := tStringList.Create;
> >dstList := tStringList.Create;
> >cnvList := tStringList.Create;
> >try
> >    srcList.LoadFromFile (aFileName);
> >    for i := 0 to (srcList.Count - 1) do
> >        begin
> >        cnvList.CommaText := srcList [i];
> >        dstList.Add (cnvList.CommaText);
> >        end;
> >    dstList.SaveToFile (ChangeFileExt (aFileName, '.CSV');
> >finally
> >    srcList.Free;
> >    dstList.Free;
> >    cnvList.Free;
> >    end;

Re:Changing Tab-delimited file to Comma-delimited


If your table isn't to large (whatever that may mean), you could import it into
an MS Access table as a tab delimited file and then export it as a comma
delimited file.

Quote
Peter Lawrance wrote:
> Oh, oh...

> I whacked this code into my app with great anticipation. Worked fine, with
> one major drawback. It seems that CommaText replaces not just Tabs but
> spaces, periods, etc. with commas. This is a problem because many of the
> fields in the source file contain spaces, etc. as legitimate parts of the
> text, eg. 'Ext. door w/o hinges'.

> I have tried unsuccessfully to make the code ignore everything but Tabs. I
> have tried to adapt it using a version of my Memo Search & Replace routine,
> but to no avail.

> Surely Delphi / Pascal must have some simple function that either populates
> a .dbf file with data from a Tab-Delimited text file or converts
> Tab-Delimited text data to Comma-Delimited. Unfortunately I don't yet know
> enough to write a file-level routine that simply seeks Chr(9) and replaces
> it with Chr(34)+Chr(44)+Chr(34) or something like that.

> Any further help will be greatly appreciated.

> Sincerely

> Peter Lawrance
> POSability Software
> Email: p...@seol.net.au
> PO Box 3353 Mount Gambier SA 5290
> Australia

> Bite off more than you can chew - then chew like hell!

> ---------------------------------------------------------------

> Bruce Roberts wrote in message ...
> >procedure ConvertTabDelimitedToCSV (aFileName : string);

> >var    srcList,
> >        dstList,
> >        cnvList        : tStringList;
> >        i                : integer;

> >begin
> >srcList := tStringList.Create;
> >dstList := tStringList.Create;
> >cnvList := tStringList.Create;
> >try
> >    srcList.LoadFromFile (aFileName);
> >    for i := 0 to (srcList.Count - 1) do
> >        begin
> >        cnvList.CommaText := srcList [i];
> >        dstList.Add (cnvList.CommaText);
> >        end;
> >    dstList.SaveToFile (ChangeFileExt (aFileName, '.CSV');
> >finally
> >    srcList.Free;
> >    dstList.Free;
> >    cnvList.Free;
> >    end;

--
Patrick Goupell

www.nite.org
www.noneusa.org

Re:Changing Tab-delimited file to Comma-delimited


Thanks for the replies.

Bruce's new code does the trick, as does one Mike Manning sent me privately.
The reason I don't want to make the user go through Excel or similar is that
many of the users of this app may not be up to that - it has to be *very*
simple, just take their QuickBooks or other export file and suck it into
this app.

Cheers

Peter Lawrance.

Re:Changing Tab-delimited file to Comma-delimited


On Wed, 26 Apr 2000 07:49:59 +0930, "Peter Lawrance" <p...@seol.net.au>
wrote:
Quote
>Bruce's new code does the trick, as does one Mike Manning sent me privately.

Please post his answer to this question so that we may all see it and use
it.

-- THANKS!
Marc / Tech Support Sunbelt Software Inc.
** EZ Auto Leasing...consumer car & truck leasing software **
To e-mail, remove "nogoodpart" from t...@nogoodpartsunbeltinc.com
Web site: remove "nogoodpart" from www.nogoodpartsunbeltinc.com

Re:Changing Tab-delimited file to Comma-delimited


Mike Manning's code:

---------------- start code ----------------------------

  procedure substitute_ch;
  var
    F1, F2: textfile;
    ch2 : char;
    textfile1, textfile2 : string;

  begin
    textfile1 := // (....source filename)
    textfile2 := // (....new filename)

    { open the 2 files etc ....}
    assignfile(F1, txtfile1);   // sourcefile
    reset(F1);
    assignfile(F2, txtfile2);   // new file
    rewrite(F2);

    repeat
      read(F1,ch2);
      if ch2 = #9 then ch2 := #44;
      write(F2, ch2);
    until eof(F1);

    close(F1);
    close(F2);

  end;

--------------------- end code ---------------------------

Bruce Roberts' revised code (avoiding use of CommaFile):

-------------------- start code --------------------------

procedure ReplaceTabChars (srcFileName : string);

var    srcFile,
        dstFile    : TextFile;
        ch          : char;

begin
if FileExists (srcFileName)
then begin
    AssignFile (srcFile, srcFileName);
    Reset (srcFile);
    try
        AssignFile (dstFile, ChangeFileExt (srcFileName, '.TAB'));
        Rewrite (dstFile);
        try
            while not EOF (srcFile) do
                begin
                Read (srcFile, ch);
                if ch = Char (9)
                then Write (dstFile, Chr(34)+Chr(44)+Chr(34))
                else Write (dstFile, ch);
                end;
        finally
            CloseFile (dstFile);
            end;
    finally
        CloseFile (srcFile);
        end;
end;

-------------------- end code ---------------------------

Quote
t...@nogoodpartsunbeltinc.com wrote in message

<390c85db.80207...@news1.news.adelphia.net>...
Quote
>On Wed, 26 Apr 2000 07:49:59 +0930, "Peter Lawrance" <p...@seol.net.au>
>wrote:
>>Bruce's new code does the trick, as does one Mike Manning sent me
privately.
>Please post his answer to this question so that we may all see it and use
>it.

>-- THANKS!
>Marc / Tech Support Sunbelt Software Inc.
>** EZ Auto Leasing...consumer car & truck leasing software **
>To e-mail, remove "nogoodpart" from t...@nogoodpartsunbeltinc.com
>Web site: remove "nogoodpart" from www.nogoodpartsunbeltinc.com

Re:Changing Tab-delimited file to Comma-delimited


Quote

>Bruce Roberts' revised code (avoiding use of CommaFile):

Oops, I mean, 'CommaText'...

Other Threads