Solved

Getting path info.

Posted on 2000-03-01
10
145 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Expert Comment

by:X_Kalibur
Comment Utility
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
Comment Utility
Adjusted points to 200
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

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

Accepted Solution

by:
aubs earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
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.

728 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

12 Experts available now in Live!

Get 1:1 Help Now