Its working. The code is till messy thoght because I have to comgine two
proceduers to make it work, my old DIR code and rhe new code. Still probably
has some extra variables to clean up. A have a textbox for pathname, find and
replace texboxes, and a checkbox for subdirectories. I basically made it to
change all my .jpegs to .jpgs:
Sub FNR()
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
F1 = Me.Find1.Text 'find textbox
F2 = "*" & Me.Find1.Text & "*" 'search criteria
R1 = Me.Replace1.Text 'replace textbox
Dim PathLength As Integer, FullLength As Integer
Name1 = MyPath & Me.Find1.Text
Name2 = MyPath & Me.Replace1.Text
PathLength = Len(MyPath)
Dim FNO As String, Pname As String
Set fs = Application.FileSearch
With fs
.LookIn = MyPath
If Me.CheckBox1.Value = True Then
.SearchSubFolders = True
Else
SearchSubFolders = False
End If
.FileName = Me.Find1.Text
If .Execute() > 0 Then
MsgBox "There were " & .FoundFiles.Count & _
" file(s) found."
For i = 1 To .FoundFiles.Count
FullLength = Len(.FoundFiles(i))
FN = Mid(.FoundFiles(i), PathLength, FullLength - PathLength + 1)
FN = .FoundFiles(i)
FNO = Dir$(FN)
Pname = Left$(FN, Len(FN) - Len(FNO)) 'path
Tstart = InStr(1, FNO, F1, 1)
If FNO Like (F2) Then
If Left(FNO, Len(F1)) = F1 Then 'see if anything before criteria
T1 = ""
Else:
T1 = Left(FNO, (Tstart - 1))
End If 'any letter to left of criteria
T2 = Right(FNO, Len(FNO) - (Len(F1) + Len(T1)))
Name1 = Pname & FNO
Name2 = Pname & T1 & R1 & T2
''''''''''''
Name Name1 As Name2 'rename file
'''''''''''
End If
Next i
Else
MsgBox "There were no files found."
End If
End With
MsgBox "Done"
End Sub
(e-mail address removed)