Looping through subdirectories

G

Guest

I have a form that replaces filenames with others. Find1 and Replace1 are
textboxes which let you specify what to find and replace. A browse button lets
you choose the dir to do it on. Anyone know how to make it search
subdirectories? I want to replace all the .jpegs with .jpg and want to do my
entire C drive instead of searching all the individual folders. Some programs
still can't see .jpegs so I want to rename them.

Dim FN As String, MyPath As String
Dim name1 As String, name2 As String 'oldname newname
Dim F1 As String, F2 As String, R1 As String 'Find and replace vars
Dim T1 As String, T2 As String
Dim Tstart As Integer, Tend As Integer
'this is a button on a form with three textboxes
'for path, find, and replace criteria
MyPath = Me.TextBox1.Text 'path text box
FN = Dir(MyPath & "*.*")
F1 = Me.Find1.Text 'find textbox
F2 = "*" & Me.Find1.Text & "*" 'search criteria
R1 = Me.Replace1.Text 'replace textbox

Do Until FN = ""

Tstart = InStr(1, FN, F1, 1)
If FN Like (F2) Then

If Left(FN, Len(F1)) = F1 Then 'see if anything before criteria
T1 = ""
Else:

T1 = Left(FN, (Tstart - 1))

End If
T2 = Right(FN, Len(FN) - (Len(F1) + Len(T1)))
name1 = MyPath & FN
name2 = MyPath & T1 & R1 & T2
Name name1 As name2 'rename file
End If

FN = Dir
Loop
MsgBox "Done"

End Sub

(e-mail address removed)
 
G

Guest

Okay, I found some subdir code. I guess I can loop through a dir and pass the
sub dir to a procedure using the procedure as a function to work on subdirs.
But how do I do subdirs of the subdir? Its like a Chinese puzzle. Some of those
dirs might be may levels deep:

Sub ListSubDirs()
'
' ListSubDirs Macro
' Macro recorded 2/8/2004 by Jim Stiene
' Display the names in C:\ that represent directories.
MyPath = "c:\" ' Set the path.
myname = Dir(MyPath, vbDirectory) ' Retrieve the first entry.
Do While myname <> "" ' Start the loop.
' Ignore the current directory and the encompassing directory.
If myname <> "." And myname <> ".." Then
' Use bitwise comparison to make sure MyName is a directory.
If (GetAttr(MyPath & myname) And vbDirectory) = vbDirectory Then

' Display entry only if it
MsgBox myname

End If ' it represents a directory.
End If
myname = Dir ' Get next entry.
Loop

End Sub
(e-mail address removed)
 
G

Gary Barnes

Hi,

Write a recursive sub routine. Basically the first thing that the sub does
is checks to see if there are any sub folders. If it finds a sub folder then
it calls itself with the name (or whatever) of the sub folder. The second
thing the sub does is to process the files in the current sub folder. i.e.

public sub main
processSubFolder("c:\")
end sub

private sub processSubFolder (subFolderName as string)
for each subfolder in this subfolder
processSubFolder (name of the sub folder)
next

for each .jpeg
rename the .jpeg file
next
end sub

The sub routine will keep calling itself until it hits the lowest level
where it has no sub folders to process so it processes the .jpeg files and
then exits at which point it returns (as any sub routine would) to the line
after it was called, however in this case that just happens to be in itself.
All the local variables and byVal (the default I think) variables passed
into the routine are stored on the stack so when the routine calls itself it
is actually dealing with a different set of variables the same as it would
be if it was called by any other routine.

It is an excellent programming trick and ideal for solving this sort of
repeative problem but very easy to shoot your self in the foot (a lot of
times REALLY fast).

Good luck

Gary
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top