How to scan disk for files & folders and save them to Strinlist

I'm trying to read all the folders & files from a disk or cd (with all the info name,type,size and date).

I want to save temporarily this information into a string list or something similar, and list them like this: folders in a treeview and the files in a listview. After that i want to save all the informations to a local file. I want to do something like a catalog( To save to a file a data Cd)

I successed to read the files & folders with FindFirst & FindNext functions, but only from a directory (without the subfolders & the other files). So if anyone can help me to read all the files & folders from a disk please help me.

If it's possible i want through FindFirst & FindNext functions.

LVL 6
so3Asked:
Who is Participating?
 
alsantosConnect With a Mentor Commented:
Hi so3. I've finished your sample... tell me your email so, I'll send it to you...  and If you like it, increase the points... :)

alsantos
0
 
emu10k1Commented:
You can use this code to search and show the result at ListBox. It will result all the files with date and time creation and size.
You need 1 button and 1 listbox

procedure FindFile(PathFileName : string; IncludeSubDirs : Boolean; ResultList : TStringList);
var
  srec : TSearchRec;
  FHandle : integer;
  Path, FileName : String;
begin
  Path := ExtractFilePath(PathFileName);
  FileName := ExtractFileName(PathFileName);
  // CurrentDirectory
  if findfirst(PathFileName,faanyfile,srec) = 0 then
  begin
    repeat
      If Not(srec.attr and fadirectory > 0) then
      begin
        FHandle := FileOpen(Path + Srec.Name, 0);
        ResultList.Add(Path + Srec.Name + ' ' + DateTimeToStr(FileDateToDateTime(FileGetDate(FHandle))) + ' ' + IntToStr(srec.Size) + 'bytes');
      end;
    Until findnext(srec) <> 0;
  end;
  FindClose(SRec);
  { SubDirectories }
  If IncludeSubDirs then
  begin
    if FindFirst(Path+'*.*',FaAnyFile,Srec) = 0 then
    begin
      repeat
        // StartRecursion
        if not(Srec.Name[1] = '.') and (srec.attr and fadirectory > 0)then
          FindFile(Path + Srec.Name + '\' + FileName,IncludeSubDirs,ResultList)
      Until findnext(srec) <> 0;
    end;
    FindClose(SRec);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var SL : TStringList;
begin
  SL := TStringList.Create;
  Try
    FindFile('F:\*.*',True,SL);
    listbox1.Items.Assign(sl);  //just to show the result
  finally
    sl.Free;
  end;
end;

alsantos
0
 
emu10k1Commented:
If you think that my comment answer your question put the points to alsantos. I'm alsantos in friend's house.

alsantos
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
alsantosCommented:
I'm here.

alsantos
0
 
tommyliuCommented:
The progarm is show all file and directory :

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button2: TButton;
    memo1: TMemo;
    TreeView1: TTreeView;
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure showallDirectory(pdirectory : string; pParentNode: TTreeNode; pLevel: integer);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure Tform1.showallDirectory(pdirectory : string; pParentNode: TTreeNode; pLevel : integer);
var
  xSearchResult, xFileAttr    : integer;
  CurrentDirInfo              : TSearchRec;
begin
  xFileAttr := faAnyFile;
  xSearchResult := FindFirst(pdirectory+'\*.*',xFileAttr,CurrentDirInfo);
  While xSearchResult = 0 Do
  Begin
    If (CurrentDirInfo.Name <> '.') And (CurrentDirInfo.Name <> '..') Then
    begin
      If (CurrentDirInfo.Attr And faDirectory) <> 0Then
      Begin
      {Is Directory}
        memo1.lines.add('Directory : '+pdirectory+CurrentDirInfo.name);
        If pLevel = 1 Then
        Begin
          showallDirectory(pdirectory+CurrentDirInfo.name,nil,pLevel+1);
        end
        Else
        Begin
          showallDirectory(pdirectory+'\'+CurrentDirInfo.name,nil,pLevel+1);
        end
      End
      Else
      Begin
      {Is Other}
        memo1.lines.add('File : '+pdirectory+CurrentDirInfo.name);
      End;
      xSearchResult := FindNext(CurrentDirInfo);
    End
    else
    Begin
      xSearchResult := FindNext(CurrentDirInfo);
    end;
  End;
  FindClose(CurrentDirInfo);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  memo1.Text := '' ;
  showallDirectory('d:\temp2\',nil,1);
end;

end.
0
 
so3Author Commented:
I need 48h to test the code.
0
 
Wim ten BrinkSelf-employed developerCommented:
I'm getting a bit sick and tired of people asking this question again and again while it has been answered so many times already in the past... :-(
0
 
so3Author Commented:
this code didn't solved my problem entirely. The findfile procedure works very well but i want to save the result in something temporary(a file or list). I want to read the folders in a treeview and in a listview only the files&folders from parent path.

Here is what i'm trying to do:

1. scan the disk c: and save all the info into something temporarily
2. read the infos in a treeview and a listview (to read them just as in windows explorer) from the save list above
3. repeat the process for disk d, e,f
4. save to local file all the info for later to open it and edit, add or delete disks.

0
 
so3Author Commented:
this code didn't solved my problem entirely. The findfile procedure works very well but i want to save the result in something temporary(a file or list). I want to read the folders in a treeview and in a listview only the files&folders from parent path.

Here is what i'm trying to do:

1. scan the disk c: and save all the info into something temporarily
2. read the infos in a treeview and a listview (to read them just as in windows explorer) from the save list above
3. repeat the process for disk d, e,f
4. save to local file all the info for later to open it and edit, add or delete disks.

0
 
tommyliuCommented:
Dear so3,

   My comment as above it can show all files in directory and it will store in memo object,

is it you mean the result is very near, only add the tree view control in the answer in ok,

and complete you job ???

Tommy
0
 
AFZDeveloperCommented:
listening...
0
 
so3Author Commented:
I've tried the code from you Tommy and i've inserted a treeView control in the procedure but the directory was not listed in the treeview. I've tried to insert some new code lines but the result wasn't satisfied. The folders was not showed correctly in the treeview.

0
 
alsantosCommented:
so3... my code shows the files name, directory name, type, size and date.... and not only the file name and directory.... so... If you to accept one answer, you have to accept the more complete answer. I know that we don't answer all of your questions, but my code (the code i've writted with emu10k1 account) is more complete than Tommy's code. Tommy, nothing about yourself. But we have to be fair.

alsantos
0
 
so3Author Commented:
can you send me the complete code of what i'm looking for.
for this:
read from disk -save to file (not text file) &treeview- open the file - list in treeview.
Also when i select a subfolder from the treeview folder to list them from the saved file into listview.
I want to make the operations like the program  Where is it?. to explore the contents from this saved file.

I know that is more work to do but i'll increase the points.
0
 
so3Author Commented:
yes alsantos you're right and you'll receive the points when i close the question if there isn't another answer. Can you help me to solve what i want to do.
0
 
alsantosCommented:
I'm on college now, 2 hours later I'll try at home.

alsantos
0
 
so3Author Commented:
ok
0
 
alsantosCommented:
I think you can increase the points :) It's a lot of work. When I finish, I'll put the code here if anyone put before.

alsantos
0
 
so3Author Commented:
alsantos i've increased the points.
I will tell you once more what i want to do, to be sure you understood correctly :)
I want to store to a non-txt file multiple disks informations (all files&folders with name,size,type, date,and an empty filed for description) like in windows explorer. After adding the disk files i want to explore them (when i select from treeview a path, in the list view to show the files from the selected path).
After saving the infos to a file, i want to aopen this file for reading, adding and deleting the disk. Also i want to show the icons for the files in the treeview and listview (i've made a function to read the iconimage but for disk i don't know).

I know that is a lot of work but i'm sure that you can do it, so please help me. After sending me the code i'll increase the points again :)

0
 
so3Author Commented:
my email
idaniel_bv@yahoo.com
0
 
so3Author Commented:
ok tnx for the sample
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.