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
Manuel Lopez-MichelonePhD candidateCommented:
A RTF starts with {\rtf. Read this from your target file and your problem is over.
Stef MerlijnDeveloperAuthor Commented:
Thank you for your answers, but I was looking for a function that would return the filetype.
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Manuel Lopez-MichelonePhD candidateCommented:
A procedure written in  minutes. You'll get the idea:

procedure TForm1.Button1Click(Sender: TObject);
    File1    : string;
    InFile   : file of char;
    FileDesc : string;
    i        : byte;
    B        : char;
  if Opendialog1.FileName <> '' then File1 := OpenDialog1.Filename;
  FileDesc := '';
  AssignFile(Infile, File1);
  for i := 1 to 5 do
    FileDesc := FileDesc + B;
  if FileDesc = '{\rtf' then Showmessage('File '+ File1 + ' is a RTF file');
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;
  Result := False;
  LStrings := TStringList.Create;
    Result := (Copy(LStrings.text, 1, 5) = '{\rtf');

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
Manuel Lopez-MichelonePhD candidateCommented:
I tested my code and it works without problems. What kind of answer gave you FileDesc?
Stef MerlijnDeveloperAuthor Commented:
It had some chinese looking content.
Manuel Lopez-MichelonePhD candidateCommented:
Really? strange. Maybe something in your configuration I guess. Anyway, thanks for the answer.
Manuel Lopez (lopem)
Stef MerlijnDeveloperAuthor Commented:
Any idea what might have caused that?
Stef MerlijnDeveloperAuthor Commented:
Thank you all.
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

From novice to tech pro — start learning today.