Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Getting path info.

Posted on 2000-03-01
10
Medium Priority
?
160 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Independent Software Vendors: 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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

721 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