PDA

View Full Version : VBA functionality for String manipulation



volabos
10-14-2012, 03:41 AM
I am given a complete path of a file like "c:/folder1/subfolder-2/.../subfolder-n/xyz.xls". Now from this string, I need to extract only the file name i.e. 'xyz.xls'.

Is there any VBA function to accomplish that?

Thanks for your help.

GarysStudent
10-14-2012, 05:57 AM
Parse the string with SPLIT():


Sub dural()
Dim s As String
s = "C:\bigfolder\mediumfolder\smallfolder\tinyfolder\microfolder\temp.xls"
ary = Split(s, "\")
MsgBox ary(UBound(ary))
End Sub

shrivallabha
10-14-2012, 06:29 AM
One more:

Public Sub GetFileName()
Dim strPath As String, strFile As String
strPath = "c:/folder1/subfolder-2/.../subfolder-n/xyz.xls"
strFile = Mid(strPath, InStrRev(strPath, "/") + 1, Len(strPath))
MsgBox strFile
End Sub

snb
10-14-2012, 02:41 PM
You must be kidding:



msgbox dir("c:/folder1/subfolder-2/.../subfolder-n/xyz.xls")


Your first lesson/chapter in VBA manipulating files/folders.

See also:

http://www.snb-vba.eu/VBA_Bestanden_en.html