evansj
asked on
Getting path info.
How would you determine all directories on a hard drive and get a listing of them in your delphi app?
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;
//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
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;
or cheat :)
procedure TForm1.FormCreate(Sender: TObject);
begin
shellExecute(handle,'open' ,'c:\windo ws\command \command.c om dir c:\*.* /s/b > c:\testdir.txt','','',SW_s how);
memo1.lines.loadFromFile(' c:\testdir .txt');
deleteFile('c:\testdir.txt ');
end;
GL
Mike
procedure TForm1.FormCreate(Sender: TObject);
begin
shellExecute(handle,'open'
memo1.lines.loadFromFile('
deleteFile('c:\testdir.txt
end;
GL
Mike
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
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
ASKER
Adjusted points to 200
ASKER
Not only get the directory, but the files in the directory.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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)
:)
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)
:)
ASKER
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?
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?
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
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
Delhpi 4 have a similar demo, but it is better in D5.
Regards
Peter