PDA

Click to See Complete Forum and Search --> : VB - Detect File Type


TheVader
Feb 24th, 2004, 11:16 AM
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. :rolleyes:

Can anyone optimize this code so it runs a bit faster?

'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:

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. :)

si_the_geek
Feb 24th, 2004, 12:14 PM
:eek: 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:

'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:

'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.

TheVader
Feb 24th, 2004, 12:23 PM
:wave: 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. :(

si_the_geek
Feb 24th, 2004, 12:28 PM
:)

I know in terms of how the code looks there isn't much difference, but all functions are slower that accessing a variable (even with VBs' built in ones).

Calling a function takes a while as the memory for it has to be allocated & initialised, then the function is invoked, and then the memory has to be cleared. Reading a variable simply needs reading a piece of memory.

Originally posted by TheVader
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. :(
oh well :(

I've seen a few examples from vbAccellerator, and they always seem pretty good. maybe they have an example of how to do it better?