Solved

filenames Inside Listbox

Posted on 2001-06-04
9
303 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
Comment Utility
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
Comment Utility
"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
Comment Utility
by not specifying a directory but instead it automatically put all files inside the list. how can i do that?
0
 
LVL 2

Expert Comment

by:bugroger
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 6

Expert Comment

by:edey
Comment Utility
"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
Comment Utility
does putting c:\ on search will lists all files inside
the listbox?
0
 
LVL 6

Expert Comment

by:edey
Comment Utility
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
Comment Utility
ok that answers my question thankyou
0
 
LVL 6

Expert Comment

by:edey
Comment Utility
Glad to be of service :)


GL
Mike
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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 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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

11 Experts available now in Live!

Get 1:1 Help Now