Neil,
 
This is a little sub that I rolled up to provide the path, filename, and extension:
 
sub Fparse($)
{
    local $_ = shift;
    my ($Delim, $Fp, $Fn, $Fe, $Pos1, $Pos2);
 
    # Check whether the filename contains forward or backward slashes.
    $Delim = (index($_,'/') > -1) ? '/' : '\\';
 
    # Find the last slash in the filename.
    $Pos1 = rindex($_,$Delim);
 
    # Extract the path if present.
    if ($Pos1) {$Pos1++; $Fp = substr($_,0,$Pos1)} else {$Fp = undef; $Pos1 = 0}
 
    # Find the last period in the filename.
    $Pos2 = rindex($_,'.');
 
    # Extract the name and extension.
    if ($Pos2) {$Fn = substr($_,$Pos1,$Pos2 - $Pos1); $Pos2++; $Fe = substr($_,$Pos2)}
    else {$Fn = substr($_,$Pos1); $Fe = undef}
 
    return(($Fp,$Fn,$Fe));
}
=item Fparse($filename)
 
=item
 
=over 4
 
This routine accepts a string as an argument that will contain a
filename and will parse the filename into the path, name, and file
extension. The parsed elements will be returned as an lvalue. An undef
will be returned for elements that do not exist in the passed filename, i.e.
path and/or file extension.
You could select just the path like this, assuming $file contains the fully qualified filename:
 
($path, undef, undef) = Fparse($file);

Dirk Bremer - Systems Programmer II - ESS/AMS  - NISC St. Peters
636-922-9158 ext. 8652 fax 636-447-4471
 
----- Original Message -----
From: Neil
Sent: Wednesday, December 04, 2002 10:48
Subject: perl-win32-users - Best way to return a path to a file ?

Hi,
 
I have just started to code up a subroutine to return the path only from a complete path to a file. This must the the 3rd time in two months as I have mislaid the routine on my HD and searching for it would probably take longer than the coding.
 
When writing this routine, I always seem to take the long way round and this got my curiosity going and wondering what other solutions people have come up with that are more elegant and obvious than my own.
 
I find many times that looking at someone elses code can provide an "aha" moment that last far beyong the snippet involved.
 
If anyone cares to share their code for this - that would be cool. If not - thats cool too. I am gonna head back to the editor and throw it together - again ;-) and I'll post it later so we can flame it in a public display of humiliation ;-)
 
Neil

Reply via email to