Solved

How to pass FileName as a string?

Posted on 2004-09-18
4
312 Views
Last Modified: 2010-04-05
O.K., I have been stuck on this for about 3 days straight.  I am trying to pass the name of a file as a string so I can open it in a procedure.  The problem I am having, is that TStringList is not compatible with String (obvious, I know).  Here's some of my code:

//Onclick procedure which lets the user navigate to and select a file.

procedure TForm1.Button1Click(Sender: TObject);

var
  OpenFile : TOpenDialog;
  FileName : TStringList;

begin

  OpenFile := TOpenDialog.Create(self);
  OpenFile.InitialDir := GetCurrentDir;
  OpenFile.Options := [ofFileMustExist];
  OpenFile.Filter := 'CSV files only |*.csv';
  OpenFile.FilterIndex := 2;

   if OpenFile.Execute
     then FileName := Tstringlist.Create
         else MessageDlg('Are you sure?',mtError, mbOKCancel, 0);

   Edit1.Text := OpenFile.FileName;

end;

//Here's the procedure which loads the CSV and stores the data in an array.


Procedure CSVimport(FileName: String);
Var
  MyFile, MyColumns: TStringList;
  Row, Col: integer;
begin
 
  MyFile := TStringList.Create;
  MyColumns := TStringList.Create;
  MyColumns.QuoteChar := ' ';
  try
    MyFile.LoadFromFile(FileName);  //This is where the code errors.  File name is not passed.
    If MyFile.Count > 0 then
    Begin
      MyColumns.DelimitedText := MyFile[Row];

      For Col := 0 to MyColumns.Count -1 Do
        MyColumns[Col] := MyColumns[Col] + ' varchar(50)';

      MyFile[Row] := 'CREATE TABLE TABLE1 (ID INT(2), ' + MyColumns.DelimitedText +  ', PRIMARY KEY(ID));';
    end;  

This is not all the code, the rest seems to compile no probs.  I just can't figure out how pass the file name to the procedure, so I can load it in the procedure and pass the contents of the CSV into the array.  I realize there are a few ways of doing this, however, my main goal is to pass the filename as a string, so I can potentially use a constructor Create(FileName: String); in another unit.  I can figure this part out, just need to get through my initial stumbling block...  

HELP!!

Ta.
0
Comment
Question by:south_paw
4 Comments
 
LVL 2

Expert Comment

by:gary_williams
Comment Utility

Why do you not call

CSVImport(OpenFile.FileName);

instead of stuffing the filename into an edit control?
0
 
LVL 4

Accepted Solution

by:
Evarest earned 500 total points
Comment Utility
I'm at a loss :-)

Your code as you give it gives no error with me. Its syntax is correct.

If you want to pass the FileName as string to a procedure, and that filename is stored in a TStringList, just pass an item out of that list to the procedure. I've altered your code so that every file opened by using TOpenDialog will be sent to the CSVImport procedure:

procedure TForm1.Button1Click(Sender: TObject);
var
  OpenFile : TOpenDialog;
  FileName : TStringList;
  Q: integer;
begin

  OpenFile := TOpenDialog.Create(self);
  OpenFile.InitialDir := GetCurrentDir;
  OpenFile.Options := [ofFileMustExist, ofAllowMultiSelect]; //<-- ofAllowMultiSelect to select more files in the TOpenDialog
  OpenFile.Filter := 'CSV files only |*.csv';
  OpenFile.FilterIndex := 2;

   if OpenFile.Execute
    then
     begin
      for Q :=0 to OpenFile.Files.Count -1 do // <-- Loop to send the FileNames to the procedure
       begin
        CSVImport(OpenFile.Files[Q]); // <-- Call to the procedure CSVImport
       end;
     end
     else MessageDlg('Are you sure?',mtError, mbOKCancel, 0);

   // Edit1.Text := OpenFile.FileName; // <-- has no use when multiselecting

end;

//Here's the procedure which loads the CSV and stores the data in an array.


Procedure CSVimport(FileName: String);
Var
  MyFile, MyColumns: TStringList;
  Row, Col: integer;
begin
 
  MyFile := TStringList.Create;
  MyColumns := TStringList.Create;
  MyColumns.QuoteChar := ' ';
  try
    MyFile.LoadFromFile(FileName);  //This is where the code errors.  File name is not passed.
    If MyFile.Count > 0 then
    Begin
      MyColumns.DelimitedText := MyFile[Row];

      For Col := 0 to MyColumns.Count -1 Do
        MyColumns[Col] := MyColumns[Col] + ' varchar(50)';

      MyFile[Row] := 'CREATE TABLE TABLE1 (ID INT(2), ' + MyColumns.DelimitedText +  ', PRIMARY KEY(ID));';
    end;
  except
  end;
end;

Let me know if this is what you mean!
Evarest
0
 
LVL 7

Expert Comment

by:LRHGuy
Comment Utility
Maybe I don't understand the problem, but why don't you make Filename a string, as in:

procedure TForm1.Button1Click(Sender: TObject);

var
  OpenFile : TOpenDialog;
  FileName : String;

begin

  OpenFile := TOpenDialog.Create(self);
  OpenFile.InitialDir := GetCurrentDir;
  OpenFile.Options := [ofFileMustExist];
  OpenFile.Filter := 'CSV files only |*.csv';
  OpenFile.FilterIndex := 2;

   if OpenFile.Execute
     then begin
        FileName := OpenFile.FileName;

       CVSImport(FileName);
    end
    else
      MessageDlg('Are you sure?',mtError, mbOKCancel, 0);

   Edit1.Text := OpenFile.FileName;

end;
0
 

Author Comment

by:south_paw
Comment Utility
Thanks, I get it now.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now