in reply to Re: Parse out the extension of a filename - return base of filename.
in thread Parse out the extension of a filename - return base of filename.

Yes. I was aware of the availability of function from File::Basename. However, in order to use it you needed to have prior knowledge of the filename which was being passed to the module ie you needed to know how many periods are in the filename. This wouldn't work very well if you were going through a whole list of files that were not structured the same. That is one reason I wrote this lil sub because it doesn't care how many period '.' delimiters are in the filename. It will always return the full filename minus the last part of the filename which is what I have always considered to be the extension.

_ _ _ _ _ _ _ _ _ _
- Jim
Insert clever comment here...

  • Comment on Re: Re: Parse out the extension of a filename - return base of filename.

Replies are listed 'Best First'.
Knob Re: Parse out the extension of a filename - return base of filename.
by knobunc (Pilgrim) on Mar 13, 2002 at 14:43 UTC

    Umm.. try running the example he gave (or reading the node more closely). It does match the last part of the filename following the final dot. So if you run:

    perl -MFile::Basename -e 'print join ":", fileparse("foo.bar.baz", "\\ +.[^.]+"), "\n"'

    It produces foo.bar as the filename, ./ as the directory and baz as the extension. A few more filenames and results follow:

    Input filenameBase nameExtension
    foo.bar.bazfoo.barbaz
    foo.bar foo bar
    foo foo none

    -ben

      Yup. I know. Clearly I spoke before I tested and I did a terrible terrible thing and assumed his code wouldn't work. But, alas, I was wrong. :)

      _ _ _ _ _ _ _ _ _ _
      - Jim
      Insert clever comment here...