Solved

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

Posted on 2003-10-25
21
466 Views
Last Modified: 2010-04-05
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.

0
Comment
Question by:so3
  • 10
  • 5
  • 2
  • +3
21 Comments
 

Expert Comment

by:emu10k1
ID: 9621831
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
 

Expert Comment

by:emu10k1
ID: 9621836
If you think that my comment answer your question put the points to alsantos. I'm alsantos in friend's house.

alsantos
0
 
LVL 2

Expert Comment

by:alsantos
ID: 9621838
I'm here.

alsantos
0
 
LVL 1

Expert Comment

by:tommyliu
ID: 9622014
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
 
LVL 6

Author Comment

by:so3
ID: 9623780
I need 48h to test the code.
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 9627631
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
 
LVL 6

Author Comment

by:so3
ID: 9630101
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
 
LVL 6

Author Comment

by:so3
ID: 9630102
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
 
LVL 1

Expert Comment

by:tommyliu
ID: 9631408
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
 
LVL 3

Expert Comment

by:AFZDeveloper
ID: 9635978
listening...
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

Author Comment

by:so3
ID: 9637339
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
 
LVL 2

Expert Comment

by:alsantos
ID: 9637402
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
 
LVL 6

Author Comment

by:so3
ID: 9637551
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
 
LVL 6

Author Comment

by:so3
ID: 9637587
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
 
LVL 2

Expert Comment

by:alsantos
ID: 9637794
I'm on college now, 2 hours later I'll try at home.

alsantos
0
 
LVL 6

Author Comment

by:so3
ID: 9637834
ok
0
 
LVL 2

Expert Comment

by:alsantos
ID: 9639322
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
 
LVL 6

Author Comment

by:so3
ID: 9641001
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
 
LVL 2

Accepted Solution

by:
alsantos earned 250 total points
ID: 9645366
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
 
LVL 6

Author Comment

by:so3
ID: 9645812
my email
idaniel_bv@yahoo.com
0
 
LVL 6

Author Comment

by:so3
ID: 9704708
ok tnx for the sample
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

13 Experts available now in Live!

Get 1:1 Help Now