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


"Steven Revell" wrote in message
...
Hi all,

I'm looking for a way to loop through all the directories
on a server and run find files (without subdirectory
searching).

I would like it to run on Excel 97 but Excel 2000 will be
fine.

So my questions is does anyone know how to run through all
the subdirectories within a directory. And, if those
directories have sub directories then go through them as
well.

thanks for any help,

Steven


You need to write a recursive function that starts out at some
point does a dir call and lists the files required and calls itself when
it encounters a subdir

Below is an example in VB

Function GetRecursiveFileList(Startpoint As String, FileExtension As String,
filelist() As String, FileCount As Long) As Long
' ----------------------------------------
' Routine scans filesystem recursively for
' files with specified extension
' ----------------------------------------
Dim file_name As String
Dim FIndent As Integer, FIndex As Integer
Dim File_Path As String, StrtPath As String
Dim File_Read As Integer
Dim x As Boolean, xTemp As Integer, S$
Dim i As Integer, ierr As Long
On Error Resume Next
FIndent = FIndent + 1
If Right(Startpoint, 1) < "\" Then
File_Path = Startpoint & "\"
Else
File_Path = Startpoint
End If

file_name = Dir$(File_Path, vbDirectory + vbReadOnly + vbArchive)
File_Read = 1
Dim FileNameToAdd As String
Dim IsDirectory As Boolean
x = False
Const CustomTabs = vbTab
File_Read = 1

Do While file_name < ""
If file_name < "." And file_name < ".." Then
IsDirectory = (GetAttr(File_Path & file_name) = vbDirectory) Or
(GetAttr(File_Path & file_name) = vbDirectory + vbArchive) _
Or (GetAttr(File_Path & file_name) = vbDirectory + vbReadOnly) _
Or (GetAttr(File_Path & file_name) = vbDirectory + vbReadOnly +
vbArchive)

FileNameToAdd = ""


'Check for file types


If IsDirectory = False Then
If UCase(Right(file_name, Len(file_name) - InStr(file_name,
"."))) = UCase(FileExtension) Then
FileNameToAdd = File_Path & file_name
End If

If FileNameToAdd < "" Then
filelist(FileCount) = FileNameToAdd
FileCount = FileCount + 1
End If

Else
If IsDirectory Then 'Perform recursive
'MsgBox "Searching directory :" & File_Path & file_name
StrtPath = File_Path & file_name
ierr = GetRecursiveFileList(StrtPath, FileExtension,
filelist(), FileCount)
End If
End If
x = True
' FIndex = FIndex + 1
End If

If x Then
file_name = Dir$(File_Path, vbDirectory + vbArchive +
vbReadOnly)
For i = 2 To File_Read
file_name = Dir$
Next i
x = False
End If
file_name = Dir$

If file_name = "" Then
file_name = Dir$(, vbReadOnly)
End If
File_Read = File_Read + 1

Loop
FIndent = FIndent - 1



End Function


Keith


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
Looping Maggie[_6_] Excel Discussion (Misc queries) 6 October 2nd 08 09:14 PM
Not Looping Roger Excel Discussion (Misc queries) 0 February 26th 08 06:18 PM
Looping David T Excel Discussion (Misc queries) 2 August 30th 06 10:51 PM
Looping through each directory Tom Ogilvy Excel Programming 1 July 11th 03 09:39 AM
Looping through each directory Jim Rech Excel Programming 0 July 10th 03 06:36 PM


All times are GMT +1. The time now is 06:12 PM.

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

About Us

"It's about Microsoft Excel"