Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
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.
// 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;
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;
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit2.Text := WhereIs ( 'C:\', Edit1.Text );
end;