Parsing Filenames

This one is something that's required so frequently, it might be helpfull if I present my standard subroutine for it...

# ==================================================
# Gets a specified component from a given file name.
# --------------------------------------------------
# $_[0] Component to extract: 'PATH', 'FILE' or 'EXT'
# $_[1] Full file name to extract from
# --------------------------------------------------
# Returns the base filename
# Examples...
#    GetPathComponent "FILE", "\anypath\Test1.scr"
#        returns "Test1"
#    GetPathComponent "PATH", "\anypath\Test1.scr"
#        returns "\anypath\"
#    GetPathComponent "EXT", "\anypath\Test1.scr"
#        returns ".scr"
# --------------------------------------------------
# $C_LOCAL_OS must be set to use with DOS filenames.
# $C_EXTENSION_PATTERN must be set to a valid
# pattern for the extension before calling this
# routine. A typical value is \\..*? which will
# work for both DOS and Unix.
# ==================================================
sub GetPathComponent
{
   my $P_COMPONENT = $_[0];
   my $P_FULL_NAME = $_[1];
   my $P_OS_NAME = $_[2];
   my $V_BASE_NAME;
   my $V_PATH_NAME;
   my $V_FILE_EXT;

   # -----------------------------------------------------
   # DOS uses the escape character (\) as a path seperator
   # which defeats the fileparse function, so convert any
   # escape character to a forward slash first...
   # -----------------------------------------------------
   if( $C_LOCAL_OS eq "DOS" ) { $TESTVAL =~ s/\\/\//g; }

   # -----------------------------------------
   # The fileparse sub does the actual work...
   # -----------------------------------------
   ($V_BASE_NAME, $V_PATH_NAME, $V_FILE_EXT)
        = fileparse($P_FULL_NAME, $C_EXTENSION_PATTERN);

   TraceScript 3, "GetPathComponent",
                  "Base name is [" . $V_BASE_NAME . "]";
   TraceScript 3, "GetPathComponent",
                  "Path name is [" . $V_PATH_NAME . "]";
   TraceScript 3, "GetPathComponent",
                  "Extension is [" . $V_FILE_EXT . "]";

   # -----------------------------------------------------
   # Return the requested component (or die if invalid)...
   # -----------------------------------------------------
   if($P_COMPONENT eq "PATH") { return($V_PATH_NAME); }
   elsif($P_COMPONENT eq "FILE") { return($V_BASE_NAME); }
   elsif($P_COMPONENT eq "EXT") { return($V_FILE_EXT); }
   else {die "Invalid component ["
             . $P_COMPONENT
             . "] specified in GetPathComponent" }
}

This piece of code should be sufficiently self explanatory in itself. The important thing to remember is that Perl can return complete arrays from subroutines and that's precisely what fileparse() does. You give it the filename and a regular expression that defines the filename extension and fileparse returns a three element array containing the base name, the path name and the extention.

Again, Perl allows you to split the elements into seperate variables so that's what we do here. The rest of this subroutine is simply to make the whole thing easier to use.

Find out more by searching Google here...


Google