finding just docname in File search

G

Guest

I'm using a file search and I want to be able to seperate a docname from a path
name in the found files? Is there a way to do this without looking for
backslashes from right to left? I want to be able to change the names of actual
docs, and cant if I don't know the docname without the path name of
subdirectory name.


(e-mail address removed)
 
J

Jonathan West

Try this function

Function FileNameOnly(FullPathName As String) As String
FilenameOnly = WordBasic.FileNameInfo$(FullPathName, 3)
End Function
 
T

Tim Ferguson

(e-mail address removed) (Spammastergrand) wrote in
I'm using a file search and I want to be able to seperate a docname
from a path name in the found files?

' dirS returns the file name without the path
strFileName = Dir$(strPathAndFile)

' what's left is the path
strPath = _
Left$(strPathAndFile, Len(strPathAndFile) - Len(strFileName))

Hope that helps


Tim F
 
G

Guest

Thanks. I'll try that. But I'm not sure I understand. I'm not using a Dir to
loop through a directory, I'm using a filesearch.

' dirS returns the file name without the path
strFileName = Dir$(strPathAndFile)

' what's left is the path
strPath = _
Left$(strPathAndFile, Len(strPathAndFile) - Len(strFileName))
(e-mail address removed)
 
G

Guest

Try this function
A basic function. Intersting. I'll have to try that. Thanks.

Function FileNameOnly(FullPathName As String) As String
FilenameOnly = WordBasic.FileNameInfo$(FullPathName, 3)
End Function
 
T

Tim Ferguson

(e-mail address removed) (Spammastergrand) wrote in
But I'm not sure I understand. I'm not using a Dir to
loop through a directory, I'm using a filesearch.

Dir() just looks to see if a file is there: you don't have to use wildcards
with it! The only problem with this is that it will fail if the file
doesn't exist. And it's not very good if the drive (or floppy or CD, etc)
is not readable. But as long as you know the file is really there, it's
fine.

Hope that helps

Tim F
 
G

Guest

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)
 

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