Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > A file's directory

Reply
Thread Tools

A file's directory

 
 
User At Domain Dot Invalid
Guest
Posts: n/a
 
      04-02-2004
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?

 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      04-02-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?


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

jue


 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      04-02-2004
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
 
Reply With Quote
 
Tore Aursand
Guest
Posts: n/a
 
      04-03-2004
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 <(E-Mail Removed)>
"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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
Session is lost if a directory is created under virtual directory Jack Wright ASP .Net 1 04-19-2005 03:04 PM
Directory and files names in temporary asp.net directory =?Utf-8?B?QmFzIEhlbmRyaWtz?= ASP .Net 5 01-31-2005 01:56 PM
list all css files in a directory and sub directory TJS ASP .Net 1 06-23-2004 10:49 AM
question regarding overriding of web.config in the root directory..in a web app in a virtual directory dotnetprogram ASP .Net 1 12-27-2003 06:02 AM



Advertisments