Oct 3rd, 2002, 04:14 AM
#1
Thread Starter
Hyperactive Member
Search a directory for a file including sub direcotries
How do I search a directory for a file and have it search SUB dir's also? i.e searching the Directory C:\ and being able to find my file.txt which is a few levels down.
C:\
C:\new folder 1\
C:\new folder 1\new folder 2\
C:\new folder 1\new folder 2\my file.txt
-------------------------------------------------
My code - doesn't work sadly....
Dim objFSO As New FileSystemObject
Dim objFile As File
Dim objFolder As Folder
Dim objsubfolder As Folders
Set objFolder = objFSO.GetFolder(DirList.List(DirList.ListIndex))
' Loop through each sub directory.
For Each objsubfolder In objFolder.SubFolders
' Loop through each file in the sub directory.
For Each objFile In objFolder.Files
Debug.Print objFile.Name
Next objFile
Next objsubfolder
----------------------------------
Any ideas? Thanks!
- Gabe
Oct 3rd, 2002, 04:21 AM
#2
-= B u g S l a y e r =-
sample for you
VB Code:
Option Explicit
Private Function FoldersWhereFileExists(ByVal sFolder As String, sFileName As String) As String
Dim fso As New FileSystemObject
Dim f As Folder
Dim fldr As Folder
Dim fil As File
Dim sFoldersContainingFiles As String
'set start folder
Set fldr = fso.GetFolder(sFolder)
For Each fil In fldr.Files
If UCase(fil.Name) = UCase(sFileName) Then
sFoldersContainingFiles = sFoldersContainingFiles & fldr.Path & vbCrLf
Exit For
End If
Next fil
FoldersWhereFileExists = sFoldersContainingFiles
'in order to retrieve all the subs in the subs in the subs.... uh... _
'we have to call 'ourselfs' until there are no more subfolders left...
If fldr.SubFolders.Count > 0 Then
For Each f In fldr.SubFolders
FoldersWhereFileExists = FoldersWhereFileExists & FoldersWhereFileExists(f.Path, sFileName)
Next f
End If
End Function
Private Sub Command1_Click()
MsgBox FoldersWhereFileExists("C:\new folder 1\", "file.txt")
End Sub
Oct 3rd, 2002, 04:30 AM
#3
Hyperactive Member
Here is one using API
VB Code:
Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Public Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * 250
cAlternate As String * 14
End Type
Public Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Public Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Public Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Public Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long
Public Sub copyRoot(strSource As String, Optional strDest As String)
Dim lngCntr As Long, lngHandle As Long, lngNextFile As Long
Dim WFDFind As WIN32_FIND_DATA, SECAT As SECURITY_ATTRIBUTES
Dim strFileName As String
lngNextFile = 1
lngHandle = FindFirstFile(strSource & "\*.*", WFDFind)
While (lngNextFile)
strFileName = stripNull(WFDFind.cFileName)
If Not (strFileName = "." Or strFileName = "..") Then
If (GetFileAttributes(strSource & "\" & strFileName) And FILE_ATTRIBUTE_DIRECTORY) Then
'File attribute is DIRECTORY
'Create destination directory
lngTemp = CreateDirectory(strDest & "\" & strFileName, SECAT)
'Check here for desired folder name
Call copyRoot(strSource & "\" & strFileName, strDest & "\" & strFileName)
Else
'Check here for desired filename
lngTemp = CopyFile(strSource & "\" & strFileName, strDest & "\" & strFileName, 0)
DoEvents
End If
End If
lngNextFile = FindNextFile(lngHandle, WFDFind)
Wend
lngHandle = FindClose(lngHandle)
End Sub
Actually I made it for copying recursively. You can modify it for searching file.
Oct 16th, 2002, 02:50 PM
#4
Addicted Member
But this doesn't searches the sub directory's
Oct 16th, 2002, 03:04 PM
#5
Frenzied Member
There is a project that i did just for fun. It's a file search engine.
Look in the code you'll find that you need
Attached Files
Sep 19th, 2003, 03:59 AM
#6
New Member
I am trying to use the example that peet posted but I get "Compile Error: User-defined type not available." for all the variables at the top.
Previously I have used:
Dim FileSystemObject As Object
Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
for the FileSystemObject but don't know what to do for the Folder & File objects.
I am really new to this so any help would be appreciated.
Thanks.
Sep 19th, 2003, 06:26 AM
#7
-= B u g S l a y e r =-
Hi Angela,
in order to use the FSO in early binding mode, you have to add a
reference to the 'Microsoft Scripting Runtime' (scrrun.dll)
In vb, select from the menu : Project -> Add references...
In the reference list, browse down until you find the 'Microsoft
Scripting Runtime' (scrrun.dll)
Make sure it is selected.
Close the dialog.
Sep 19th, 2003, 06:33 AM
#8
New Member
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width