Best way to find a file with a certain extension? - Solved

Hello all,

What is the best way to search a folder for a file with a certain extension? In this case “.dat” or “.dta”? Also is Windows, Linux or Mac case sensitive to file names, or extension? .DAT/.dat??

Thank you,

Use the Name property: FolderItem.Name.Right(3) = "DAT"

Window and OS X are not case-sensitive by default, but I think you can install a custom file system that is. Linux is always case-sensitive.

But remember string comparisons in Xojo are case-insensitive unless you use the StrComp function.

Cool! Thank you Paul!

OS X may be case sensitive depending on whether you set up a case sensitive file system or not

Does OS X have an API for finding files in a directory using wildcarded file names?

In Xojo on Windows, I do that (get .ext files from a directory) using code from the Windows functionality suit that uses the declares FindFirstFile and FindNextFile* and it is very fast.

That can be very handy when you need to process a bunch of folders/files produced from another app/instrument that can be mixed with other files in a large directory.

On OS X I have to loop through all the files and check the names individually to make sure they have teh right extension before processing.

If there is a fast API to do wildcard file name retrieval on OS X , i would be nice to have a an X-Platform Xojo API for it!

I’d almost expect that a fast or bulk method was already in MacOSLib

I know carbon has some and I think Thomas at one time posted declares for them (I could be wrong about this)

For OS X/Linux, I would expect shell.execute(“ls …”) would be the fastest and easiest approach. You get a nice list of file names that you can GetFolderItem on.

you might want to include the “.” as well

FolderItem.Name.Right(4) = ".DAT"

otherwise “myfile.xdat” would return true as well

And make sure the name is more than 4 characters too to avoid a file that might be named “.dat”.

but .dat matches .dat :stuck_out_tongue:

Thanks again everyone. The file name(s) are all more than 4 characters. Lots of food for thought here, thanks again everyone!

Yes Norman. “.dat” does match “.dat” if you are looking at FOUR characters…

but if you are looking at only the last THREE (as in your example) then “.DAT” matches “.XDAT”

and in OSX and LINUX a file named “.DAT” would be invisible :slight_smile:

Can anyone elaborate on the correct code to get a list of all files with a certain extension in a folder? I cannot seem to get any of my attempts to work! I tried to get Pauls suggestion to work, it will not compile FolderItem.Name.Right(3) = “DAT” or other similar to this.
This one displays only the folder name “history”…

  Dim f As FolderItem = GetFolderItem(GatePath).Child("History")   //.Name(4).Right(".dta")
  Dim FileNames As String =f.Name.Right(4)  //(".dta",4)

All help appreciated!
Thank you,

[quote=35127:@Dave S]Yes Norman. “.dat” does match “.dat” if you are looking at FOUR characters…

but if you are looking at only the last THREE (as in your example) then “.DAT” matches “.XDAT”
Right - you can’t look at just the last 3 because long extensions would mess you up
Either see if it ends with “.dat” OR the extension (after the last dot) is DAT and you’re looking for “DAT”
Functionally the same

[quote=35127:@Dave S]
and in OSX and LINUX a file named “.DAT” would be invisible :)[/quote]
Invisible to the end user but not your application

I found code like this in the old RS forum. It worked for me as shown.
Thanks again for all of your responses!

  Dim FileNames() As String
  Dim f As FolderItem
  Dim filetype() As String
  Dim i As Integer
  f = GetFolderItem(GatePath).Child("History")
  dim fileList() as FolderItem
  dim folderCount as integer = f.Count
  for i = 1 to folderCount
    fileList.Append f.Item( i )
  for i = 0 to fileList.Ubound
    dim thisFile as FolderItem = fileList( i )
    dim thisName as string = thisFile.Name
    dim thisExt as string = thisName.Right(4)   
    if thisExt = ".dta" Then
      FileNames.Append thisName
    end if

Just to know the informatic vocabulary:
For example, on Mac, we have file “.DS_Store”. Is it a file with no name and an extension “.DS_Store”. Or “.DS_Store” is it the name and the file has no extension?
I know how to manage that depending of what I want to do in my code. I’m just asking the technical name of those files.

That’s the name of the file with no extension. Also, extensions don’t have spaces (I’m not sure if they are no allowed or just never do), so you can use NthField and CountFields to get the last segment of the name after the dot, make sure it’s not the entire name, then make sure it has no spaces. Something like this:

Function FolderItemExtension (f as FolderItem) As String
  dim name as string = f.Name.Mid( 2 )  // Lop off the first character
  dim cnt as integer = name.CountFields( "." )
  if cnt < 2 then return "" // No extension
  dim ext as string = name.NthField( ".", cnt )
  if ext.InStr( " " ) <> 0 then ext = ""
  return ext
End Function

I did this from memory, so forgive any bugs.

I do

[code]Dim TampTabNom_f(-1), Elt_ext as String
Dim NbEltTabNom_f as Int16

TampTabNom_f = Split(CetElt.Name, “.”)
NbEltTabNom_f = UBound(TampTabNom_f)

If (NbEltTabNom_f = 0) or ((NbEltTabNom_f = 1) and (TampTabNom_f(0) = “”)) Then ’ (NbEltTabNom_f = 1) and (Left(CetElt.Name, 1) = “.”)
Elt_ext = “”
Elt_ext = ("." + TampTabNom_f(NbEltTabNom_f))
End If

Return Elt_ext
Then if the name is for example “.Abc.def” I return “.def” like you do it seems. If the name is “Abc.” you return “” and I return “.” it seems, then an empty extension.
I return the dot “.” in the extension as I prefer like that in my programs. And I always read extension “.doc” or “.html”, but not “doc” or “html”. But it seems that, in informatics vocabulary, the extension is without the dot. Applescript return the extension without the dot “.”.

And I never write many Return in my Method like you do. I always have a Variable that I return. And the Return is always the last line code of my Method. I do like that for 90 years, and I will not change now .


In your Mehod, Dave S, “.DS_Store” return extension “DS_Store”.