Solved

TXT files display on listbox

Posted on 2004-04-12
5
707 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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