Stripping File Names (1 Viewer)

Oldsoftboss

AWF VIP
Local time
Tomorrow, 05:32
Joined
Oct 28, 2001
Messages
2,499
I am at a bit of a loss with the left/right functions to strip down a file name as follows:

I have a list of autocad reference files located at

E:\Equipment Drawing Database\FANTECH FANS\AE404S.dwg
E:\Equipment Drawing Database\FANTECH FANS\AD314D.dwg
etc. These file paths are saved in a table.

When I access them from a networked computer the path is:

M:\Equipment Drawing Database\FANTECH FANS\AE404S.dwg
\\DADSCOMPUTER\Back Up\Equipment Drawing Database\FANTECH FANS\AE404S.dwg

What I am trying to achive is only saving the last bit of the file name ie:

\FANTECH FANS\AE404S.dwg and then combine it with the first bit with
Left(CurrentDb.Name, Len(CurrentDb.Name) - Len(Dir(CurrentDb.Name)))
The trouble I am having is stripping the first bit of the file name when trying to save the last bit to the table.

Hope this makes sense to someone and they can help.

Dave
 

DCrake

Remembered
Local time
Today, 20:32
Joined
Jun 8, 2005
Messages
8,632
If you know that all drawing are in a sub folder names \FANTECH FANS

Then you can use the InStrRev(SearchString,"\") to get the position of the last forward slash in the string

Dim dwg As String


dwg = Mid(SearchString,InStrRev(SearchString,"\")+1)


Then concac CurrentProject.Path & "\FANTECH FANS\" & dwg

David
 

Oldsoftboss

AWF VIP
Local time
Tomorrow, 05:32
Joined
Oct 28, 2001
Messages
2,499
Ah, thanks for the reply, but not all the stuff is in the FANTECH FANS folder. I also have folders such as Mitsubishi, Daikin etc with other drawings in them, thus why I was trying to isolate say the folder name, then the drawing name. I will be sure not to have any sub folders, so I am trying to find a way to remove the file path up to the second last "/" if that makes sense.

Dave
 

DCrake

Remembered
Local time
Today, 20:32
Joined
Jun 8, 2005
Messages
8,632
You could use the split() function to create a dynamic array of the path using the / as a delimter

Code:
Function Test(AnyString As String) As String


    Dim arr() As String
    Dim intHigh As Integer

 
    arr = Split(AnyString, "\")
    'get number of items in the array
    intHigh = UBound(arr)

   Dim Sfile As String
    dwg = arr(intHigh)  ' File name
    SubFolder = arr(intHigh - 1) ' Subfolder
    
'Child Folder and filename

Test = SubFolder & "\" & dwg
End Function

To use this pass the full path and file name to the function as a string such as:

Str = Test("C:\Root\Sub1\Sub2\Sub3\File.txt")

Would return

Sub3\File.txt

David
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 20:32
Joined
Sep 12, 2006
Messages
15,640
oldsoftboss

this function works in A97 (and later). After A97 there was a instrrev, to search from the end, which makes it easier

I still have this forward search in my standard library.

Note that thepath and thefile are byref arguments (which is standard), but they are modfied in this sub. I don't usually midfiy any arguments in my subs.

note that the string for thepath includes the terminating \ character

Code:
Sub splitpath(infile As String, thepath As String, thefile As String)
Dim pos As Long

    pos = 1
    While InStr(pos, infile, "\") > 1
        pos = InStr(pos, infile, "\") + 1
    Wend
    thepath = Left(infile, pos - 1)
    thefile = Mid(infile, pos)

End Sub
 

John.Woody

Registered User.
Local time
Today, 20:32
Joined
Sep 10, 2001
Messages
354
I use this to check for the filename

Public Function FindChar92(ByVal xChar As String)
Dim i As Integer

For i = 1 To Len(xChar)

If Mid(xChar, Len(xChar) - i, 1) = Chr(92) Then

Exit Function
End If

Next i

End Function

so would this work

Public Function FindChar92(ByVal xChar As String)
Dim i As Integer
Dim x as Integer

x = 0
For i = 1 To Len(xChar)

If Mid(xChar, Len(xChar) - i, 1) = Chr(92) Then
If x = 1 then
Exit Function
else
x = x + 1
end if

End If

Next i

End Function

Not tested but I think it should work
 

darbid

Registered User.
Local time
Today, 21:32
Joined
Jun 26, 2008
Messages
1,428
I thought I would add another method as well which is not mine but I found. It will give you the file name.

Code:
Function GetFilenameFromPath(ByVal strPath As String) As String
' Returns the rightmost characters of a string upto but not including the rightmost '\'
' e.g. 'c:\winnt\win.ini' returns 'win.ini'
    
    If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then
        GetFilenameFromPath = GetFilenameFromPath(Left$(strPath, Len(strPath) - 1)) + Right$(strPath, 1)
    End If
End Function
 

Users who are viewing this thread

Top Bottom