Vbscript Code

Vbscript Code

I'm playing around with this code from the tutorials.


Sub Object_OnDropFiles(files)
Object.picture = files
End Sub


How would we test for valid file formats (bmp,ico,png,tga,jpg) for the picture file being dragged?


I want to do something like this, but do not know the correct syntax.


If filename extension <> bmp,ico,png,tga or jpg then I display a message box message, then exit the subroutine to try again.


Thanks


 

5,999 views 7 replies
Reply #1 Top
Hi there. I used this very technique in my Hardest Button to Button: http://rabidrobot.wincustomize.com/ViewSkin.aspx?SkinID=579&LibID=34&comments=1

Essentially you'll need to use some string manipulation functions to find the file's extension, then compare it with the acceptable extensions. Import my widget and you'll get the idea. There may be one thing you find odd, though, so I'll try to explain from memory what was happening. When multiple files are dragged, my script would take the first one, found by splitting the filelist by "|" chars. (You could also just refuse multi-file drags instead). But, when one file only is dragged I had a heck of a time getting it accepted--no matter how I phrased the comparasin it never seemed to work. A couple hints from folks on #stardock helped me discover that there was some kind of Null character being appended to the filename when only one file was dragged. So, as you'll see in my code, you have to chop off that last character, then take the remaining last three as the extension.

Whew. Hope all that is some help, gl.

rr
Reply #2 Top
Thanks for the link rabidrobot. I downloaded it and will work with it. Thanks again.
Reply #3 Top
What you need to do is split the string first to see if mulitple files are dropped. fileArray = Split(files, "|") You now have the list of all the files stored in a array named "fileArray". Loop through this array to check the file extensions. Then to get the file extension you split it again, looking for the dot (.) . yourFile = Split(files, ".") the extension would now be stored in the last item in the array returned.
Reply #4 Top
thomassen is right, puts it much more clearly than I did. I'd only add that you may want to UCase() the extension once you've split it off, and compare it to a upper case extension. I mean, ucase(theFileExtension) = ".MP3" will accept *.mp3 files and *.MP3 files.

I also wanted to stress that in my experience there is a null char at the end when only one file is dropped. Perhaps this is not intended and a DX bug, but for now, if you
fileExt = Split(SingleDroppedJPG, ".")
and compare the extension
fileExt(1) = "jpg"
it will not work, because fileExt(1) actually equals "jpg#" where # is invisible but there.
Reply #5 Top
I also wanted to stress that in my experience there is a null char at the end when only one file is dropped.

Oh yea. That's true. That cause me alot of hazzle before. Though, I thought it has been fixed now. (but I might be wrong. However, this can easily be checked by extracting the last character of the file string and see if it's a null character.)
Reply #6 Top
hm... doesn't Trim tsrip of null characters?
Reply #7 Top
You can also use the FileSystemObject method GetExtensionName.

Link