Solved

filenames Inside Listbox

Posted on 2001-06-04
9
304 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

863 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

26 Experts available now in Live!

Get 1:1 Help Now