N
N
nalimite2016-10-22 05:55:21
Delphi
nalimite, 2016-10-22 05:55:21

Delphi how to find folder on all drives in system?

Hello.
There was such a question. There is a folder called, say, "ASDFG".
How to do it programmatically in Delphi so that when a button is pressed, the program quickly searches through all disks (namely, the root of the disk), including removable media (flash drives), and if it finds this folder, it gives the path to it, let's edit.
The folder will lie at the root of the disk, but at the same time, the drive letter will always change, it will be a flash drive.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2016-10-22
@nalimite

procedure GetDriveLetters(AList: TStrings);
var
  vDrivesSize : Cardinal;
  vDrives	: array[0..128] of Char;
  vDrive : PChar;
begin
  AList.BeginUpdate;
  try
    AList.Clear;
    vDrivesSize := GetLogicalDriveStrings(SizeOf(vDrives), vDrives);
    if vDrivesSize = 0 then Exit;

    vDrive := vDrives;
    while vDrive^ <> #0 do
    begin
      AList.Add(StrPas(vDrive));
      Inc(vDrive, SizeOf(vDrive));
    end;
  finally
    AList.EndUpdate;
  end;
end;

function FindFolderOnDrives(sFolderName : String):string;
var
  i : Integer;
  lDrivers : TStringList;
  sFolderPath : String;
begin
  Result := '';
  lDrivers := TStringList.Create;
  try
    GetDriveLetters(lDrivers);
    for i := 0 to lDrivers.Count - 1 do
    begin
      sFolderPath := lDrivers[i] + sFolderName;
      if DirectoryExists(sFolderPath) then
      begin
        Result := sFolderPath;
        Exit;
      end;
    end;
  finally
    lDrivers.Free;
  end;
end;

Usage example
procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit1.Text := FindFolderOnDrives('ASDFG');
end;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question