Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

TXT files display on listbox

Posted on 2004-04-12
5
Medium Priority
?
716 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 120 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

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!

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

604 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