Split name and extension of a FolderItem
From REALbasicWiki
| Overall article skill | ✭ |
If you need to to extract the name (without extension) and the extensions from a file name, here are some functions to help with this.
- To use these functions, you can call them like this:
f.NameWithoutExtensionandf.Extension. To check if a file has an extension, simply test if the string returned fromf.Extensionis non-empty. To add an extension to a file name, e.g. ".txt", do this:f.Name = f.NameWithoutExtension + ".txt"
Add each of these functions to a module, not to a class (this is necessary because of the "extends" keyword):
Function Extension(extends f as FolderItem) As string
// Returns the extension, always including the period (e.g ".app")
// If the returned string is empty, it means that the file name has no extension
// If the returned string equals ".", it means that the file name ended in a period
dim components() as string
dim ext as string
components = Split( f.Name, "." )
if Ubound( components ) <= 0 then
// There is no extension
return ""
end if
if Ubound( components ) = 1 and LenB( components( 0 ) ) = 0 then
// The file name begins with a period and has no extension
return ""
end if
// There is an extension, return it with the "." to indicate this
return "." + components.Pop
End Function
Function NameWithoutExtension(extends f as FolderItem) As string
// Complements the Extension() function
dim components() as string
dim name as String
name = f.Name
components = Split( name, "." )
if Ubound( components ) = 0 then
// There is no extension
return name
end if
if Ubound( components ) = 1 and LenB( components( 0 ) ) = 0 then
// The file name begins with a period and has no extension
return name
end if
// If we get here, it means that the file has an extension, whether or not it begins with a period
// Remove the last component (i.e. the extension)
call components.Pop
// Re-join the name components without the last one
return Join( components, "." )
End Function
Note: this code is always returning a reasonable value and should raise no exceptions
Some notes about error checking (something that needs always be considered in reusable code):
- As we implemented the functions using extends, there is need to check f against Nil — instead, the caller will have to deal with it.
- Due to the way the Split function works, there is always at least one component, even if the file name does not contain any period. Hence,
components(0)always exists.
[edit] An Alternative Implementation
Here is an alternative implementation of the code to extract the file extension.
Function IndexOfExtensionSeparator(s as String, separator as String = ".") As Integer
dim theIndex as Integer = Len(s) + 1
for i as Integer = Len(s) downTo 1
if Mid(s, i, 1) = separator then
theIndex = i
exit
end if
next
return theIndex
End Function
When no extension separator is found, IndexOfExtensionSeparator returns a position past the end of the string, instead of 0. That this better agrees with the definitions of extension and separator is confirmed by the absence of special-case code to handle the no-extension case in the code below.
Function Extension(extends f as FolderItem) As String
return Mid(f.Name, IndexOfExtensionSeparator(f.Name) + 1)
End Function
Function NameWithoutExtension(extends f as FolderItem) As String
return Left(f.Name, IndexOfExtensionSeparator(f.Name) - 1)
End Function
