Solved

Getting path info.

Posted on 2000-03-01
10
152 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

752 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