Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

finding file on drive

Status
Not open for further replies.

domkop

Programmer
Aug 19, 2003
22
NL
i want to know if an file exists on the computer.
does anyone has an source or example of how to do this
i can't seem to get the grip on this
 
Here's the Delphi help on the FileExists function:
Code:
Tests if a specified file exists.

Unit

SysUtils

Category

file management routines

function FileExists(const FileName: string): Boolean;

Description

FileExists returns True if the file specified by FileName exists. If the file does not exist, FileExists returns False.



Leslie
 
DomKop

I am presuming you mean "anywhere" on "computer"... and as such the previous post will not accomplish what you want.

There is a program that has been around for a L-O-N-G time called "whereis.exe"....

The source should be available... but it will entail the systematic perusal of ALL the directories on ALL drives in "question"....

The command whereis.exe finds "all" occurances of the filename specified... whereas, if you had the source (converted to Delphi), you could "stop" when the file was found (if..).



Regards and HTH,
JGS
 
the way to do this is to set up a recursive search using the findfirst and findnext functions

This should demonstrate the technique (its not recursive, it uses a loop and is flawed because an exception is raised if a folder is private, hence the exception handling stuff)

Code:
// Search procedure looks for files with given filter in root folder
// sub folders are searched, file attributes can be applied
// . & .. items are ignored
procedure TForm1.Search(Root: Tfilename; Filter: string; Attr: integer; var FList: Tstringlist);
var x, FF: integer;
    Folders: TStringlist;
    F: TsearchRec;
begin
   Folders := TStringList.Create;
   Filter := uppercase(Filter);
   x := NO_ERROR;
   repeat
      application.ProcessMessages;
      if findfirst(addslash(Root) + '*', Attr, F) = 0 then
         begin
            ff := 0;
            repeat
               application.ProcessMessages;
               x := findnext(F);
               if x = 0 then
                  begin
                     // is it a folder ?
                     if (F.Attr and $10) = $10 then
                        begin
                           if F.name <> '..' then
                              begin
                                 Folders.Insert(0, addslash(Root) + F.Name);
                                 DiagBox.Items.Add('ADD FOLDER '+ addslash(Root) + F.Name);
                                 inc(ff);
                              end;

                           if (ff = 0) and (folders.Count > 0) then
                               begin
                                  Diagbox.Items.Add('DELETING FOLDER '+ Folders.Strings[0]);
                                  Folders.Delete(0);
                               end;
                        end
                     else
                        if (uppercase(extractfileext(f.name))) = Filter then
                            begin
                               FList.Add(addslash(Root) + F.name);
                               Diagbox.Items.Add('ADD FILE ' + addslash(Root) + F.name+ ' FILTER '+ Filter);
                            end
                  end
            until x <> 0;
        end;

        // attempt to trap access denied error loop condition
        // Delete the current folder if theere are any incorrect errors
        if (x <> ERROR_NO_MORE_FILES) and( folders.Count > 0)then
            begin
               diagbox.Items.Add('BAD ERROR A '+ inttostr(x)+': DELETING '+ Folders.Strings[0]);
               Folders.Delete(0);
            end;

        if (GetLastError <> ERROR_NO_MORE_FILES) and( folders.Count > 0)then
            begin
              diagbox.Items.Add('BAD ERROR B '+ inttostr(x)+': DELETING '+ Folders.Strings[0]);
              Folders.Delete(0);
              SetLastError(NO_ERROR);
            end;
        // now start looking in the folders
        findclose(F);

        // Get next root
        if folders.count > 0 then
            begin
               diagbox.Items.Add('NEW ROOT '+ Folders.Strings[0]+ ' ERROR: '+ inttostr(x));
               // another catch me if we try to add the same root again it must be wrong so delete it
               if Root = Folders.Strings[0] then
                     begin
                        diagbox.Items.Add('DUPLICATE ERROR: DELETING '+ Folders.Strings[0]);
                        Folders.Delete(0);
                     end;
               Root := Folders.Strings[0];
            end;
   until Folders.count <= 0;

   for x := 0 to Folders.count -1 do
      Folders.Strings[x] := '';
   Folders.Free;
end;



Steve
Be excellent to each other and Party on!
 
The original request is ambiguous. The thread title says finding file on drive but the text says want to know if a file exists on the computer.

The following function will return the path of the folder of the first occurence of the filename if it exists on the drive otherwise it returns an empty string.

Code:
function WhereIs ( path: string; const filename: string ): string;

  function IsDirectory ( const tsr: TSearchRec ): boolean;
  begin
    result := ( tsr.Attr and faDirectory ) = faDirectory;
  end;

  procedure RecursiveWhereIs ( path: string; filename: string );
  var
    tsr: TSearchRec;
  begin
    Application.ProcessMessages;
    path := IncludeTrailingPathDelimiter(path);
    if FindFirst ( path + '*.*', faDirectory, tsr ) = 0 then begin
      repeat
        if AnsiCompareText ( tsr.Name, filename ) = 0 then
          result := path
        else if IsDirectory ( tsr ) and ( tsr.Name[1] <> '.' ) then
          RecursiveWhereIs ( path + tsr.Name, filename );
        if result <> '' then
          exit;
      until FindNext ( tsr ) <> 0;
      FindClose ( tsr );
    end;
  end;

begin
  result := '';
  RecursiveWhereIs ( path, filename );
end;
It can be called from a button click as in this example:
Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit2.Text := WhereIs ( 'C:\', Edit1.Text );
end;

It shouldn't bee too difficult to adapt this code to display all the directory names containing the required file it that is required.

Andrew
Hampshire, UK
 
as you can read i am a beginner, when i cut en paste it to delphi it gives me back errors.
what's wrong

 
sorry paste it in another procedure that was wrong

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top