Displaying file's name in a TextFiled

Hi, everyone,

I have the following code in the PushButton Action event


Dim dialog As OpenDialog
Dim file As FolderItem

dialog = New OpenDialog

dialog.Filter = "text/plain"

file = dialog.ShowModal

If file <> Nil Then
TextField1.Text = file.Name
Else
TextField1.Text = "User clicked Cancel."
End If

I have several questions:

  1. Why does the dialog show all files and folder while dialog.Filter is set equal to “text/plain?”

  2. How to remove file’s extension from the TextField1.Text?

Thank you in advance,

Val

What platform?

Hi, Tim,

Ideally, it should be universal.

If there is no universal solution, I would need all of them( Windows, Mac, and Linux.)

Thank you in advance,

Val

You need a File Type Set in your project for the OpenDialog.Filter to work properly.

You can do what smart Xojo developers do and invest in the Monkey Bread Software plugins (MBS) :smiley: and use f.NameWithoutExtensionMBS or you can write a method to read the name from the end backwards dropping everything from the end to and including the first period encountered.

[code]Public Function NameWithoutExtension(extends afi as FolderItem) as String
dim ch as String = afi.Name
dim i as Integer = CountFieldsB(ch,".")
dim ext as String = NthFieldB( ch, “.”, i)

Return LeftB(ch, LenB(ch)-LenB(ext)-1)

End Function
[/code]

Note a very important part of when Jean-Yves posted - the use of the “B” variants for NthField, CountField, Left, and Len. They really speed things up if you are dealing with lots of strings.

Here’s how I strip off the extension from a folderitem:

With f as the folderitem…

Dim fn() As String fn = Split(f.NativePath, ".") fn.Remove(fn.Ubound) return Join(fn,".")

In my experience, it doesn’t

That’s because project File Type Sets and filters are broken. The engineers involved with the feedback case insist everyone is just doing it wrong. The most reliable fix is to define the FileType object in code directly before using it.

dim ftFilter as new FileType
ftFilter.Name = "PDF"
ftFilter.Extensions = ".pdf"

dim dlgOpen as new OpenDialog
dlgOpen.Filter = ftFilter

call dlgOpen.ShowModal

You both seem to assume that the FileName actually contains a Dot…
Guess what happens with the code above and these filenames:

  • MyFileWithoutExtension
  • .myappprefs

Not thought-through… but how about these additional checks?

[code]Dim fn As String = afi.Name

Dim parts() As String = SplitB(fn, “.”)
if (InStrB(fn, “.”) > 1) or ((parts.Ubound = 1) and (parts(0) <> “”)) or (parts.Ubound > 1) then
parts.Remove(parts.Ubound)
fn = Join(parts, “.”)
end if

return fn[/code]
I know… that still doesn’t account for Filenames such as ..test (even though one can argue what the Filename/Extension is in this case) - so there is still room for improvements :wink:

Oh, and I guess @Kem Tekinay would do that with a RegEx :slight_smile:

and rely on a dot to determine the file extension.

Nota: a file name that starts with a dot is an invisible file…

No one can create fool 100% proof code. Imagine someone building file names with a two dots file extension (against the rules)…

In your car, you run when the light is green; imagine someone who runs its car whle the light is red…

That is why following the rules is important.