Parsing filename and path 
Author Message
 Parsing filename and path

Hi,

I have used theGetOpenFilename method to get a filename. This returns the
full path and filename of the file to one variable. Is there an easy way to
parse the path and filename and assign them to two different variables?

Do I have to search for a "/" string and find its position? if so how do I
get the position of the last / and not the first?

Any assistance will be appreciated.

Thank you.

Sean Bartleet



Sat, 04 Jun 2005 11:35:29 GMT  
 Parsing filename and path
as an example:

Sub Tester5()
Dim fname As Variant, sname As String
' fname = Application.GetOpenFileName()
fname = "C:\folder1\folder2\filename.xls"
if fname <> False then
  sname = fname
  Do While InStr(sname, "\") > 0
    sname = Right(sname, Len(sname) - InStr(sname, "\"))
  Loop
  MsgBox fname & " - " & sname
End If
End Sub

Regards,
Tom Ogilvy


Quote:
> Hi,

> I have used theGetOpenFilename method to get a filename. This returns the
> full path and filename of the file to one variable. Is there an easy way
to
> parse the path and filename and assign them to two different variables?

> Do I have to search for a "/" string and find its position? if so how do I
> get the position of the last / and not the first?

> Any assistance will be appreciated.

> Thank you.

> Sean Bartleet



Sat, 04 Jun 2005 12:54:12 GMT  
 Parsing filename and path
Coincidentally, there's a thread that's discussing this right now:

http://groups.google.com/groups?threadm=mGtL9.8522%24s_.1546%40news02...
(one line in your browser)

Quote:

> Hi,

> I have used theGetOpenFilename method to get a filename. This returns the
> full path and filename of the file to one variable. Is there an easy way to
> parse the path and filename and assign them to two different variables?

> Do I have to search for a "/" string and find its position? if so how do I
> get the position of the last / and not the first?

> Any assistance will be appreciated.

> Thank you.

> Sean Bartleet

--

Dave Peterson



Sat, 04 Jun 2005 12:54:58 GMT  
 Parsing filename and path

Quote:

> Hi,

> I have used theGetOpenFilename method to get a filename. This returns the
> full path and filename of the file to one variable. Is there an easy way to
> parse the path and filename and assign them to two different variables?

> Do I have to search for a "/" string and find its position? if so how do I
> get the position of the last / and not the first?

> Any assistance will be appreciated.

> Thank you.

> Sean Bartleet

Might want to try this:

' Splits a file name and path into its seperate parts
Public Sub FileNameSplit(ByVal pstrFile As String, pstrPathPart As
String, pstrFilePart As String)

    pstrFile = Trim(pstrFile)

    pstrFilePart = FileNamePart(pstrFile)

    If Len(pstrFilePart) > 0 Then
        pstrPathPart = Left(pstrFile, Len(pstrFile) -
Len(pstrFilePart))
    Else
        pstrPathPart = pstrFile
    End If

    If Right(pstrPathPart, 1) = "\" Then pstrPathPart =
Left(pstrPathPart, Len(pstrPathPart) - 1)

End Sub

Regards

Graham

--------------
Progressive Data Solutions
http://www.pdsolutions.com.au
Home of VB Code Cutter - VB/VBA Code Library, Dev Tool with Free code
formatting/indenting



Sat, 04 Jun 2005 17:12:39 GMT  
 Parsing filename and path

Quote:

> Hi,

> I have used theGetOpenFilename method to get a filename. This returns the
> full path and filename of the file to one variable. Is there an easy way to
> parse the path and filename and assign them to two different variables?

> Do I have to search for a "/" string and find its position? if so how do I
> get the position of the last / and not the first?

If you have the XL 2002 you can use InstrRev(). This function wasn't
in XL 97, not sure about XL 2000. Alternatively you can use Split() to
get the file name:

sPathname = "C:\My Folder\Subfolder\file.dat"
aPaths = Split(sPathname, "\")
sFilename = aPaths(UBound(aPaths))



Sat, 04 Jun 2005 18:31:54 GMT  
 Parsing filename and path
Hi Sean,
FileName=Application.Workbooks(1).Name 'returns filename, can be variable or
text "myfile.xls"

Application.Workbooks(FileName).Path 'returns the full path without the
filename.

Application.ActiveWorkbook.FullName 'returns full path and filename

--
John
johnf202 at hotmail.com


Quote:
> Hi,

> I have used theGetOpenFilename method to get a filename. This returns the
> full path and filename of the file to one variable. Is there an easy way
to
> parse the path and filename and assign them to two different variables?

> Do I have to search for a "/" string and find its position? if so how do I
> get the position of the last / and not the first?

> Any assistance will be appreciated.

> Thank you.

> Sean Bartleet



Sat, 04 Jun 2005 23:15:07 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. filename or path+filename in powerpoint-slides/print-outs

2. Find File Path and Insert Path and FileName into Text Boxes

3. Parsing complete file path for use in Indirect function - plz help

4. Need path to DB - use CurrentDB and Parse?

5. Parsing a file reference from a path

6. parsing MapiFolder paths?

7. extract filename from a path

8. Filename and Path in footer

9. How to get the filename, not the whole path

10. How to Get the Filename and Path...


 
Powered by phpBB® Forum Software © phpBB Group