Link to home
Start Free TrialLog in
Avatar of ICowan
ICowan

asked on

Filename and File path

Can anybody help?

If I have obtained the filename from the commondialog control using userform1.commondialog1.filename, how can I split this down in to the file path and file name, ie C:\my documents\ for path and test.txt for file name.

Many thanks in advance, I realise that this is probably quite simple, but there seems to be no simple control.  There is a filetitle option but no filepath option.

Iain Cowan
ASKER CERTIFIED SOLUTION
Avatar of seanpage
seanpage

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of seanpage
seanpage

I forgot the closing parentheses on the Left() statement. :-)

That line should read:

sPath = Left(cDialog.FileName, Len(cDialog.FileName) - Len(sFile))

Sean

You can use the InStrRev function to find the last "\", then Left and Mid functions to return the bits you want.

slash = InstrRev(userform1.commondialog1.filename, "\")
filepath = Left(userform1.commondialog1.filename, slash)
filename = Mid(userform1.commondialog1.filename, slash+1)
seanpage, does FileTitle sometimes exclude a file extension?
Avatar of Richie_Simonetti
Private Sub Form_Load()

Dim sPath As String
Dim sFullname() As String
Dim sFile As String
Dim i As Integer

With CDiag1
    .ShowOpen
    sFullname = Split(.FileName, "\", , vbTextCompare)
    sFile = UBound(sFullname)
    For i = 0 To UBound(sFullname) - 1
        sPath = sPath & sFullname(i) & "\"
    Next i
End With
End Sub
Avatar of ICowan

ASKER

That solved the problem in a nice short piece of code, thanks very much.

Iain
burtday -- to my knowledge, filetitle will always show the complete filename.

One caveat to it -- if you SET the commondialog filename, then try to use filetitle, it won't work.  It actually has to be a file that has been selected in the commondialog window.

eg; commondialog.filename = "c:\autoexec.bat" and then using filetitle will report "".

Sean