• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 154
  • Last Modified:

Detecting partitions on a drive

I have a program that searches for a certain type of file.  I need to have the program detect any partitions on the drive in addition to the primary partition (c) such d, e, etc. so that I can pass those drive letters to my search routine.
0
Orca
Asked:
Orca
1 Solution
 
intheCommented:
do something like this:
it will search all the drives for the file of type.dll for instance:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ComboBox1: TComboBox;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure GatherFiles(Const Lines: TStrings; Const Dir,Match: String);
var
  Rec: TSearchRec;
  Found: Integer;
Begin
  // Add files
  Found:= FindFirst(Dir+'\'+Match, faAnyFile, Rec);
  While Found = 0 do
  begin
    If (Rec.Attr AND faDirectory) = 0 Then Lines.Add(Dir+'\'+Rec.Name);
    Found := FindNext(Rec);
  End;
  FindClose(Rec);
  // Find directories (Without match)
  Found:= FindFirst(Dir+'\*.*', faAnyFile, Rec);
  While Found = 0 do
  begin
    If ((Rec.Attr AND faDirectory)>0) AND
       (Rec.Name<>'.') AND
       (Rec.Name<>'..') Then GatherFiles(Lines,Dir+'\'+Rec.Name,Match);
    Found := FindNext(Rec);
  End;
  FindClose(Rec);
End;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ComboBox1.items.Clear;
  GatherFiles(ComboBox1.Items,'','*.dll');
  If ComboBox1.Items.Count>0 Then
    ComboBox1.Text:= ComboBox1.Items[0];
  ShowMessage('Found '+IntToStr(ComboBox1.Items.Count)+' matching files.');
end;

end.
0
 
simonetCommented:
inthe, I don't think that's what Orca wants. I think he wants to know what are the letters for the local drives.

Orca, if inthe routine's not what you want, you can reject it.

Do you really mean partitions?

Let's suppose I have 2 physical HDs plus a few more devices:

HD1 is drives C, E, F and G
HD2 is drive D
CDROM is drive H
ZIP is drive I

Do you want to get the letters for all the local drives or only for the partitions on HD1 (C, E, F, G)?

If what you want is the letter of all available local drives, then there are a few API calls that will work for you:

GetLogicalDriveStrings  and GetDriveType.

Here's an example of how to use them:

var
  pDrives : pchar;
  NumDrives : cardinal;
  i : integer;
  root : string;
begin
      Result := '';
      GetMem(pDrives, 1024);
  try
     NumDrives := 1023;
     NumDrives := GetLogicalDriveStrings(NumDrives, pDrives);
     NumDrives := NumDrives  div 4;
     for i := 0 to NumDrives do
     begin
        Root := string(pchar(pDrives+i*4));
        if GetDriveType(pchar(Root))=DRIVE_FIXED  then
        begin
           // Your search routine goes here!!!
           // The drive pointed by "root" is a local fixed drive.
        end;
     end;
  finally
     FreeMem(pDrives);
  end;
end;

Yours,

Alex
0
 
intheCommented:
yeh but using the search routine i gave you wouldnt need to pass any drive letters as it searches all drives anyway..
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.

 
bryan7Commented:
hmm, how do you avoid Win NT to show up a message because there's no disk in drive A ?
0
 
intheCommented:
try somethin like so:

var
  NoBox: Word;
begin
 NoBox := SetErrorMode(SEM_FailCriticalErrors);
  try
  {search code}
  except
  {your error code}
  end;
  SetErrorMode(NoBox);
end;

0
 
OrcaAuthor Commented:
Thanks for all of the great information.

Between inthe's suggestions and Alex, I was able to do exactly what I wanted.  You guys were a huge help.

Thanks again.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Tackle projects and never again get stuck behind a technical roadblock.
Join Now