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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Process csv file

Status
Not open for further replies.

lespaul

Programmer
Feb 4, 2002
7,083
US
I'm trying to take this code that I found in Thread102-839480 and tweak it to process a file a little differently.

I have a file with data like:

[tt]
documentid CitNum Path
1245655 02B0050223551 \\server\001301D7.tif
[/tt]
The file is currently an excel spreadsheet, but I have saved it as a cvs.
I need to for each line in the file to run two queries and a file process. The two queries need the doucmentid information to delete the related table records and then the file name to process the deletion of the related image.

I'm stuck at the Assign process, I'm not sure how to take that section that's extracting each piece and not put it in a StringGrid, but put it in my query or process.

Any Suggestions?

Here's the original code:

Code:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


procedure TForm1.Button1Click(Sender: TObject);
var
  oFileStrings:TStringList;
  oRowStrings:TStringList;
  i:integer;
begin
  oFileStrings := TStringList.Create;
  oRowStrings := TStringList.Create;
  try
    StringGrid1.FixedCols := 1;
    StringGrid1.FixedRows := 1;
    StringGrid1.RowCount := 2;
    StringGrid1.ColCount := 2;
    oFileStrings.LoadFromFile('c:\test.csv');
    StringGrid1.RowCount := oFileStrings.Count;
    for i := 0 to oFileStrings.Count - 1 do
    begin
      oRowStrings.Clear;
      oRowStrings.CommaText := oFileStrings[i];
      oRowStrings.Insert(0,IntToStr(i));
      if oRowStrings.Count > StringGrid1.ColCount then
        StringGrid1.ColCount := oRowStrings.Count;
      StringGrid1.Rows[i].Assign(oRowStrings);
    end;
    StringGrid1.Cells[0,0] := '';
  finally
    oFileStrings.Free;
    oRowStrings.Free;
  end;
end;

end.

And here is what I have so far, the bolded section is what I can't get to work....of course it is, it's the meat of the solution!!!
Code:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure deleteRecords (DocumentID : string);
begin
  With Query1 do
  begin
    SQL.Clear;
    SQL.Add('DELETE FROM CMPCITIMGS WHERE DOCUMENTID = ' + QuotedSTr(DOcumentID));
    ExecSQL;

    SQL.clear;
    SQL.Add('DELETE FROM CMPIMGPATH WHERE DOCUMENTID = ' + QuotedSTr(DOcumentID));
    ExecSQL;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  TheList : TStringList;
  TheRecord : TStringList;
  i : integer;
begin
  TheList := TStringList.Create;
  TheRecord:= TSTringlist.Create;
  try
  TheList.LoadFromFile('C:\Documents and Settings\landrews\Desktop\badimages.csv');
  for i  := 0 to TheList.Count - 1 do
  begin
    [b]TheRecord.Clear;
    TheRecord.CommaText := TheList[i];
    TheRecord.Insert(0, IntTostr(i));

    TheRecord.Assign(TheList[i]);
    SHowmessage(TheRecord[0]);[/b]

    deleteRecords(DocIDValue);
    if FileExists(PathNameValue) then
      DeleteFile(PathNameValue);

  end;
  finally
    TheList.Free;
    TheRecord.Free;
  end;
end;

end.

Leslie

Have you met Hardy Heron?
 
change of plans, i'm just going to use

Code:
assignfile(f,'xxxxx.csv');
 reset(f);
 while (not eof(f)) do begin 
  readln(f,mystring);
  // process the lines

Leslie

Have you met Hardy Heron?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top