VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



FilesSearch

by Andrew Tang (1 Submission)
Category: Files/File Controls/Input/Output
Compatability: Visual Basic 3.0
Difficulty: Unknown Difficulty
Date Added: Wed 3rd February 2021
Rating: (8 Votes)

This sub/function searches your hard drive(s) or directories for file(s) like the Windows 'Find Files or Folders...'. It uses mainly the Dir() command and can be used with any programs and visual basic I have encountered. This helps uses to quickly find a file or program for their applications.

Inputs
It needs two parameters, the start directory or drive and the extension. Example: FilesSearch "C:\", "*.txt".
Assumes
None that I am aware of.
Code Returns
Finds the file(s) with a particular extension from a start directory.
Side Effects
None. Slightly slower than the windows 'Find Files or Folder...' function.
API Declarations

Rate FilesSearch

Sub FilesSearch(DrivePath As String, Ext As String)
Dim XDir() As String
Dim TmpDir As String
Dim FFound As String
Dim DirCount As Integer
Dim X As Integer
'Initialises Variables
DirCount = 0
ReDim XDir(0) As String
XDir(DirCount) = ""
If Right(DrivePath, 1) <> "\" Then
  DrivePath = DrivePath & "\"
End If
'Enter here the code for showing the path being
'search. Example: Form1.label2 = DrivePath
'Search for all directories and store in the
'XDir() variable
DoEvents
TmpDir = Dir(DrivePath, vbDirectory)
Do While TmpDir <> ""
  If TmpDir <> "." And TmpDir <> ".." Then
    If (GetAttr(DrivePath & TmpDir) And vbDirectory) = vbDirectory Then
      XDir(DirCount) = DrivePath & TmpDir & "\"
      DirCount = DirCount + 1
      ReDim Preserve XDir(DirCount) As String
    End If
  End If
  TmpDir = Dir
Loop
'Searches for the files given by extension Ext
FFound = Dir(DrivePath & Ext)
Do Until FFound = ""
  'Code in here for the actions of the files found.
  'Files found stored in the variable FFound.
  'Example: Form1.list1.AddItem DrivePath & FFound
  FFound = Dir
Loop
'Recursive searches through all sub directories
For X = 0 To (UBound(XDir) - 1)
  FilesSearch XDir(X), Ext
Next X
End Sub

Download this snippet    Add to My Saved Code

FilesSearch Comments

No comments have been posted about FilesSearch. Why not be the first to post a comment about FilesSearch.

Post your comment

Subject:
Message:
0/1000 characters