Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Search through subfolders

Given a parent folder, I need to be able to search through subfolders
looking for files with the extension ".mxd" and write the name of those
files to an excel spreadsheet.

Can anyone point me in the direction of any parts of this problem being
done elsewhere? (or tell me how its done?)

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 644
Default Search through subfolders

I came up with this using Application.FileSearch
Sub FindFiles()
Dim i As Long
With Application.FileSearch
.SearchSubFolders = True
.LookIn = "C:\"
.Filename = "*.txt"
.FileType = msoFileTypeAllFiles
.Execute
For i = 1 To .FoundFiles.Count
Range("A" & i) = .FoundFiles(i)
Next
End With
End Sub

I also found this on Ron De Bruin's site:
http://www.rondebruin.nl/copy33.htm

Charles

JustinP wrote:
Given a parent folder, I need to be able to search through subfolders
looking for files with the extension ".mxd" and write the name of those
files to an excel spreadsheet.

Can anyone point me in the direction of any parts of this problem being
done elsewhere? (or tell me how its done?)


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Search through subfolders

Using the Scripting Runtime FileSystemObject is my preferred method...
It is included with Windows versions released after Windows 95.Microsoft Windows Script 5.6 Documentation...
http://msdn.microsoft.com/library/de...ist/webdev.asp

The free Excel add-in "List Files" does what you want.
Download from ... http://www.realezsites.com/bus/primitivesoftware
No registration required.
--
Jim Cone
San Francisco, USA


"JustinP"
wrote in message
Given a parent folder, I need to be able to search through subfolders
looking for files with the extension ".mxd" and write the name of those
files to an excel spreadsheet.

Can anyone point me in the direction of any parts of this problem being
done elsewhere? (or tell me how its done?)

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Search through subfolders

This was posted by Jim Thomlinson in the past. (there have been many others
of similar ilk or using DIR)

Option Explicit
Option Compare Text


Sub test()
Call ListFiles("C:\Management", Sheet1.Range("A2"), "dbq", True)
End Sub


Public Sub ListFiles(ByVal strPath As String, _
ByVal rngDestination As Range, Optional ByVal strFileType As String = "*",
_
Optional ByVal blnSubDirectories As Boolean = False)
Dim objFSO As Scripting.FileSystemObject
Dim objFolder As Scripting.Folder
Dim objFile As Scripting.File
Dim strName As String


'Specify the file to look for...
strName = "*." & strFileType
Set objFSO = New Scripting.FileSystemObject
Set objFolder = objFSO.GetFolder(strPath)


For Each objFile In objFolder.Files
If objFile.Name Like strName Then
rngDestination.Value = objFile.Path
rngDestination.Offset(0, 1).Value = objFile.DateLastAccessed
Set rngDestination = rngDestination.Offset(1, 0)
End If
Next 'objFile
Set objFile = Nothing


'Call recursive function
If blnSubDirectories = True Then _
DoTheSubFolders objFolder.SubFolders, rngDestination, strName


Set objFSO = Nothing
Set objFolder = Nothing
End Sub


Function DoTheSubFolders(ByRef objFolders As Scripting.Folders, _
ByRef rng As Range, ByRef strTitle As String)
Dim scrFolder As Scripting.Folder
Dim scrFile As Scripting.File
Dim lngCnt As Long


On Error GoTo ErrorHandler
For Each scrFolder In objFolders
For Each scrFile In scrFolder.Files
If scrFile.Name Like strTitle Then
rng.Value = scrFile.Path
rng.Offset(0, 1).Value = scrFile.DateLastAccessed
Set rng = rng.Offset(1, 0)
End If
Next 'scrFile


'If there are more sub folders then go back and run function again.
If scrFolder.SubFolders.Count 0 Then
DoTheSubFolders scrFolder.SubFolders, rng, strTitle
End If
ErrorHandler:
Next 'scrFolder


Set scrFile = Nothing
Set scrFolder = Nothing
End Function




--

Regards,

Tom Ogilvy



"JustinP" wrote in message
ups.com...
Given a parent folder, I need to be able to search through subfolders
looking for files with the extension ".mxd" and write the name of those
files to an excel spreadsheet.

Can anyone point me in the direction of any parts of this problem being
done elsewhere? (or tell me how its done?)



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Search through subfolders

Thanks for your help guys. It is much appreciated.


Tom Ogilvy wrote:
This was posted by Jim Thomlinson in the past. (there have been many others
of similar ilk or using DIR)

Option Explicit
Option Compare Text


Sub test()
Call ListFiles("C:\Management", Sheet1.Range("A2"), "dbq", True)
End Sub


Public Sub ListFiles(ByVal strPath As String, _
ByVal rngDestination As Range, Optional ByVal strFileType As String = "*",
_
Optional ByVal blnSubDirectories As Boolean = False)
Dim objFSO As Scripting.FileSystemObject
Dim objFolder As Scripting.Folder
Dim objFile As Scripting.File
Dim strName As String


'Specify the file to look for...
strName = "*." & strFileType
Set objFSO = New Scripting.FileSystemObject
Set objFolder = objFSO.GetFolder(strPath)


For Each objFile In objFolder.Files
If objFile.Name Like strName Then
rngDestination.Value = objFile.Path
rngDestination.Offset(0, 1).Value = objFile.DateLastAccessed
Set rngDestination = rngDestination.Offset(1, 0)
End If
Next 'objFile
Set objFile = Nothing


'Call recursive function
If blnSubDirectories = True Then _
DoTheSubFolders objFolder.SubFolders, rngDestination, strName


Set objFSO = Nothing
Set objFolder = Nothing
End Sub


Function DoTheSubFolders(ByRef objFolders As Scripting.Folders, _
ByRef rng As Range, ByRef strTitle As String)
Dim scrFolder As Scripting.Folder
Dim scrFile As Scripting.File
Dim lngCnt As Long


On Error GoTo ErrorHandler
For Each scrFolder In objFolders
For Each scrFile In scrFolder.Files
If scrFile.Name Like strTitle Then
rng.Value = scrFile.Path
rng.Offset(0, 1).Value = scrFile.DateLastAccessed
Set rng = rng.Offset(1, 0)
End If
Next 'scrFile


'If there are more sub folders then go back and run function again.
If scrFolder.SubFolders.Count 0 Then
DoTheSubFolders scrFolder.SubFolders, rng, strTitle
End If
ErrorHandler:
Next 'scrFolder


Set scrFile = Nothing
Set scrFolder = Nothing
End Function




--

Regards,

Tom Ogilvy



"JustinP" wrote in message
ups.com...
Given a parent folder, I need to be able to search through subfolders
looking for files with the extension ".mxd" and write the name of those
files to an excel spreadsheet.

Can anyone point me in the direction of any parts of this problem being
done elsewhere? (or tell me how its done?)




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
SubFolders in macro pianoman[_4_] Excel Programming 2 April 28th 06 03:25 PM
Auto look through subfolders grewpp Charts and Charting in Excel 1 February 14th 06 02:35 PM
copy subfolders, replace text in files and save files in copied subfolders pieros Excel Programming 0 November 1st 05 12:08 PM
file search in subfolders Pflugs Excel Programming 2 September 16th 05 05:00 AM
Get list of subfolders Darren Hill[_3_] Excel Programming 3 March 6th 05 09:28 PM


All times are GMT +1. The time now is 08:54 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"