Help me get "Directory" out of full windows path name?

R

Random Task

Hi i am trying to get $tmpdir to contain the "directory" instead of a
fully pathed file name ... i.e. truncate the file name.

I am trying the below code ... but $1 is always empty ... help please :)

my $tmpdir = $file;
if ( $tmpdir =~/([.]*)[\\]{1}/)
{
print("MATCH\n");
$tmpdir = $1;
print ("[--->]".$1."<----]\n");
}
else
{
print("NO MATCH\n");
$tmpdir = $tmpdir;
}
 
A

A. Sinan Unur

Hi i am trying to get $tmpdir to contain the "directory" instead of a
fully pathed file name ... i.e. truncate the file name.

I am trying the below code ... but $1 is always empty ... help please
:)

Why are you reinventing the wheel?

See

http://search.cpan.org/~nwclark/perl-5.8.8/lib/File/Basename.pm

See also splitpath and splitdir in

http://search.cpan.org/~kwilliams/PathTools-3.16/lib/File/Spec.pm

There is immense value to doing this kind of thing portably.
my $tmpdir = $file;
if ( $tmpdir =~/([.]*)[\\]{1}/)

What are you fascinated with square brackets

Do you know what [.]* means? Dot is not special in a character class. So
[.]* means "match a dot zero or more times" before matching a character
class containing a slash exactly one time. Of course, you need not
specify a quantifier if you only want something to match exactly once.

The regex automatically matches nothing followed by a slash in the path
name, and nothing is captured as you specified.

You need to read perldoc perlre.

You also need to anchor the regex to the end of the path:

#!/usr/bin/perl

use strict;
use warnings;

chomp(my @paths = <DATA>);

for my $p ( @paths ) {
if ( $p =~ m{ \A ( .+ (?:/|\\) ) }x ) {
print "$1\n";
}
}

__DATA__
C:\
C:\Home\asu1\Dload\gvim.exe
C:\Home\asu1\src\usenet\.test
But\Please\use\File\Spec\.

Sinan
 
J

jl_post

Random said:
Hi i am trying to get $tmpdir to contain the "directory" instead of a
fully pathed file name ... i.e. truncate the file name.

For that task, I'd recommend using the dirname() function from the
File::Basename module. It's standard, so you should aready have it.
Read about it by typing "perldoc File::Basename" at the command prompt.

Here's a quick code sample that does exactly what you want:


#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;

print "Type a filename: ";
my $file = <STDIN>;

# This next line is the line that does what you want:
my $tmpdir = dirname($file);

print "The directory is: $tmpdir\n";

__END__


By using the File::Basename module, you remove any need for regular
expressions and any dependence on code that hasn't already been tested
by many people in the Perl community. The code that does what you want
in the sample program above is only one line long, so why write our own
(longer) code that may initially suffer from several bugs?
I am trying the below code ... but $1 is always empty ... help please :)

my $tmpdir = $file;
if ( $tmpdir =~/([.]*)[\\]{1}/)
{
print("MATCH\n");
$tmpdir = $1;
print ("[--->]".$1."<----]\n");
}
else
{
print("NO MATCH\n");
$tmpdir = $tmpdir;
}

Uh... what is the purpose of this line, exactly?:
$tmpdir = $tmpdir;

Do you know what this line even does? If so, why did you put it in?

To be honest, it seems like you are fairly new to Perl programming.
I would recommend purchasing (or checking-out from your local library)
a book named "Learning Perl" (published by O'Reilly). It makes the
assumption that the reader does not know any Perl programming, and
teaches the beginner Perl programmer many programming concepts that all
decent Perl programmers are expected to know (like regular expressions,
which you seem to want to use but don't know how to use correctly).

Personally, I don't think any programmer has become a "real Perl
programmer" until he/she is familiar with at least 90% of that book.
And I'm guessing that you never read that book, otherwise you wouldn't
have made some of the regular expression mistakes that you did.

Don't feel bad if you've never read the "Learning Perl" book. Every
one of us, at some point in our lives, had not read that book. Pick it
up, read it, work through the examples, and you'll find that you can
read and write Perl code much easier and better than ever before.

I hope this helps.

-- Jean-Luc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top