Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 325
  • Last Modified:

filenames Inside Listbox

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
ryan_sabarre
Asked:
ryan_sabarre
  • 5
  • 3
1 Solution
 
edeyCommented:
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
 
edeyCommented:
"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
 
ryan_sabarreAuthor Commented:
by not specifying a directory but instead it automatically put all files inside the list. how can i do that?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
bugrogerCommented:
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
 
edeyCommented:
"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
 
ryan_sabarreAuthor Commented:
does putting c:\ on search will lists all files inside
the listbox?
0
 
edeyCommented:
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
 
ryan_sabarreAuthor Commented:
ok that answers my question thankyou
0
 
edeyCommented:
Glad to be of service :)


GL
Mike
0

Featured Post

Technology Partners: 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!

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now