April 21, 2003

RefTools’ Algorithms: Look-up Strategies

his is the first in a short series of articles describing Visual Basic components from the RefTools add-in. A first conclusion is that simple binary search of a sorted list is the fastest for two data sets and a close second for the third.

SearchFileInDirTree – Searches a file on a directory tree

Shared Function _ SearchTreeForFile(ByVal rootPath As String, ByVal inputPathName As String, _ ByVal outputPathBuffer As System.Text.StringBuilder) As BooleanEnd Function’ Returns the complete path+name of the filename or a null string if the’ filename hasn’t been found. Only the first occurrence of the file is returned.” Example: Debug.WriteLine(SearchFileInDirTree(“d:codearchitectscodebox”,’ “codebox.exe”))Function SearchFileInDirTree(ByVal rootDir

SearchFileOnPath – Searching a file on the system

Shared Function _ SearchPath(ByVal tartPath As String, ByVal fileName As String, _ ByVal extension As String, ByVal bufferLength As Integer, _ ByVal buffer As System.Text.StringBuilder, ByVal filePart As String) As _ IntegerEnd Function’ Searches a file on the system’ Returns the complete file path+name, or “” if file not found”

IsExecutableFile – Returns whether the file is an executable

‘ Returns a boolean indicating whether the file is an executableFunction IsExecutableFile(ByVal filePath As String) As Boolean ‘ add more extensions if you wish Dim extensions() As String = New String() {“.exe”, “.bat”, “.com”, “.pif”} ‘ return true if the extension of the specified file is contained in the ‘

IsImageFile – Returns whether the file is an image

‘ Returns a boolean indicating whether the file is an imageFunction IsImageFile(ByVal filePath As String) As Boolean ‘ add more extensions if you wish Dim extensions() As String = New String() {“.bmp”, “.jpg”, “.jpeg”, “.gif”, _ “.tif”, “.tiff”, “.png”, “.tga”, “.emf”, “.wmf”, “.bid”} ‘ return true if the extension of

IsDriveReady – Returns whether a drive is ready

‘ Returns a boolean indicating whether a given drive is ready’ Example: check if the floppy disk drive is ready’ Debug.WriteLine(IsDriveReady(“a”))Function IsDriveReady(ByVal driveLetter As String) As Boolean If driveLetter.Length = 1 Then driveLetter &= “:” Dim drive As New System.IO.DirectoryInfo(driveLetter) Try ‘ if the drive is not ready, the following