|
-
Feb 24th, 2004, 11:16 AM
#1
Thread Starter
Fanatic Member
VB - Detect File Type
OK, I'm reading files from an FTP server and I'm detecting the filetypes. It takes quite some time, however, to detect all files - up to a few seconds. In fact it takes more time to detect the filetypes than to make the FTP connection. 
Can anyone optimize this code so it runs a bit faster?
VB Code:
'Define filetypes and add files
For i = 0 To frmContentCreator.lstFiles.ListCount - 1
strFile = frmContentCreator.lstFiles.List(i)
If GetExt(strFile) = "htm" Or GetExt(strFile) = "html" Or GetExt(strFile) = "shtm" Or GetExt(strFile) = "shtml" Or GetExt(strFile) = "php" Or GetExt(strFile) = "php3" Or GetExt(strFile) = "php4" Or GetExt(strFile) = "asp" Or GetExt(strFile) = "hta" Or GetExt(strFile) = "htx" Or GetExt(strFile) = "pht" Or GetExt(strFile) = "phtml" Or GetExt(strFile) = "cfm" Or GetExt(strFile) = "cfml" Then
'Webpage
frmContentCreator.exbServer.Bars.Item(2).Items.Add , , strFile, 2
ElseIf GetExt(strFile) = "jpg" Or GetExt(strFile) = "jpeg" Or GetExt(strFile) = "gif" Or GetExt(strFile) = "png" Or GetExt(strFile) = "" Then
'Image
frmContentCreator.exbServer.Bars.Item(3).Items.Add , , strFile, 3
ElseIf GetExt(strFile) = "doc" Or GetExt(strFile) = "dot" Or GetExt(strFile) = "rtf" Or GetExt(strFile) = "ppt" Or GetExt(strFile) = "xls" Or GetExt(strFile) = "pdf" Or GetExt(strFile) = "swf" Or GetExt(strFile) = "dcr" Or GetExt(strFile) = "class" Or GetExt(strFile) = "mpg" Or GetExt(strFile) = "mpeg" Or GetExt(strFile) = "mov" Or GetExt(strFile) = "avi" Or GetExt(strFile) = "snd" Or GetExt(strFile) = "mp3" Or GetExt(strFile) = "wav" Or GetExt(strFile) = "mid" Or GetExt(strFile) = "zip" Or GetExt(strFile) = "arc" Or GetExt(strFile) = "cab" Or GetExt(strFile) = "tar" Or GetExt(strFile) = "rar" Or GetExt(strFile) = "ace" Then
'Document
Select Case GetExt(strFile)
Case "doc", "dot", "rtf": frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 9
Case "ppt": frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 8
Case "xls": frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 7
Case "pdf": frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 6
Case "swf", "dcr": frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 5
Case "class": frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 3
Case "mpg", "mpeg", "mov", "avi": frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 3
Case "snd", "mp3", "mid", "wav": frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 3
Case "zip", "arc", "cab", "tar", "rar", "ace": frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 3
End Select
Else
'File
frmContentCreator.exbServer.Bars.Item(5).Items.Add , , strFile, 10
End If
Next
The GetExt() function looks like this:
VB Code:
Public Function GetExt(Path As String, Optional LowerCase As Boolean = True) As String
If LowerCase Then GetExt = LCase(Mid$(Path, InStrRev(Path, ".", , vbTextCompare) + 1)) Else GetExt = Mid$(Path, InStrRev(Path, ".", , vbTextCompare) + 1)
End Function
Oh, and btw, the exbServer component is a vbAccelerator Explorer Bar. Thank you all.
Author for Visual Basic Web Magazine
-
Feb 24th, 2004, 12:14 PM
#2
Re: VB - Detect File Type
you should NEVER call a function more times than you have to.. just call it once and store the result. In your code you had over 40 calls to GetExt (all returning the same result), and at least 14 calls are made in each iteration of the loop!
here's part of an example of using that:
VB Code:
'Define filetypes and add files
For i = 0 To frmContentCreator.lstFiles.ListCount - 1
strFile = frmContentCreator.lstFiles.List(i)
strExt = GetExt(strFile)
If strExt = "htm" Or strExt = "html" ....
'Webpage
frmContentCreator.exbServer.Bars.Item(2).Items.Add , , strFile, 2
ElseIf strExt = "jpg" Or strExt = "jpeg" ....
the next thing I noticed, to check the extension you are using lots of IF's and OR's (on the same text), then a select. why not use a select on the whole lot? it should improve the speed a bit (and the readability). This would also remove the need for a variable to store the function result.
You can also remove the line "ElseIf GetExt(strFile) = "doc"... " if you just put the " 'File " part into an else clause on the select, eg:
VB Code:
'Define filetypes and add files
For i = 0 To frmContentCreator.lstFiles.ListCount - 1
strFile = frmContentCreator.lstFiles.List(i)
Select Case GetExt(strFile)
'Webpage
Case "htm", "html", "shtm", "shtml", "php", "php3", "php4", _
"asp", "hta", "htx", "pht", "phtml", "cfm", "cfml"
frmContentCreator.exbServer.Bars.Item(2).Items.Add , , strFile, 2
'Image
Case "jpg", "jpeg", "gif", "png", ""
frmContentCreator.exbServer.Bars.Item(3).Items.Add , , strFile, 3
'Document
Case "doc", "dot", "rtf"
frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 9
Case "ppt"
frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 8
Case "xls"
frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 7
Case "pdf"
frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 6
Case "swf", "dcr"
frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 5
Case "class"
frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 3
Case "mpg", "mpeg", "mov", "avi"
frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 3
Case "snd", "mp3", "mid", "wav"
frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 3
Case "zip", "arc", "cab", "tar", "rar", "ace"
frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 3
Case Else
'File
frmContentCreator.exbServer.Bars.Item(5).Items.Add , , strFile, 10
End Select
Next i '(you should always put the variable name, it makes it harder to make mistakes!)
This still isn't the fastest method, but should be a big improvement on what you have already. It would probably be best to have an array list of extensions (with a reference to file types), and then search the array for matches.
-
Feb 24th, 2004, 12:23 PM
#3
Thread Starter
Fanatic Member
Thanks a lot for the help. Actually I didn't know that calling functions more than once cost a lot of extra time (if the function isn't too long, that is). The code is indeed running faster now. However, I've also found that part of the problem lies with the vbAccelerator Explorer Bar Control. It's awfully slow sometimes; guess it wasn't designed to hold many items.
Author for Visual Basic Web Magazine
-
Feb 24th, 2004, 12:28 PM
#4
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|