rooneyl has asked for the wisdom of the Perl Monks concerning the following question:
Hello,
I know this subject has been covered,but I don't seem to get it. I have got a cgi script that uploads a file. I am using File::Basename to split the path to extract just the file name and extension. it works fine with Unix path separators, but I can not get it to work when dealing with a windows path. It strips out the path separators, but doesn't give the filename.
If I upload a file called C:\Documents and Settings\Ryan\My Documents\sample.txt it gives me the full path minus the path separators.
The output for Unix is sample.txt.
The output for windows is C:Documents and SettingsMy Documentssample.txt.
Thank you for any help.
If I upload a file called C:\Documents and Settings\Ryan\My Documents\sample.txt it gives me the full path minus the path separators.
use File::Basename; my ($name, $path, $suffix); my $filename_with_path_unix = "/usr/local/sample.txt"; print "path is "; print $filename_with_path_unix."\n" ; # test 1, unix print "TEST #1\n"; ( $name, $path, $suffix ) = fileparse( $filename_with_path_unix, "\.[^ +.]*" ); print $name."\n"; print $path."\n"; print $suffix."\n"; print "File is ". $name.$suffix."\n"; #test 2, dos print "TEST #2\n"; my $filename_with_path_dos = "C:\Documents and Settings\My Documents\s +ample.txt"; print "path is ".$filename_with_path_dos."\n"; ( $name, $path, $suffix ) = fileparse( $filename_with_path_dos, "/.[^. +]*" ); print $name."\n"; print $path."\n"; print $suffix."\n"; print "File is ". $name.$suffix."\n";
The output for Unix is sample.txt.
The output for windows is C:Documents and SettingsMy Documentssample.txt.
Thank you for any help.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Parsing a filename
by toolic (Bishop) on Jul 07, 2008 at 17:45 UTC | |
Re: Parsing a filename
by tomfahle (Priest) on Jul 07, 2008 at 17:47 UTC | |
Re: Parsing a filename
by graff (Chancellor) on Jul 08, 2008 at 00:46 UTC | |
by rooneyl (Sexton) on Jul 08, 2008 at 08:06 UTC |
Back to
Seekers of Perl Wisdom