View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Auric__ Auric__ is offline
external usenet poster
 
Posts: 538
Default Check for invalid file types?

Robert Crandal wrote:

I am using the FileDialog() function to let users select multiple
files. After the user chooses all their files and presses the
"Okay" button, my program loops to each chosen file.

Is there an effective way to test when a user chooses an
invalid file type? My users are only allowed to choose
plain text files, hopefully files ending in *.txt. I'm just
looking for ideas how to prevent users from choosing
files that contain non-text data.


Not really. You can set your function to only open files with the extension
..txt, but that doesn't really guarantee anything, especially if they have
text files that *don't* end with .txt.

If you're looking for plain old 7-bit ASCII files (as opposed to Unicode,
UTF-8/16, etc.) you can scan each file as you get to it, sorta like this:

For file = 0 To UBound(FileList)
fn = FreeFile
Open FileList(file) For Binary As fn
contents$ = Space$(LOF(fn))
Get #fn, 1, contents$
Close fn
For L0 = 1 To Len(contents$)
Select Case Asc(Mid$(contents$, L0, 1))
'adjust as necessary
Case 0 To 8, 14 To 25, 27 To 31, 127 To 255
'file has non-text characters
GoTo skipper
End Select
Next
'process the file he
'[...]
skipper:
Next

There are, of course, other possibilities. You could, for example, use
TrIDlib:

http://mark0.net/code-tridlib-e.html

....or other, more exotic methods.

--
I'm gonna sit down now and ignore you, so my head doesn't explode.