Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Help me get "Directory" out of full windows path name?

Reply
Thread Tools

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

 
 
Random Task
Guest
Posts: n/a
 
      02-21-2006
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;
}
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      02-21-2006
Random Task <(E-Mail Removed)> wrote in news:1SHKf.1285$XZ3.142971
@news20.bellglobal.com:

> 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...le/Basename.pm

See also splitpath and splitdir in

http://search.cpan.org/~kwilliams/Pa...b/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
 
Reply With Quote
 
 
 
 
jl_post@hotmail.com
Guest
Posts: n/a
 
      02-21-2006
Random Task wrote:
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get Full Path of current directory ? (ASP.net) PHaroZ ASP .Net 5 08-17-2012 08:39 AM
os.path.walk() to get full path of all files dude Python 5 03-17-2011 09:09 AM
Get Current Full App Path? xeroxero ASP .Net 1 12-08-2006 05:56 AM
Re: XP Pro (Full) , X64 (Full) and Windows Server 2003 R2 TRIAL - which is latest ntldr ? - Bobb - Windows 64bit 9 12-14-2005 04:59 PM
Simple way to get the full path of a running script? Benjamin Han Python 7 01-03-2004 07:15 PM



Advertisments