ICowan
asked on
Filename and File path
Can anybody help?
If I have obtained the filename from the commondialog control using userform1.commondialog1.fi lename, 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
If I have obtained the filename from the commondialog control using userform1.commondialog1.fi
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can use the InStrRev function to find the last "\", then Left and Mid functions to return the bits you want.
slash = InstrRev(userform1.commond ialog1.fil ename, "\")
filepath = Left(userform1.commondialo g1.filenam e, slash)
filename = Mid(userform1.commondialog 1.filename , slash+1)
slash = InstrRev(userform1.commond
filepath = Left(userform1.commondialo
filename = Mid(userform1.commondialog
seanpage, does FileTitle sometimes exclude a file extension?
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
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
ASKER
That solved the problem in a nice short piece of code, thanks very much.
Iain
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
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
That line should read:
sPath = Left(cDialog.FileName, Len(cDialog.FileName) - Len(sFile))
Sean