Solved

TXT files display on listbox

Posted on 2004-04-12
5
700 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now