Determine internal filetype of document (rtf or doc)

I have several documents that are actually *.rtf files, but the extension was changed to *.doc.
Now I need to know if it is actually a *.rtf or a real *.doc file.

So I'm looking for a Delphi function that will determine if a document-file internally is ​a *.rtf of *.doc file.
The function should handle older version of word documents too (like Word 98).
Stef MerlijnDeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sinisa VukSoftware architectCommented:
Simple - load first bytes of each file and check:
- if there is: {\rtf  (hex: 7b 5c 72 74 66) - then it is surely .rtf
- otherwise - is doc. - doc is a separate problem - there are many types of doc - for MS Doc first two bytes are: hex: D0 CF
0
lopemCommented:
A RTF starts with {\rtf. Read this from your target file and your problem is over.
0
Stef MerlijnDeveloperAuthor Commented:
Thank you for your answers, but I was looking for a function that would return the filetype.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

lopemCommented:
A procedure written in  minutes. You'll get the idea:

procedure TForm1.Button1Click(Sender: TObject);
var
    File1    : string;
    InFile   : file of char;
    FileDesc : string;
    i        : byte;
    B        : char;
begin
  OpenDialog1.Execute;
  if Opendialog1.FileName <> '' then File1 := OpenDialog1.Filename;
  FileDesc := '';
  AssignFile(Infile, File1);
  Reset(Infile);
  for i := 1 to 5 do
  begin
    read(Infile,B);
    FileDesc := FileDesc + B;
  end;
  CloseFile(Infile);
  if FileDesc = '{\rtf' then Showmessage('File '+ File1 + ' is a RTF file');
end;
0
Stef MerlijnDeveloperAuthor Commented:
FileDesc was scrambled, so it was never a rtf. Some changes made it work.

function TForm1.BestandIsRTF(CONST vBestandsnaam : String) : Boolean;
var LStrings: TStringList;
begin
  Result := False;
  LStrings := TStringList.Create;
  try
    LStrings.Loadfromfile(vBestandsnaam);
    Result := (Copy(LStrings.text, 1, 5) = '{\rtf');
  finally
    FreeAndNil(LStrings);
  end;
end;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lopemCommented:
I tested my code and it works without problems. What kind of answer gave you FileDesc?
0
Stef MerlijnDeveloperAuthor Commented:
It had some chinese looking content.
0
lopemCommented:
Really? strange. Maybe something in your configuration I guess. Anyway, thanks for the answer.
Manuel Lopez (lopem)
0
Stef MerlijnDeveloperAuthor Commented:
Any idea what might have caused that?
0
Stef MerlijnDeveloperAuthor Commented:
Thank you all.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.