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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to determine file type from header record?

Status
Not open for further replies.

8MaRk4

IS-IT--Management
Mar 6, 2001
4
CA
I need to search all files on a netware server for a particular file type, ie WordPerfect files, however there are a number of these files which do not have the .wpd extension. Is there a way to read the file header record and test for the file type?

Mark.
 
If you know the format of WordPerfect files, you can certainly read x bytes and try and determine if the file is a match or not.

Chip H.
 
I believe that WordPerfect files begin with an ASCII 255 followed by "WPC" (I kinda like that...). You might have decent results if you....
[tt]
Search$ = Chr$(255) & "WPC"
Test$ = String$(4, 0)
ff = FreeFile
Fi$ = "C:\MyFile.WPG"
Open Fi$ For Binary As #ff
Get #ff, 1, Test$
Close #ff
If Search$ = Test$ Then
MsgBox "This is a WordPerfect file."
End If
[/tt]
VCA.gif
 
Thanks Chip, I guess my question is a bit more basic. How would I manage to read the header record in the first place is what I am trying to determine. Any suggestions?

Mark.
 
Thanks Alt255, That is exactly what I needed. Works great!
 
Do like Alt255 suggests and open the file as binary. Read the first 4 bytes to see if they're equal to hex 255, followed by "WPC". You can easily do this by reading a Long (a 4-byte value), and comparing it to:
[tt]
255[tab]= &hff x 16777216 = 4278190080
W [tab]= &h57 x 65536 = 5701632
P [tab]= &h50 x 256 = 20480
C [tab]= &h43 x 1 = 67
---------------
= 4283912259
[/tt]
But since VB only knows about signed Longs, that becomes:
-2136428612

Complicated, but it will make things faster.

Chip H.

 
Interesting. I had always assumed that the VB Get statement would retrieve a 4-byte string at the same approximate speed as a 4-byte number.

In any case, reading a string may allow a slight advantage by providing additional information:
Code:
Test$ = String$(10, 0)
ff = FreeFile
Fi$ = "c:\MyFile.doc"
Open Fi$ For Binary As #ff
Get #ff, 1, Test$
Close #ff
If Left$(Test$, 4) <> Chr$(255) & &quot;WPC&quot; Then
    Exit Sub
End If
FileType% = Asc(Right$(Test$, 1))
Select Case FileType%
    Case 1: Ret$ = &quot;macro file&quot;
    Case 2: Ret$ = &quot;help file&quot;
    Case 3: Ret$ = &quot;keyboard definition file&quot;
    Case 10: Ret$ = &quot;document file&quot;
    Case 11: Ret$ = &quot;dictionary file&quot;
    Case 12: Ret$ = &quot;thesaurus file&quot;
    Case 13: Ret$ = &quot;block&quot;
    Case 14: Ret$ = &quot;rectangular block&quot;
    Case 15: Ret$ = &quot;column block&quot;
    Case 16: Ret$ = &quot;printer resource file&quot;
    Case 17: Ret$ = &quot;setup file&quot;
    Case 18: Ret$ = &quot;prefix information file&quot;
    Case 19: Ret$ = &quot;printer resource file&quot;
    Case 20: Ret$ = &quot;display resource file&quot;
    Case 21: Ret$ = &quot;overlay file&quot;
    Case 22: Ret$ = &quot;graphics file&quot;
    Case 23: Ret$ = &quot;hyphenation code module&quot;
    Case 24: Ret$ = &quot;hyphenation data module&quot;
    Case 25: Ret$ = &quot;macro resource file&quot;
    Case 26: Ret$ = &quot;graphics driver&quot;
    Case 27: Ret$ = &quot;hyphenation lex module&quot;
    Case Else: Ret$ = &quot;(??) WordPerfect file&quot;
End Select
MsgBox Fi$ & &quot; is a &quot; & Ret$ & &quot;.&quot;
Naturally, in the absence of a requirement for additional information, the fastest way is the way to go. :)

VCA.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top