Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

parse file name

by Anonymous Monk
on Sep 22, 2009 at 17:23 UTC ( #796769=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am looking for a perl regex that parse filename, extension and folder name from a whole file_path.

ex : $file = '/abc/xyz/abc.txt'

I need to parse filename , folder path and extension from the $file


Replies are listed 'Best First'.
Re: parse file name
by Joost (Canon) on Sep 22, 2009 at 17:28 UTC

      I agree - Since I discovered File::Basename, i use it all the time.

      Maybe OP should learn to love Super Search?

      Just a something something...

        Ahh... Super Search sucks. I'm so sorry to say that. It does- slow as world peace. Google does it better..

        Search perlmonks for parsing a file path.. parse file path

        Works better for me- searching inside perlmonks.. :-(

      Thanks ALL!
Re: parse file name
by ccn (Vicar) on Sep 22, 2009 at 17:26 UTC
    see File::Spec
    use File::Spec; my ($volume, $directories, $file) = File::Spec->splitpath( $path ); my ($extension) = $file =~ /\.([^\.]+)$/;
      Note that the notion of a "file extension" is a rather vague concept. For example in perl-5.10.1.tar.gz I would consider the .tar.gz to be the extension.

      So if you want to DWIM, you'd have to keep a list of known file extensions that contain a dot, and special case them.

        Or you could consider it to be a recursive definition, so that .gz is the extension of the perl-5.10.1.tar, and .tar is the extension of perl-5.10.1.

        6 of one, 1/2 dozen of another.


        Please consider supporting my wife as she walks in the 2009 Alzheimer's Walk.

Re: parse file name
by leocharre (Priest) on Sep 22, 2009 at 19:57 UTC
    use File::PathInfo; my $abs = '/home/myself/thing.txt'; my $o = File::PathInfo->new($abs) or die; $o->ext; $o->abs_loc; # then dir it resides in $o->abs_path; $o->filename; $o->filename_only; # filename without ext
Re: parse file name
by sabari (Beadle) on Sep 23, 2009 at 06:34 UTC
    What about the split function ...
    Best Regards, S.Sabarinathan,
Re: parse file name
by bichonfrise74 (Vicar) on Sep 23, 2009 at 21:43 UTC
    You can simply use split to get the filename.
    #!/usr/bin/perl use strict; my $long_file = '/abc/xyz/abc.txt'; my $file = ( split( "/", $long_file ) )[-1]; print "$file\n";
      The scenario is, I need to get the filename and file path no matter it is in windows or unix type.

      it can be either of the below
      my $long_file = '/abc/xyz/abc.txt';
      my $long_file = '\abc\xyz\abc.txt';

      In this case File::Basename and File::Spec wont return the directory part if the file name is windows format and i am running the script from unix.

      Any Solution for this?
      Thanks, Tom

        Replace all backslashes in $long_file with forward slashes before working with $long_file. Using the tr/// operator should be the most efficient implementation.


        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://796769]
Approved by Joost
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2020-08-13 11:37 GMT
Find Nodes?
    Voting Booth?
    Which rocket would you take to Mars?

    Results (70 votes). Check out past polls.