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

That’s insufficient Dave. If the filename has no extension, i.e., “MyFileName”, it would return the entire name as if it were the extension.

Also, as an alternate, this regular expression pattern would do it too:

"(?mi-Us)(?<!^)\\.[^.\\s]+$"

They can and sometimes do

Do you have an example?

One I just created :stuck_out_tongue:
I just renamed a pdf to have “some thing other than pdf” as the extension (using either the finder or mv in the terminal)
There’s no restriction by the OS that they cannot have spaces.
It’s uncommon - but not restricted

I just give a try in Snow Leopard. I get info for a .rbl file and I added a space in the extension (".r bl") and the Finder told me "are you sure to remove the extension “.rbl”. Another try, if I added a letter, a “t” for example, then the Finder say "are you sure to change the extension “.rbl” by “.rtbl”.

And for my comment above, I note that the Finder write the dot “.” in the extension name.

Edit : I didin’t try but I’m nearly sure that we can, with AppleScript, put a space in the Extension. And I suppose with Xojo too as Norman did it.

I understand it’s not restricted by the OS, but as a practical matter, I don’t think it’s ever done and would be confusing if it were.

Also, despite the OS, I’m pretty sure the Mac Finder does not consider a string with spaces to be an extension. I have to test this again when I get back.

Being visible ? being legal
Spotlight sure treats it as the extension.
Changed the extension - searched in Spotlight for files with the extension I created (that had a space in it) and there it is.
Finder isn’t the final arbiter at any rate - the file system is and there the extension is valid as it’s just another part of the entire name which can have spaces.
As you know the Finder is just the user UI for the file system & there are many things that are legal in the file system (like names that start with a . that are hidden from view in the Finder.
Even in a terminal window they are normally hidden from being shown via ls, but ls can show them if you use it the right way.

But Xojo is. :smiley: (I’m kidding)

Again, I’m not disputing what the OS considers to be a legal extension, I’m saying, as a practical matter, you’re safe to ignore strings with spaces in them. I still can’t think of a real-world example where that is untrue.

Consider the case of a file called “My 9.29.99 letter”, the kind you might find from the OS 9 days (and I still have plenty of those files lying around). In fact, that file name has no extension, but the OS would consider “13 letter” as the extension. I think this is a case of what’s right vs. what’s legal.

[quote=37316:@Kem Tekinay]Again, I’m not disputing what the OS considers to be a legal extension, I’m saying, as a practical matter, you’re safe to ignore strings with spaces in them. I still can’t think of a real-world example where that is untrue.

Consider the case of a file called “My 9.29.99 letter”, the kind you might find from the OS 9 days (and I still have plenty of those files lying around). In fact, that file name has no extension, but the OS would consider “13 letter” as the extension. I think this is a case of what’s right vs. what’s legal.[/quote]

I’m curious how you come to conclude that file has no extension ?
If the OS considers “99 letter” as the extension on that file that seems to me “the right thing”

That one of my first program on Mac OS X, add the extension of my files from Mac OS 9.

I did a google search, and I read somewhere that the extension can be separate of the file name by a dot “.” or by a space " ".

I’ve personally never encountered a file system that used space as the delimiter for the extension.
The runs the gamut of VMS, Multics, DOS, Windows (from 3.0 on up), a small bit of OS/2, some CP/M, various flavors of Unix & Mac OS from OS 6 on up.
The period has been pretty universal in my experience.

As a side note but actually on topic:

I currently launch multiple shells with “find” commands when I need to find the whole hierarchy below a given folder. I’ve found that console “find” is the fastest option in all platforms compared to parsing the folderitem.itemcount (breadth-first or depth-first, no difference).

Is there a declare-y way to do it for Mac, Windows and Linux that can be at least as fast a the find command but doesn’t require me to launch shell programs?

Unix file systems don’t technically use extensions. A period in the name is just another character. By convention, we impose the concept of extension, but it isn’t baked into the filesystem the way it was with DOS/Windows 8.3 filenames.

[quote=37319:@Norman Palardy]I’m curious how you come to conclude that file has no extension ?
If the OS considers “99 letter” as the extension on that file that seems to me “the right thing”[/quote]

Yes, the machine sees it as an extension, but a human would know it’s not. The purpose of the extension is to match the file with an application, or otherwise identify its type or purpose. You and I would look at that file and know that “99 letter” does none of those things, so calling it an extension isn’t “the right thing” no matter what the OS says.

Again, as a practical matter, I think it’s safe to assume that a string following the final dot is not meant as an extension if it contains a space, even if that assumption contradicts the OS.

Yes, but will you modify your method to return “” (empty string then no extension) if there is a space in the last characters behind the dot “.” ?
I think I won’t. If one day my programs see this case, it will be condidered as an extension.

I’m on iPad and sometimes I lost my comment when I do some search in another Window. But it seems I reached this time. Just read the first line (dot or space to separate extension):
http://en.wikipedia.org/wiki/Filename_extension
But wikipedia is not THE reference neither.

No, I wouldn’t consider a lone space an extension, but rather a mistake. That Wikipedia article talks about a space as an alternate to the dot as a separator, which Norman discusses above.

Think of it this way: If you need to know the extension so you can duplicate a file, which name would you rather end up with? In the case of the example I used, I’d rather not produce “My 9.29 copy.99 letter”, but rather “My 9.29.99 letter copy”.