A file's directory

  • Thread starter User At Domain Dot Invalid
  • Start date
U

User At Domain Dot Invalid

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?
 
J

Jürgen Exner

User said:
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
 
P

Paul Lalli

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
 
T

Tore Aursand

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 <[email protected]>
"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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top