Perl/DOS how do i check if directories are equivalent ...

R

Random Task

Hi quick question

I have two paths say:

C:\pRoGram Files\Monkey Boy\\\\\\
C:\PRoGraM Files\Monkey BOY

Is there a quick way of telling the directories are the same?

Jim
 
S

Sherm Pendley

Random Task said:
I have two paths say:

C:\pRoGram Files\Monkey Boy\\\\\\
C:\PRoGraM Files\Monkey BOY

Is there a quick way of telling the directories are the same?

I'm not certain if it would work on Windows, or with directories, but the
first thing I'd try is doing a stat() on both paths and comparing the inodes.

sherm--
 
R

Random Task

Sherm said:
I'm not certain if it would work on Windows, or with directories, but the
first thing I'd try is doing a stat() on both paths and comparing the inodes.

sherm--

Doesn't seem to work on windows :-(

I guess i am stuck using:
- lc
- compare length
- remove trailing \'s until strlen = or no \'s
- compare when equal len and no \'s
 
R

Random Task

Sherm said:
I'm not certain if it would work on Windows, or with directories, but the
first thing I'd try is doing a stat() on both paths and comparing the inodes.

sherm--
here's what i did to get around this ... seems to work ok at first
glance :)

my $tmp = lc $dir1;
my $tmp2 = lc $dir2;


if ($tmp eq $tmp2)
{
&somemagic();
}
elsif ( length($tmp) < length($tmp2))
{
$tmp=~s/\\/\\\\/g;
$tmp2 =~ s/$tmp//g;
$tmp2 =~ s/\\//g;
if ($tmp2 eq "")
{
&somemagic();
}
}
else #if (length($tmp) > length($tmp2))
{
$tmp2=~s/\\/\\\\/g;
$tmp =~ s/$tmp2//g;
$tmp =~ s/\\//g;
if ($tmp eq "")
{
&somemagic();
}
}
 
T

Thomas Kratz

Random said:
Hi quick question

I have two paths say:

C:\pRoGram Files\Monkey Boy\\\\\\
C:\PRoGraM Files\Monkey BOY

Is there a quick way of telling the directories are the same?

Jim

Here's one way:

use strict;
use warnings;

use File::Spec::Functions;

my $p1 = 'C:\pRoGram Files\Monkey Boy\\\\\\';
my $p2 = 'C:\PRoGraM Files\Monkey BOY';

print "YEP!\n" if canonpath(uc($p1)) eq canonpath(uc($p2));


Thomas

--
$/=$,,$_=<DATA>,s,(.*),$1,see;__END__
s,^(.*\043),,mg,@_=map{[split'']}split;{#>J~.>_an~>>e~......>r~
$_=$_[$%][$"];y,<~>^,-++-,?{$/=--$|?'"':#..u.t.^.o.P.r.>ha~.e..
'%',s,(.),\$$/$1=1,,$;=$_}:/\w/?{y,_, ,,#..>s^~ht<._..._..c....
print}:y,.,,||last,,,,,,$_=$;;eval,redo}#.....>.e.r^.>l^..>k^.-
 
D

Damian James

I have two paths say:

C:\pRoGram Files\Monkey Boy\\\\\\
C:\PRoGraM Files\Monkey BOY

Is there a quick way of telling the directories are the same?

One quick and messy way I have resorted to in the past to match
directory names in a case insensitive way is to turn the path
into a glob pattern:
(untested)

lc $name;
$name =~ s/[a-z]/"[$&".uc($&)."]"/eg;
my $file = glob $name;

or somesuch.

You'd probably store the results of the glob() as hash keys,
if you wished to eliminate duplicates.

--Damian
 

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
474,266
Messages
2,571,079
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top