![]() |
Check for invalid file types?
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 |
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. |
Check for invalid file types?
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 |
All times are GMT +1. The time now is 10:10 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com