?
Solved

TXT files display on listbox

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

771 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