STrip path of a file

  • Thread starter Thread starter SF
  • Start date Start date
S

SF

Hi,

I have file eg G:\MYPICTURES\123456.JPG

What is the function to strip the PATH (eg G:\MYPICTURES\) out of the enture
string?

SF
 
If the file exists, you can do this:

'~~~~~~~~~~~~~
dim mPath as string, mFile as string
dim mSpec as string

mSpec = "G:\MYPICTURES\123456.JPG"

mFile = DIR(mSpec)

mPath = left(mSpec, _
len(mSpec)-len(mfile))
'~~~~~~~~~~~~~


if you do not know if the file exists...

'~~~~~~~~~~~~~
Function GetPath( _
pSpec as string) _
as String

dim mPath as string, i as integer

i = len(mSpec)

for i = len(mSpec) to 1 step -1
if mid(pSpec,i,1) = "\") then
GetPath = _
left(pSpec, i)
exit function
end if
next i

End Function

'~~~~~~~~~~~~~


Warm Regards,
Crystal
Microsoft Access MVP 2006

*
Have an awesome day ;)

remote programming and training
strive4peace2006 at yahoo.com

*
 
SF said:
Hi,

I have file eg G:\MYPICTURES\123456.JPG

What is the function to strip the PATH (eg G:\MYPICTURES\) out of the
enture string?

If the file exists, you can use this simple function:

'----- start of code -----
Function PathOnly(FullFilePath As String) As String

PathOnly = _
Left(FullFilePath, _
Len(FullFilePath) - Len(Dir(FullFilePath)))

End Function

'----- end of code -----

If the file doesn't exist, you need to do a bit more work:

'----- start of code #2 -----
Function PathOnly2(FullFilePath As String) As String

'Use this version if the file may not exist.

Dim I As Long

I = InStrRev(FullFilePath, "\")

If I = 0 Then
I = InStrRev(FullFilePath, ":")
End If

If I = 0 Then
PathOnly2 = FullFilePath
Else
PathOnly2 = Left(FullFilePath, I)
End If

End Function
'----- end of code #2 -----
 
thanks, Steve ... forgot about InStrRev ... old habits die hard!


Warm Regards,
Crystal
Microsoft Access MVP 2006

*
Have an awesome day ;)

remote programming and training
strive4peace2006 at yahoo.com

*
 
Well, looks like I may have misinterpreted your question. I assumed
that after a "strip", the interest is more on what's left rather than on
what's removed ;-). In any case, even with my interpretation, a simple
Dir([YourField]) would have sufficed. Oh well...
 
Hi Steve,

you're not the only one ;) ... now that you mention it, the
question could be interpreted 2 ways...


Warm Regards,
Crystal
Microsoft Access MVP 2006

*
Have an awesome day ;)

remote programming and training
strive4peace2006 at yahoo.com

*
 
SF wrote in message said:
Hi,

I have file eg G:\MYPICTURES\123456.JPG

What is the function to strip the PATH (eg G:\MYPICTURES\) out of the
enture string?

SF

For the fun of it, having a bit of flexibility:

Dim strDrive As String
Dim strDir As String
Dim strFile As String
Dim strExt As String

Const cstrFullName As String = _
"G:\MYPICTURES\123456.JPG"

With WizHook
.Key = 51488399
.SplitPath cstrFullName, strDrive, strDir, strFile, strExt
Debug.Print strDrive, strDir, strFile, strExt
End With
 

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

Back
Top