Getting filename and file extension of a path?

I was wondering if there is a built-in function to get the filename without extension and the file extension, similar to the following Delphi code:

filename:=extractfilename(filepath); fileext:=extractfileext(filepath);

so I can modify the filename part itself, like adding a number (example again in Delphi):


f.displayname returns the filename without the path
f.parent returns the path without the filename

and it should be

newPath = f.parent.child(str(number)+f.displayname)

place these methods in a public module

[code]Public Function NameWithoutExtension(extends afi as FolderItem) as String
Dim completeName As String = afi.Name
If completeName="" Then Return completeName

Dim i As Integer = CountFieldsB(completeName,".")
Dim ext As String = NthFieldB( completeName, “.”, i)

’ completeName like “.xxxxxx”
If i>1 And InStrB(completeName, “.”) = 1 Then Return completeName

If i>1 Then Return LeftB(completeName, LenB(completeName)-LenB(ext)-1)

’ completeName without extension at all
Return completeName

End Function

[code]Public Function ExtensionName(extends afi as FolderItem) as String
Dim ch As String = afi.Name
Dim i As Integer = CountFieldsB(ch,".")
Dim ext As String
If i>1 Then
ext = NthFieldB( ch, “.”, i)
ext = “”
End If

Return ext

End Function

[quote=442042:@Dave S]f.displayname returns the filename without the path
it depends on the user prefs. if the user asks to display all name with extension, then it does.

I thought it’d be a fun quick thought experiment, so here’s my version that applies the number to the file directly:

Public Sub ApplyNumber(fTarget as FolderItem, iNum as Integer)
  // Sanity check
  if fTarget = nil then return
  if fTarget.Exists = false then return
  dim arsFilenameParts() as String = Split(fTarget.Name, ".")
  dim sFilename as String
  dim sExt as String
  // If we have enough parts, get the extension
  if arsFilenameParts.Ubound > 0 then
    sExt = "." + arsFilenameParts.Pop
  // Rebuild the rest of the filename
  sFilename = Join(arsFilenameParts, ".")
  // Generate the new filename
  dim sNewName as String = sFilename + "_" + str(iNum) + sExt
  // Apply it
  fTarget.Name = sNewName
End Sub

It returns whatever the user would see in the Finder / Explorer which may include the extension
If you set a file to NOT hide the extension the display name includes the extension

If by this you mean the path of the enclosing entity (dir, volume, etc)

A 21 years experience with REALbasic / Real Studio / Xojo and this is news to me !

Thanks Norman.