Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Comparing filenames in different directories

Reply
Thread Tools

Comparing filenames in different directories

 
 
Deepu
Guest
Posts: n/a
 
      03-16-2007
Hi All,

I am trying to compare file names in 2 different directories.

DIR1:

FILENAME_1.a
FILENAME_2.a
FILENAME_3.a
FILENAME_4.a

DIR2:

FILENAME_1.x
FILENAME_2.x
FILENAME_3.x
FILENAME_5.x


I need to 'diff' files with same name else if the filename (Ex :
FILENAME_4) is not present in DIR2 then display message "FILENAME_4 is
not present in DIR2" when it fails to get FILENAME_4.x OR viceversa
"FILENAME_5 is not present in DIR1"

Please help me with some ideas on how this can be achieved.

Thanks

 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      03-16-2007
Deepu wrote:
> I am trying to compare file names in 2 different directories.
> I need to 'diff' files with same name else if the filename (Ex :
> FILENAME_4) is not present in DIR2 then display message "FILENAME_4 is
> not present in DIR2" when it fails to get FILENAME_4.x OR viceversa
> "FILENAME_5 is not present in DIR1"
>
> Please help me with some ideas on how this can be achieved.


Please see "perldoc -q difference":
How do I compute the difference of two arrays? How do I compute the
intersection of two arrays?

jue


 
Reply With Quote
 
 
 
 
cmic
Guest
Posts: n/a
 
      03-16-2007
Hi Deepu

On 16 mar, 03:39, "Deepu" <pradeep...@gmail.com> wrote:
> Hi All,
>
> I am trying to compare file names in 2 different directories.
>
> DIR1:
>
> FILENAME_1.a
> FILENAME_2.a
> FILENAME_3.a
> FILENAME_4.a
>
> DIR2:
>
> FILENAME_1.x
> FILENAME_2.x
> FILENAME_3.x
> FILENAME_5.x
>
> I need to 'diff' files with same name else if the filename (Ex :
> FILENAME_4) is not present in DIR2 then display message "FILENAME_4 is
> not present in DIR2" when it fails to get FILENAME_4.x OR viceversa
> "FILENAME_5 is not present in DIR1"
>

If you are working under Unix (lInux or whatever), you can use the
diff command on diretories. The -r option of diff can even works
recursively.
But on windows ....

Regards
--
michel Marcon (aka cmic) sysadmin


> Please help me with some ideas on how this can be achieved.
>
> Thanks



 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      03-16-2007
cmic wrote:
> If you are working under Unix (lInux or whatever), you can use the
> diff command on diretories. The -r option of diff can even works
> recursively.
> But on windows ....


wahab@WINBOX ~
$ set | grep WINDIR & diff -v

WINDIR='C:\WINDOWS'

diff (GNU diffutils) 2.8.7
Written by Paul Eggert, Mike Haertel, David Hayes,
Richard Stallman, and Len Tower.
...


(this comes from a Cygwin environment)

Regards

Mirco
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      03-16-2007
Deepu <> wrote:

> I am trying to compare file names in 2 different directories.
>
> DIR1:
>
> FILENAME_1.a
> FILENAME_2.a
> FILENAME_3.a
> FILENAME_4.a
>
> DIR2:
>
> FILENAME_1.x
> FILENAME_2.x
> FILENAME_3.x
> FILENAME_5.x
>
>
> I need to 'diff' files with same name



None of those file have the same name...


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Deepu
Guest
Posts: n/a
 
      03-16-2007
> > FILENAME_2.a
> > FILENAME_3.a
> > FILENAME_4.a

>
> > DIR2:

>
> > FILENAME_1.x
> > FILENAME_2.x
> > FILENAME_3.x
> > FILENAME_5.x

>
> > I need to 'diff' files with same name

>
> None of those file have the same name...


I am trying to compare file with name "FILENAME_1" in DIR1 and
"FILENAME_1" in DIR2 after ignoring .a & .x and then continue the same
for other files.


 
Reply With Quote
 
Kalyan Manchikanti
Guest
Posts: n/a
 
      03-16-2007
On Mar 16, 9:13 am, "Deepu" <pradeep...@gmail.com> wrote:
> > > FILENAME_2.a
> > > FILENAME_3.a
> > > FILENAME_4.a

>
> > > DIR2:

>
> > > FILENAME_1.x
> > > FILENAME_2.x
> > > FILENAME_3.x
> > > FILENAME_5.x

>
> > > I need to 'diff' files with same name

>
> > None of those file have the same name...

>
> I am trying to compare file with name "FILENAME_1" in DIR1 and
> "FILENAME_1" in DIR2 after ignoring .a & .x and then continue the same
> for other files.


