A file's directory

Discussion in 'Perl Misc' started by User At Domain Dot Invalid, Apr 2, 2004.

  1. Given a file name (possibl a path to it given as well), is there a way
    to get or extract the name of the directory the file is in?
     
    User At Domain Dot Invalid, Apr 2, 2004
    #1
    1. Advertising

  2. User At Domain Dot Invalid wrote:
    > Given a file name (possibl a path to it given as well), is there a way
    > to get or extract the name of the directory the file is in?


    Sure, just do a File::Find starting at /.
    Remember, there may be many files with the same name.

    jue
     
    Jürgen Exner, Apr 2, 2004
    #2
    1. Advertising

  3. User At Domain Dot Invalid

    Paul Lalli Guest

    On Fri, 2 Apr 2004, User At Domain Dot Invalid wrote:

    > Given a file name (possibl a path to it given as well), is there a way
    > to get or extract the name of the directory the file is in?



    Assuming you're looking for the directory part of the path (so that a pure
    filename would return "." as the directory):


    #!/usr/bin/perl
    use strict;
    use warnings;
    use File::Basename;

    my $dir = dirname($ARGV[0]);

    print "Directory of $ARGV[0] is $dir\n";

    __END__



    Paul Lalli
     
    Paul Lalli, Apr 2, 2004
    #3
  4. User At Domain Dot Invalid

    Tore Aursand Guest

    On Fri, 02 Apr 2004 14:19:50 +0200, User At Domain Dot Invalid wrote:
    > Given a file name (possibl a path to it given as well), is there a way
    > to get or extract the name of the directory the file is in?


    If you know the location of the file (ie. the path), you can use
    File::Basename to extract only the directory part.

    If you don't know where the file is (ie. you don't know that path), you
    need to search for it. Take a look at File::Find and File::Find::Rule.


    --
    Tore Aursand <>
    "Anyone who slaps a 'this page is best viewed with Browser X'-label on
    a web page appears to be yearning for the bad old days, before the
    web, when you had very little chance of reading a document written on
    another computer, another word processor or another network." -- Tim
    Berners-Lee, July 1996
     
    Tore Aursand, Apr 3, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TJS
    Replies:
    1
    Views:
    519
    Eliyahu Goldin
    Jun 23, 2004
  2. Jack Wright
    Replies:
    1
    Views:
    474
    sonikchopra
    Apr 19, 2005
  3. =?Utf-8?B?QmFzIEhlbmRyaWtz?=

    Directory and files names in temporary asp.net directory

    =?Utf-8?B?QmFzIEhlbmRyaWtz?=, Jan 28, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    843
    Juan T. Llibre
    Jan 31, 2005
  4. Steve Singer
    Replies:
    0
    Views:
    546
    Steve Singer
    Feb 15, 2005
  5. davidw
    Replies:
    2
    Views:
    554
    Brock Allen
    May 30, 2005
Loading...

Share This Page