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.![]()



Reply With Quote
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!
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.
