-1

I am using Delphi2006 and I want to find the location of a particular program using Delphi code.

ulrichb
  • 19,610
  • 8
  • 73
  • 87
Amit Kumar Jain
  • 487
  • 2
  • 11
  • 25
  • location of what program? You want your program to find its own absolute path or look for a program on the system? – John T May 17 '09 at 08:24

1 Answers1

1

Here's a Delphi program that can find all files called aFileName, and puts the results into the aDestFiles stringlist.

 function findFilesCalled(aFileName : String; aDestFiles : TStringList) : boolean;
 var
   subDirs : TStringList;
   dir : Char;
   sRec : TSearchRec;
   toSearch : string;
 begin
   subdirs := TStringList.Create;
   for dir := 'A' to 'Z' do
     if DirectoryExists(dir + ':\') then
       subdirs.add(dir + ':');
   try
     while (subdirs.count > 0) do begin
       toSearch := subdirs[subdirs.count - 1];
       subdirs.Delete(subdirs.Count - 1);
       if FindFirst(toSearch + '\*.*', faDirectory, sRec) = 0 then begin
         repeat
           if (sRec.Attr and faDirectory) <> faDirectory then
             Continue;
           if (sRec.Name = '.') or (sRec.Name = '..') then
             Continue;
           subdirs.Add(toSearch + '\' + sRec.Name);
         until FindNext(sRec) <> 0;
       end;
       FindClose(sRec);
       if FindFirst(toSearch + '\' + aFileName, faAnyFile, sRec) = 0 then begin
         repeat
           aDestFiles.Add(toSearch + '\' + sRec.Name);
         until FindNext(sRec) <> 0;
       end;
       FindClose(sRec);
     end;
   finally
     FreeAndNil(subdirs);
   end;
   Result := aDestFiles.Count > 0;
 end;
Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122