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

x
?
Solved

Getting path info.

Posted on 2000-03-01
10
Medium Priority
?
162 Views
Last Modified: 2010-04-04
How would you determine all directories on a hard drive and get a listing of them in your delphi app?
0
Comment
Question by:evansj
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 2

Expert Comment

by:PeterLarsen
ID: 2575643
In D5 there is a demo program called "Virtual Listview". I think this is what you'r looking for.
Delhpi 4 have a similar demo, but it is better in D5.

Regards
Peter
0
 
LVL 2

Expert Comment

by:aubs
ID: 2575656
try this,

//scan directory tree and return a list of all files and directories
procedure ScanDirectory(Dir: String; var FileList: TStringList);

          procedure SearchTree;
          var
            sRec    : TSearchRec;
            RetCode : Integer;
            Dir     : String;
          begin
            GetDir(0, Dir);
            if Dir[length(Dir)] <> '\' then Dir := Dir + '\'; //add trailing slash
            //Go for the subdirectories
            RetCode := FindFirst('*.*', faDirectory, sRec);
            try
              while RetCode = 0 do
              begin
                if (SRec.Attr AND faDirectory = faDirectory) and (SRec.Name[1] <> '.') then {directories}
                begin
                  FileList.Add(Dir + SRec.Name);
                  ChDir(SRec.Name);
                  SearchTree; //time for recursion
                  ChDir('..'); //back up a level
                end;
                RetCode := FindNext(SRec);
              end;
            finally
              FindClose(SRec);
            end;
          end;

begin
  //Clear existing Files...
  FileList.Clear;

  //Change to start directory
  Chdir(Dir);

  //Start file list scan
  SearchTree;
end;


you will need to create the stringlist yourself:

procedure TForm1.Button1Click(Sender: TObject);
var
      list1: TStringlist;
  i: integer;
begin
  list1:= TStringlist.create;
  ScanDirectory('c:\program files',list1);
  for i:= 0 to list1.count - 1 do
        listbox1.items.add(list1[i]);

  list1.Free;
end;
0
 
LVL 6

Expert Comment

by:edey
ID: 2576734
or cheat :)

procedure TForm1.FormCreate(Sender: TObject);
begin
     shellExecute(handle,'open','c:\windows\command\command.com dir c:\*.* /s/b > c:\testdir.txt','','',SW_show);
     memo1.lines.loadFromFile('c:\testdir.txt');
     deleteFile('c:\testdir.txt');
end;



GL
Mike
0
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!

 
LVL 1

Expert Comment

by:X_Kalibur
ID: 2585910
howdy,

yeh..i normally do something like what edey has :) it makes life so much easier if you just shellexecute the command into a file...

you can always get the drive letter and home directory (if not root), by using a TDirectoryList and TFileList
0
 

Author Comment

by:evansj
ID: 2587995
Adjusted points to 200
0
 

Author Comment

by:evansj
ID: 2587996
Not only get the directory, but the files in the directory.
0
 
LVL 2

Accepted Solution

by:
aubs earned 800 total points
ID: 2590682
You mean like this:

//scan directory tree and return a list of all files and directories
procedure ScanDirectory(Dir: String; var FileList: TStringList);

          procedure SearchTree;
          var
            sRec    : TSearchRec;
            RetCode : Integer;
            Dir     : String;
          begin
            GetDir(0, Dir);
            if Dir[length(Dir)] <> '\' then Dir := Dir + '\'; //add trailing slash
            RetCode := FindFirst('*.*', faAnyFile, sRec);
            try
              while RetCode = 0 do
              begin
                if not(SRec.Attr AND faDirectory = faDirectory) then {ignore directories}
                  FileList.Add(Dir + SRec.Name);

                RetCode := FindNext(SRec);
              end;
            finally
              FindClose(SRec);
            end;

            //Go for the subdirectories
            RetCode := FindFirst('*.*', faDirectory, sRec);
            try
              while RetCode = 0 do
              begin
                if (SRec.Attr AND faDirectory = faDirectory) and (SRec.Name[1] <> '.') then {directories}
                begin
                  ChDir(SRec.Name);
                  SearchTree; //time for recursion
                  ChDir('..'); //back up a level
                end;
                RetCode := FindNext(SRec);
              end;
            finally
              FindClose(SRec);
            end;
          end;

begin
  //Clear existing Files...
  FileList.Clear;

  //Change to start directory
  Chdir(Dir);

  //Start file list scan
  SearchTree;
end;
0
 
LVL 1

Expert Comment

by:X_Kalibur
ID: 2591054
evansi,


i suppose it all depends if you want it done quick and dirty (edey's way) or the proper way to do it (aubs' way)

:)
0
 

Author Comment

by:evansj
ID: 2597525
To Aub: I'm having trouble implementing your code snippet. When I call the ScanDirectory proc like:
myList.create;
ScanDirectory('e:\',mylist);
I get an access violation at the filelist.clear part of your code. I am using Delphi 3.0 enterprise. What am I doing wrong, or where should I place the procedure?
0
 

Expert Comment

by:JimboKern69
ID: 2597618
evansi,

It sounds like you have not, prior to calling ScanDirectory, created the TStringList object to be passed in to the procedure.

Before calling ScanDirectory, do this:

mylist := TStringList.Create();

then call:

ScanDirectory('e:\', mylist);

Otherwise, aubs' code looks solid to me.

Happy Coding!

Jimbo
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

972 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