Comparing filenames in different directories

D

Deepu

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
 
J

Jürgen Exner

Deepu said:
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
 
C

cmic

Hi Deepu

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
 
M

Mirco Wahab

cmic said:
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
 
T

Tad McClellan

Deepu said:
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...
 
D

Deepu

FILENAME_2.a
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.
 
K

Kalyan Manchikanti

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`;
}
 
D

Deepu

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
 
J

Jürgen Exner

Deepu said:
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
 
X

Xicheng Jia

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
 
X

Xicheng Jia

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};

hmmm, the s/// expression is completely redundant..

m{^/(?:(DIR2)|DIR1)/(.*?)\.[^.]*$};

Regards,
Xicheng
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top