Check for invalid file types?

R

Robert Crandal

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.

Thank you!

Robert
 
A

Auric__

Robert said:
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 here:
'[...]
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.
 
G

GS

Robert Crandal used his keyboard to write :
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.

Thank you!

Robert

I use the Filters property of a FileDialog to limit what files can be
seen. For example, my cnc program files manager app only lets users see
files with the user-defined filetypes. The default is no extension as
cnc files normally have none (ie: "O1001" is a Fanuc style program
file), but some retrofit machines have their own filetypes. The
FileTypes list is what a Filter property uses for file browsing.

My Excel apps use their own filename extensions and so I filter on
these as defaults when browsing for app files.

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top