Solved

TXT files display on listbox

Posted on 2004-04-12
5
706 Views
Last Modified: 2010-05-18
Hi Experts,

Given an editbox where a valid directory path will be entered,
how will I do it in Delphi that for any file whose extension is .TXT will be listed inside a listbox provided.
Say if the displayed directory contains 8 .TXT, 2 .DOC and 5 .XLS files, when say given a button, upon pressing it will then display the 8 TXT files (filenames) inside the listbox.
Please show me a sample coding how to do this.

I'm using Delphi 6

Thanks,
jrmn
0
Comment
Question by:jrmn
5 Comments
 
LVL 8

Accepted Solution

by:
BdLm earned 30 total points
ID: 10811367
Why don't you wotk with the FileOpen dialog and use the filter options ? to do it manually:

var    Search :    TSearchRec;
       verz   :    string;
       such   :    String;
       mask   :    String;
       Found  :    Integer;
       FoundFiles  : Integer;
begin
     
     { Search-string ...}
     mask := Pathname+ '*.txt';
     such := ExtractFilename(mask);
     verz := ExtractFilePath(mask);

     FoundFiles :=0;
     found:=FindFirst (mask, faAnyFile, Search);
     while found = 0 do
     begin
           
            { add to text Box ...}
            ListBox.Add(verz+Search.Name);

           { processing next line ... }
           found:=FindNext(Search);
           inc(FoundFiles);

     end;
     FindClose(Search);
0
 
LVL 6

Expert Comment

by:CyberGhost
ID: 10811520
You can use the FindFirst and FinNext procedures to do this.
An example:

procedure TForm1.Button1Click(Sender: TObject);
var a:TSearchRec; // variable used when searching for files in a directory
                            // note: may be always closed by FindClose() statement to free up memory
begin
        listbox.items.clear; // clear existing filenames

        if FindFirst(directory.text+'\*.txt',faAnyFile,a)<>0 then listbox.Items.Add(directory.text+'\'+a.name);
{
  search for first file with the "txt" extention and add it to the list with a full path
  note: a.name = File Name without a path, path is the directory.text variable
  note2: don't forget to check if the '\' is present as the last character of the directory name
 
  code should looks like this if you wanna find this out:
--
  if Copy(directory.text,Length(directory.text),1)<>'\' then
  begin
    if FindFirst(directory.text+'\*.txt',faAnyFile,a)<>0 then listbox.Items.Add(directory.text+'\'+a.name);
  end
  else
  begin
    if FindFirst(directory.text+'*.txt',faAnyFile,a)<>0 then listbox.Items.Add(directory.text+a.name);
  end;
--
}
        while FindNext(a)=0 do listbox.Items.Add(directory.text+'\'+a.name);
{
  while there are still any files with the "txt" extension, add them to the list. You could use the previous condition to be sure if last character is '\' or not
}
        FindClose(a);
end;

--
Additional notes:

directory = TEdit component where is the directory name entered. Note that if the directory ends with a '\', you have to change that FindFirst and FindNext statement (remove the '\' from there).
--
listbox = that TListBox where the filenames are added
--
FindFirst in this statement also find the directories, not excepting those '.' and '..' so watch for this if you will do something with all files it finds. Better use the condition "if not (DirectoryExists(directory.text+'\'+a.name)) then {Do Something here}" for manipulating with files it finds

regards
CyberGhost
0
 
LVL 17

Expert Comment

by:mokule
ID: 10811521
You can also use TFileListBox (Win 3.1 Tab) in place of ListBox
Set
FileListBox1.Directory := YourPath;
FileListBox1.Mask := '*.TXT';
0
 

Expert Comment

by:rsun
ID: 11624167
How if we wanna retrieve more than one type of file??..
Let say, jpeg files and txt files need to be diplayed in the list box??
0
 
LVL 8

Expert Comment

by:BdLm
ID: 11635010
you may extent the mask eg.

FileListBox1.Mask := 'JPG Files (*.jpg)  |*.jpg| Txt Files (*.txt)  |*.txt  '


pls. confirm with delphi help, i did not test it  .-)
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

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…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

830 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