Solved

filenames Inside Listbox

Posted on 2001-06-04
9
318 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
[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
  • 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
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!

 
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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month4 days, 1 hour left to enroll

630 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