There is a unix utility called "dircmp" which does basic directory
comparision , but assuming you are looking for just file comparision
within different directories, have you tried using diff or sdiff
( sdiff is much more readable as it shows side by side comparision).A
simple for loop ( in shell or foreach in perl) . You can obviusly
pretty it up by cleaning up the syntax and including more error
checking..

in shell,
for i in 1..10
do
echo "##Comparing FILENAME_$i in DIR1 and DIR2##"
sdiff /DIR1/FILENAME_$i.a /DIR2/FILENAME_$i.x
done | tee <output>

in perl,
foreach (1..10) {
print "##comparing FILENAME_$i in DIR1 and DIR2##";
`sdiff /DIR1/FILENAME_$i.a /DIR2/FILENAME_$i.x >> /tmp/somefile`;
}




 
Reply With Quote
 
Deepu
Guest
Posts: n/a
 
      03-16-2007

> There is a unix utility called "dircmp" which does basic directory
> comparision , but assuming you are looking for just file comparision
> within different directories, have you tried using diff or sdiff
> ( sdiff is much more readable as it shows side by side comparision).A
> simple for loop ( in shell or foreach in perl) . You can obviusly
> pretty it up by cleaning up the syntax and including more error
> checking..
>
> in shell,
> for i in 1..10
> do
> echo "##Comparing FILENAME_$i in DIR1 and DIR2##"
> sdiff /DIR1/FILENAME_$i.a /DIR2/FILENAME_$i.x
> done | tee <output>
>
> in perl,
> foreach (1..10) {
> print "##comparing FILENAME_$i in DIR1 and DIR2##";
> `sdiff /DIR1/FILENAME_$i.a /DIR2/FILENAME_$i.x >> /tmp/somefile`;
> }


How to do this ONLY after checking files with same name exists in both
directories else display FILENAME doesnot exist in DIR1/2


 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      03-16-2007
Deepu wrote:
>>> FILENAME_2.a
>>> FILENAME_3.a
>>> FILENAME_4.a

>>
>>> DIR2:

>>
>>> FILENAME_1.x
>>> FILENAME_2.x
>>> FILENAME_3.x
>>> FILENAME_5.x

>>
>>> I need to 'diff' files with same name

>>
>> None of those file have the same name...

>
> I am trying to compare file with name "FILENAME_1" in DIR1 and
> "FILENAME_1" in DIR2 after ignoring .a & .x and then continue the same
> for other files.


perldoc File::Basename


 
Reply With Quote
 
Xicheng Jia
Guest
Posts: n/a
 
      03-16-2007
On Mar 16, 9:32 am, "Deepu" <pradeep...@gmail.com> wrote:
> > There is a unix utility called "dircmp" which does basic directory
> > comparision , but assuming you are looking for just file comparision
> > within different directories, have you tried using diff or sdiff
> > ( sdiff is much more readable as it shows side by side comparision).A
> > simple for loop ( in shell or foreach in perl) . You can obviusly
> > pretty it up by cleaning up the syntax and including more error
> > checking..

>
> > in shell,
> > for i in 1..10
> > do
> > echo "##Comparing FILENAME_$i in DIR1 and DIR2##"
> > sdiff /DIR1/FILENAME_$i.a /DIR2/FILENAME_$i.x
> > done | tee <output>

>
> > in perl,
> > foreach (1..10) {
> > print "##comparing FILENAME_$i in DIR1 and DIR2##";
> > `sdiff /DIR1/FILENAME_$i.a /DIR2/FILENAME_$i.x >> /tmp/somefile`;
> > }

>
> How to do this ONLY after checking files with same name exists in both
> directories else display FILENAME doesnot exist in DIR1/2- Hide quoted text -
>
> - Show quoted text -


perl -lne '
s{^/(?DIR2)|DIR1)/(.*?)\.[^.]*$}{$2};
$seen{$2}=1 and next if defined $1;
print "$2 of DIR1 is ", !$seen{$2}&&"not ", "present in DIR2"
' /DIR2/*.x /DIR1/*.a

FILENAME_1 of DIR1 is present in DIR2
FILENAME_2 of DIR1 is present in DIR2
FILENAME_3 of DIR1 is present in DIR2
FILENAME_4 of DIR1 is not present in DIR2

Regards,
Xicheng

 
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
Macro to indicate directories in filenames raphfrk C Programming 8 03-29-2008 04:04 PM
How to use different asmx in different sub-directories ? Sophie PARISOT ASP .Net Web Services 1 02-13-2006 03:52 AM
problem with filenames, Filenames and FILENAMES B.J. HTML 4 04-23-2005 08:13 PM
newbie wants to compile python list of filenames in selected directories anthonyberet Python 5 02-08-2005 08:10 PM
Comparing a string to filenames in a directory nj_perl_newbie Perl Misc 10 02-12-2004 08:41 PM



Advertisments