Solved

Getting path info.

Posted on 2000-03-01
10
149 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
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.

 
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 200 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

830 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