[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

List all txt files in dir !

I need a code to list all *.txt files in dir where is program and add it to combo box. And also code to check if some file exists.
0
65zgtre45rr
Asked:
65zgtre45rr
  • 2
  • 2
  • 2
1 Solution
 
TheRealLokiSenior DeveloperCommented:
procedure TForm1.Button1Click(Sender: TObject); //populate combobox
  var
    searchrec: tsearchrec;
    searchresult: integer;
  begin
    combobox1.items.beginupdate;
    try
      combobox1.items.clear;
      searchresult := FindFirst(ExtractFilePath(Paramstr(0)) +'*.txt', faAnyFile, SearchRec);
      while searchresult = 0 do
      begin
        combobox1.items.add(SearchRec.Name);
        searchresult := FindNext(SearchRec);
      end;
      FindClose(SearchRec);
    finally
      combobox1.items.EndUpdate;
    end;
  end;

procedure TForm1.Button2Click(Sender: TObject); //check if fileselected in combobox exists
  begin
    if ( (combobox1.ItemIndex > -1) and (FileExists(ExtractFilePath(Paramstr(0)) + combobox1.Items[combobox1.ItemIndex])) )
    then
      Caption := 'File exists'
    else
      Caption := 'File does not exist';
  end;
0
 
ZhaawZSoftware DeveloperCommented:
Actually there's no need to use loop for this. There's a message that allows to put filenames in combobox automatically:

SendMessage(ComboBox1.Handle, cb_dir, 0, integer(pchar('d:\*.txt')));
0
 
65zgtre45rrAuthor Commented:
I get this error with your code, i'm using delphi 2005

[Error] main.pas(109): E2089 Invalid typecast
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TheRealLokiSenior DeveloperCommented:
whose code? mine or zhaawz? either way, try the other code, see if that does wht you want
0
 
65zgtre45rrAuthor Commented:
TheRealLoki solution works
0
 
ZhaawZSoftware DeveloperCommented:
I'm not sure if this helps (I don't have D2005), but it could help:

var
  dir1 : pchar;
  dir2 : integer absolute dir1;
begin
dir1 := 'd:\*.txt';
SendMessage(ComboBox1.Handle, cb_dir, 0, dir2);
end;
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now