From: a on
Hi,
I have an input string. It is a full path of a file.
eg. //server/dirA/dirB/file.txt
How can I parse it into directory and file?
i.e //server/dirA/dirB/ and file.txt

Also, I have a directory as an input.
e.g //server/dirA/dirB/
How can I check the last character is a / or not?
Thanks a lot


From: John Bokma on
"a" <a(a)mail.com> wrote:

> Hi,
> I have an input string. It is a full path of a file.
> eg. //server/dirA/dirB/file.txt
> How can I parse it into directory and file?
> i.e //server/dirA/dirB/ and file.txt

File::Spec

> Also, I have a directory as an input.
> e.g //server/dirA/dirB/
> How can I check the last character is a / or not?

Why? Also, might that File::Spec doesn't make this test necessary.

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
I ploink googlegroups.com :-)

From: usenet on
a wrote:
> I have an input string. It is a full path of a file.
> How can I parse it into directory and file?

Hmm. Someone else just asked this same question in a different group...
the answer is the same; let File::Basename (a built-in Perl module) do
it for you:

#!/usr/bin/perl
use strict; use warnings;

use File::Basename; # Standard module included in Perl

my $filename = '//server/dirA/dirB/file.txt';

my ($name, $path) = fileparse($filename);
print "$path\n$name\n";

__END__

From: James Taylor on
a <a(a)mail.com> wrote:

> I have an input string. It is a full path of a file.
> eg. //server/dirA/dirB/file.txt
> How can I parse it into directory and file?
> i.e //server/dirA/dirB/ and file.txt

If you don't want to use a separate module for such a simple task and
you know there will be at least one slash directory separator, then a
simple regex should be good enough.

my ($dir, $leaf) = $path =~ m|^(.*)/(.*)|;

If you can't be sure of getting at least one slash then you could prefix
one to the $path first, or just check for this situation and assign the
rest to the $leaf.

> Also, I have a directory as an input.
> e.g //server/dirA/dirB/
> How can I check the last character is a / or not?

Simple:

if ( $path =~ m|/$| ) {
# has trailing slash
} else {
# no trailing slash
}

--
James Taylor
From: Josef Moellers on
a wrote:
> Hi,
> I have an input string. It is a full path of a file.
> eg. //server/dirA/dirB/file.txt
> How can I parse it into directory and file?
> i.e //server/dirA/dirB/ and file.txt
>
> Also, I have a directory as an input.
> e.g //server/dirA/dirB/
> How can I check the last character is a / or not?

You seem to be very ignorant of anything other people write.
In your thread "array question", you have been asked to show code that
you wrote and that dowsn't work. You have been asked to read the posting
guidelines for this group.

Yet you post another question showing that you do not want to help us
help you.

You're steering straight into a lot of people's killfiles.
--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett