Results 1 to 4 of 4

Thread: VB - Detect File Type

  1. #1
    Fanatic Member TheVader's Avatar
    Join Date
    Oct 02
    Location
    Rotterdam, the Netherlands
    Posts
    871

    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:
    1. 'Define filetypes and add files
    2. For i = 0 To frmContentCreator.lstFiles.ListCount - 1
    3.     strFile = frmContentCreator.lstFiles.List(i)
    4.  
    5.     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
    6.         'Webpage
    7.         frmContentCreator.exbServer.Bars.Item(2).Items.Add , , strFile, 2
    8.     ElseIf GetExt(strFile) = "jpg" Or GetExt(strFile) = "jpeg" Or GetExt(strFile) = "gif" Or GetExt(strFile) = "png" Or GetExt(strFile) = "" Then
    9.         'Image
    10.         frmContentCreator.exbServer.Bars.Item(3).Items.Add , , strFile, 3
    11.     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
    12.         'Document
    13.         Select Case GetExt(strFile)
    14.             Case "doc", "dot", "rtf": frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 9
    15.             Case "ppt": frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 8
    16.             Case "xls": frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 7
    17.             Case "pdf": frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 6
    18.             Case "swf", "dcr": frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 5
    19.             Case "class": frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 3
    20.             Case "mpg", "mpeg", "mov", "avi": frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 3
    21.             Case "snd", "mp3", "mid", "wav": frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 3
    22.             Case "zip", "arc", "cab", "tar", "rar", "ace": frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 3
    23.         End Select
    24.     Else
    25.         'File
    26.         frmContentCreator.exbServer.Bars.Item(5).Items.Add , , strFile, 10
    27.     End If
    28. Next

    The GetExt() function looks like this:

    VB Code:
    1. Public Function GetExt(Path As String, Optional LowerCase As Boolean = True) As String
    2. If LowerCase Then GetExt = LCase(Mid$(Path, InStrRev(Path, ".", , vbTextCompare) + 1)) Else GetExt = Mid$(Path, InStrRev(Path, ".", , vbTextCompare) + 1)
    3. End Function

    Oh, and btw, the exbServer component is a vbAccelerator Explorer Bar. Thank you all.
    Author for Visual Basic Web Magazine

    My articles on the Web Browser Control:
    Using the Web Browser Control & Using the DHTML Document Object Model

    The examples referenced in the articles can be found here:

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 02
    Location
    Bristol, UK
    Posts
    35,548

    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:
    1. 'Define filetypes and add files
    2. For i = 0 To frmContentCreator.lstFiles.ListCount - 1
    3.     strFile = frmContentCreator.lstFiles.List(i)
    4.     strExt = GetExt(strFile)
    5.  
    6.     If strExt = "htm" Or strExt = "html" ....
    7.         'Webpage
    8.         frmContentCreator.exbServer.Bars.Item(2).Items.Add , , strFile, 2
    9.     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:
    1. 'Define filetypes and add files
    2. For i = 0 To frmContentCreator.lstFiles.ListCount - 1
    3.     strFile = frmContentCreator.lstFiles.List(i)
    4.  
    5.     Select Case GetExt(strFile)
    6.         'Webpage
    7.     Case "htm", "html", "shtm", "shtml", "php", "php3", "php4", _
    8.          "asp", "hta", "htx", "pht", "phtml", "cfm", "cfml"
    9.         frmContentCreator.exbServer.Bars.Item(2).Items.Add , , strFile, 2
    10.         'Image
    11.     Case "jpg", "jpeg", "gif", "png", ""
    12.         frmContentCreator.exbServer.Bars.Item(3).Items.Add , , strFile, 3
    13.         'Document
    14.     Case "doc", "dot", "rtf"
    15.         frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 9
    16.     Case "ppt"
    17.         frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 8
    18.     Case "xls"
    19.         frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 7
    20.     Case "pdf"
    21.         frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 6
    22.     Case "swf", "dcr"
    23.         frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 5
    24.     Case "class"
    25.         frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 3
    26.     Case "mpg", "mpeg", "mov", "avi"
    27.         frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 3
    28.     Case "snd", "mp3", "mid", "wav"
    29.         frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 3
    30.     Case "zip", "arc", "cab", "tar", "rar", "ace"
    31.         frmContentCreator.exbServer.Bars.Item(4).Items.Add , , strFile, 3
    32.     Case Else
    33.         'File
    34.         frmContentCreator.exbServer.Bars.Item(5).Items.Add , , strFile, 10
    35.     End Select
    36. 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.

  3. #3
    Fanatic Member TheVader's Avatar
    Join Date
    Oct 02
    Location
    Rotterdam, the Netherlands
    Posts
    871
    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

    My articles on the Web Browser Control:
    Using the Web Browser Control & Using the DHTML Document Object Model

    The examples referenced in the articles can be found here:

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 02
    Location
    Bristol, UK
    Posts
    35,548


    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •