in reply to Parsing a filename

If you want a single script to use File::Basename on pathname strings that come from different machines, I think you'll need to tell it how to behave for each input string, which means you have to check for backslashes (and this will probably suffice).

You probably want something like this (I've written it so that it expects you to provide one or more pathname strings as command-line args, and these may need to be quoted in some way, depending on what sort of shell you are usng):

#!/usr/bin/perl use strict; use File::Basename; for my $pathname ( @ARGV ) { my $ostype = ( $pathname =~ /\\/ ) ? "MSDOS" : "UNIX"; fileparse_set_fstype( $ostype ); warn "\n parsing $pathname using $ostype rules\n"; my ( $name, $path, $suffix ) = fileparse( $pathname, qr{\.\w+$} ); print "Name: $name\nPath: $path\n.Ext: $suffix\n"; }
Here's a sample run (using a bash shell on a unix box, so single quotes to protect the backslashes from interpretation by the shell):
$ ./test.pl 'c:\foo\bar\baz.exe' /usr/share/doc/bash/bash.pdf parsing c:\foo\bar\baz.exe using MSDOS rules Name: baz Path: c:\foo\bar\ .Ext: .exe parsing /usr/share/doc/bash/bash.pdf using UNIX rules Name: bash Path: /usr/share/doc/bash/ .Ext: .pdf

(update: In case it's not obvious, I'm not suggesting that you should use a separate command-line script from within your CGI code. The point of my script is simply to demonstrate the use of the "fileparse_set_fstype()" function in File::Basename, which is what you need in order to tell the fileparse() function how to behave for a given input. Also, I fixed a spelling error in my script's output.)

Replies are listed 'Best First'.
Re^2: Parsing a filename
by rooneyl (Sexton) on Jul 08, 2008 at 08:06 UTC
    Thank you, you have cleared things up for me.