Comparing filenames in different directories

Discussion in 'Perl Misc' started by Deepu, Mar 16, 2007.

  1. Deepu

    Deepu Guest

    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
    Deepu, Mar 16, 2007
    #1
    1. Advertising

  2. 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
    Jürgen Exner, Mar 16, 2007
    #2
    1. Advertising

  3. Deepu

    cmic Guest

    Hi Deepu

    On 16 mar, 03:39, "Deepu" <> 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
    cmic, Mar 16, 2007
    #3
  4. Deepu

    Mirco Wahab Guest

    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
    Mirco Wahab, Mar 16, 2007
    #4
  5. 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
    Tad McClellan, Mar 16, 2007
    #5
  6. Deepu

    Deepu Guest

    > > 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.
    Deepu, Mar 16, 2007
    #6
  7. On Mar 16, 9:13 am, "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.


    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`;
    }
    Kalyan Manchikanti, Mar 16, 2007
    #7
  8. Deepu

    Deepu Guest


    > 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
    Deepu, Mar 16, 2007
    #8
  9. 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
    Jürgen Exner, Mar 16, 2007
    #9
  10. Deepu

    Xicheng Jia Guest

    On Mar 16, 9:32 am, "Deepu" <> 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
    Xicheng Jia, Mar 16, 2007
    #10
  11. Deepu

    Xicheng Jia Guest

    On Mar 16, 11:00 am, "Xicheng Jia" <> wrote:
    > On Mar 16, 9:32 am, "Deepu" <> 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};


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

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

    Regards,
    Xicheng

    > $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- Hide quoted text -
    >
    > - Show quoted text -
    Xicheng Jia, Mar 16, 2007
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. B.J.
    Replies:
    4
    Views:
    732
    Toby Inkster
    Apr 23, 2005
  2. anthonyberet
    Replies:
    5
    Views:
    422
    anthonyberet
    Feb 8, 2005
  3. raphfrk

    Macro to indicate directories in filenames

    raphfrk, Mar 28, 2008, in forum: C Programming
    Replies:
    8
    Views:
    298
    Antoninus Twink
    Mar 29, 2008
  4. Sophie PARISOT

    How to use different asmx in different sub-directories ?

    Sophie PARISOT, Feb 8, 2006, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    186
    Satish
    Feb 13, 2006
  5. nj_perl_newbie

    Comparing a string to filenames in a directory

    nj_perl_newbie, Feb 10, 2004, in forum: Perl Misc
    Replies:
    10
    Views:
    132
    J. Gleixner
    Feb 12, 2004
Loading...

Share This Page