Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to pass FileName as a string?

Posted on 2004-09-18
4
Medium Priority
?
343 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

916 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