Solved

filenames Inside Listbox

Posted on 2001-06-04
9
310 Views
Last Modified: 2010-04-06
how can i put ALL files (directory + filenames) inside a
listbox.
ex.
  listbox1.items...
   c:\command.com
   c:\windows\notepad.exe
   c:\windows\command\format.com
   ...
   ...
   ...

 all files in my hdd
0
Comment
Question by:ryan_sabarre
  • 5
  • 3
9 Comments
 
LVL 6

Accepted Solution

by:
edey earned 100 total points
ID: 6154067
try something like this:

procedure search(dir : string);
var
   sr : TSearchRec;
   result : integer;
begin
     result := findFirst(dir+'*.*',faAnyfile,sr);
     while result = 0 do
     begin
          if (sr.name <> '.')and(sr.name <> '..')then
          begin
               if (sr.attr and faDirectory > 0) then
                  search(dir+sr.name+'\');
               form1.listBox1.items.add(dir+sr.name);
          end;
          result := findNext(sr);
     end;
     findClose(sr);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
     search('c:\temp\');
end;


note, this code expects a listBox - listBox1.  Also, if you get a large number of files you'll run out of room in your TMemo - try a TRichEdit instead.

GL
Mike
0
 
LVL 6

Expert Comment

by:edey
ID: 6154083
"note, this code expects a listBox - listBox1.  Also, if you get a large number of files you'll run out
of room in your TMemo - try a TRichEdit instead."

Errr... rather you might run out of room with a standard listEdit or memo - try a TRichEdit instead.

GL
Mike
0
 

Author Comment

by:ryan_sabarre
ID: 6154126
by not specifying a directory but instead it automatically put all files inside the list. how can i do that?
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
LVL 2

Expert Comment

by:bugroger
ID: 6154160
This one only shows only the files an folders in the
current directory.If you dblClick on a folder you can
get in, like a browser.

Just put a ListBox - ListBox1 in your form an use
this code !!!

-----------------------------------------------------
UNIT XXXXXXXXX;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    procedure ListBox1DblClick(Sender: TObject);
    procedure ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

Const
 vDblClick   : Boolean = FALSE; //if DoubleClick then TRUE

VAR
 CurrentDir : String;          

Procedure Get_DIRS_FILES(verzeichnis : String; VAR ListBox : TListBox);
Var
 FindDATA     : WIN32_FIND_DATA;
 SHandle      : THandle;
 ADDStr       : String;
 z            : integer;

Begin
 ListBox.Clear;

 IF Verzeichnis[Length(verzeichnis)] <> '\' then
    Verzeichnis := Verzeichnis + '\';

 //ADD Directorys
 sHandle := FindFirstFile(PChar(verzeichnis + '*'), FindData);
 If sHandle <> INVALID_HANDLE_VALUE then
 Repeat
  IF (FindData.cFileName[0] <> '.')THEN
  BEGIN
   IF ((FindData.dwFileAttributes AND FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY) THEN
    ADDStr := '_[' + String(FindData.cFileName) + ']' ELSE // _ for "Sorted"
    ADDStr := FindData.cFileName;
    ListBox.Items.Add(ADDStr);
  END;

 Until Boolean(FindNextFile(sHandle, FindData)) = False;
 Windows.FindClose(SHandle);
 ListBox.Sorted := TRUE;

 //Delete "_" form Directory names
 With ListBox.Items do
 Begin
  For z := 0 to Count -1 do
   IF ((Strings[z][1]) = '_')AND
      ((Strings[z][2]) = '[') then
   Begin
    ADDStr := Strings[z];
    System.Delete(ADDStr, 1, 1);
    Delete(z);
    Insert(z, ADDStr);
   End;
 End;

 //IF Dir is sub dir then ...
 IF Verzeichnis[Length(verzeichnis)-1] <> ':' then
  ListBox.Items.Insert(0, '>> Parent Directory <<');
End;


procedure TForm1.ListBox1DblClick(Sender: TObject);
begin
 vDblClick := TRUE;
end;

procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
VAR
 Index, z : Integer;
 s, DN    : string;


begin
 IF vDblClick then
 Begin
  vDblClick := FALSE;

  With TListBox(Sender) do
  Begin
   Index := ItemAtPos(Point(X,Y), TRUE);
   s := Items.Strings[Index];

   IF (s[1] = '[')OR
      ((CurrentDir[Length(CurrentDIR) -1] <> ':')AND(Index = 0)) THEN
   Begin
    // it' s a directory

    // if [parent directory] then ...
    IF (CurrentDir[Length(CurrentDIR) -1] <> ':')AND
       (Index = 0) THEN
    Begin
     Delete(currentDir, Length(CurrentDir), 1);
     For z := Length(CurrentDir) downto 1 do
      IF CurrentDir[z] = '\' then Break;

     Delete(CurrentDir, z +1, Length(CurrentDir) -z );

    End else
    Begin
     //set new currentDir

     DN := copy(s, 2, Length(s) -2);
     CurrentDir := CurrentDir + DN + '\';
    End;

    // get dirs and files for the new dir
    Get_Dirs_Files(CurrentDir, TListBox(Sender));
   End else
   Begin
    // it' s a file
   End;
  End;
 End;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 CurrentDir := 'D:\';
 Get_DIRS_FILES(CurrentDir, ListBox1);
end;

end.
0
 
LVL 6

Expert Comment

by:edey
ID: 6154569
"by not specifying a directory but instead it automatically put all files inside the list. how can i
do that?"

I'm not quite sure what you mean, but try calling search('c:\') instead of search('c:\temp') - it's flexible.

GL
Mike
0
 

Author Comment

by:ryan_sabarre
ID: 6156206
does putting c:\ on search will lists all files inside
the listbox?
0
 
LVL 6

Expert Comment

by:edey
ID: 6156306
the search function will recursively search the specified folder, plus any and all folders below it, and any below those, and ... etc.  So calling seach('c:\') will list everything on the entire drive c.

GL
Mike
0
 

Author Comment

by:ryan_sabarre
ID: 6156535
ok that answers my question thankyou
0
 
LVL 6

Expert Comment

by:edey
ID: 6156937
Glad to be of service :)


GL
Mike
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

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…
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…

828 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