Solved

How to pass FileName as a string?

Posted on 2004-09-18
4
315 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
ID: 12093486

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
ID: 12093490
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
ID: 12093493
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
ID: 12093586
Thanks, I get it now.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

896 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

15 Experts available now in Live!

Get 1:1 Help Now