Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Search

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello,

Is it exist a function which return the complete path of a file ?? (In VB)

Example: I want to know the path of the file "DAO350.dll" on C:
Thanks

Fred
 
'Add 1 Command Button, 4 Text Boxes and 1 List Box to your Form.
'At Run-Time, Enter the path that you want to start to search from it to Text1,
'Enter the file pattern to Text2 (like *.* or *.exe), and press the button.
'List1 will be filled with all the matching files, Text3 will display the number of files found,
'And Text4 will display the total size of the found files.
'Insert the following code to your form:

Function FindFiles(path As String, SearchStr As String, _
FileCount As Integer, DirCount As Integer)
Dim FileName As String
Dim DirName As String
Dim dirNames() As String
Dim nDir As Integer
Dim i As Integer
On Error GoTo sysFileERR
If Right(path, 1) <> &quot;\&quot; Then path = path &amp; &quot;\&quot;
nDir = 0
ReDim dirNames(nDir)
DirName = Dir(path, vbDirectory Or vbHidden)
Do While Len(DirName) > 0
If (DirName <> &quot;.&quot;) And (DirName <> &quot;..&quot;) Then
If GetAttr(path &amp; DirName) And vbDirectory Then
dirNames(nDir) = DirName
DirCount = DirCount + 1
nDir = nDir + 1
ReDim Preserve dirNames(nDir)
End If
sysFileERRCont:
End If
DirName = Dir()
Loop
FileName = Dir(path &amp; SearchStr, vbNormal Or vbHidden Or vbSystem _
Or vbReadOnly)
While Len(FileName) <> 0
FindFiles = FindFiles + FileLen(path &amp; FileName)
FileCount = FileCount + 1
List1.AddItem path &amp; FileName
FileName = Dir()
Wend
If nDir > 0 Then
For i = 0 To nDir - 1
FindFiles = FindFiles + FindFiles(path &amp; dirNames(i) &amp; &quot;\&quot;, _
SearchStr, FileCount, DirCount)
Next i
End If
AbortFunction:
Exit Function
sysFileERR:
If Right(DirName, 4) = &quot;.sys&quot; Then
Resume sysFileERRCont
Else
MsgBox &quot;Error: &quot; &amp; Err.Number &amp; &quot; - &quot; &amp; Err.Description, , _
&quot;Unexpected Error&quot;
Resume AbortFunction
End If
End Function

Private Sub Command1_Click()
Dim SearchPath As String, FindStr As String
Dim FileSize As Long
Dim NumFiles As Integer, NumDirs As Integer
Screen.MousePointer = vbHourglass
List1.Clear
SearchPath = Text1.Text
FindStr = Text2.Text
FileSize = FindFiles(SearchPath, FindStr, NumFiles, NumDirs)
Text3.Text = NumFiles &amp; &quot; Files found in &quot; &amp; NumDirs + 1 &amp; _
&quot; Directories&quot;
Text4.Text = &quot;Size of files found under &quot; &amp; SearchPath &amp; &quot; = &quot; &amp; _
Format(FileSize, &quot;#,###,###,##0&quot;) &amp; &quot; Bytes&quot;
Screen.MousePointer = vbDefault
End Sub


Eric De Decker
vbg.be@vbgroup.nl

Licence And Copy Protection AxtiveX
Source CodeBook for the programmer
 
Option Explicit

Private Declare Function ShellExecute Lib &quot;shell32.dll&quot; Alias &quot;ShellExecuteA&quot; (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Const SW_SHOWNORMAL = 1
Private Const SW_SHOWMINIMIZED = 2
Private Const SW_SHOWMAXIMIZED = 3
Private Const SW_SHOW = 5
Private Const SW_MINIMIZE = 6
Private Const SW_SHOWMINNOACTIVE = 7
Private Const SW_SHOWNA = 8
Private Const SW_RESTORE = 9
Private Const SW_SHOWDEFAULT = 10

Private Sub Form_Load()
Dim strPathToSearch As String
strPathToSearch = &quot;C:\&quot;
Call ShellExecute(Me.hwnd, &quot;find&quot;, strPathToSearch, vbNullString, vbNullString, SW_SHOWNORMAL)
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top