Title: RE: HOWTO: break up a directory path name into components ...

All,

I have a subroutine "splitFilename" that I wrote that does this for me.  It's been enhanced over time to include support for UNC, but this is the meat of that subroutine that handles directory/path/filename strings:

########
local($_) = $filename;
($file_d, $file_p_tl, $file_p, $file_f, $file_e) = m#^[\\/][\\/]#
    ? m#^([\\/][\\/][^\\/]+[\\/]) (([\\/][^\\/]+)*[\\/]) (.*?) ((\.[^.]+)*)$#x
    : m#^([a-z]:) (([\\/][^\\/]+)*[\\/]) (.*?) ((\.[^.]+)*)$#ix;

$file_e =~ s/^\.//;
__END__

The vars of interest are as follows:
$file_d    -- drive letter (with colon)
$file_p    -- path info (no trailing slash)
$file_p_tl -- path info (trailing slash)
$file_f    -- filename (not including dot or extension)
$file_e    -- extension

As I was testing these 5 lines, I realized the $file_f and $file_e doesn't work for a filename name with more then one period (.) in it!  But I will leave as an exercise for the reader!  :)

-Aaron

-----Original Message-----
From: Lawrence Lordanich (x11486) [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, May 14, 2002 9:17 PM
To: perl win32 users
Subject: HOWTO: break up a directory path name into components ...


If I have a directory: c:\component1\component2\component3
how do I get at component[i]? I know I have seen this somewhere in my
reading but cannot find it now that I want it. Want a portable answer
too.

Thanks,

Lawrence.


------------------------------------------------------------------------
------------------------------
Lawrence Lordanich                   Tel:   +1 858-651-1486
Qualcomm Inc. (E-140F)             Fax:   +1 858-845-8222
4875 Eastgate Mall                   Email:   [EMAIL PROTECTED]
San Diego, CA 92121-1978  TimeZone:  GMT-8 (PST)
------------------------------------------------------------------------
------------------------------

Reply via email to