Compare 2 files with different names (checksum?)

Discussion in 'Ruby' started by Max Williams, Sep 18, 2009.

  1. Max Williams

    Max Williams Guest

    Hey all

    I've been using File.compare to see if two files are the same, but now i
    have the case where one is a renamed copy of the other. I'd like to
    treat these two as the same in this case. Can anyone suggest a test
    that doesn't care about names?

    The files that have been renamed are xml (ie text format) if that makes
    life easier but a solution that works with binaries as well would be
    ideal.

    I've been trying to google checksums in ruby but haven't seen a useful
    example, maybe i was just being blind. Will a checksum ignore the name
    of the file?

    thanks
    max
    --
    Posted via http://www.ruby-forum.com/.
     
    Max Williams, Sep 18, 2009
    #1
    1. Advertising

  2. On Fri, Sep 18, 2009 at 12:05 PM, Max Williams
    <> wrote:
    > Hey all
    >
    > I've been using File.compare to see if two files are the same, but now i
    > have the case where one is a renamed copy of the other. =A0I'd like to
    > treat these two as the same in this case. =A0Can anyone suggest a test
    > that doesn't care about names?
    >
    > The files that have been renamed are xml (ie text format) if that makes
    > life easier but a solution that works with binaries as well would be
    > ideal.
    >
    > I've been trying to google checksums in ruby but haven't seen a useful
    > example, maybe i was just being blind. =A0Will a checksum ignore the name
    > of the file?


    google for Ruby digest. A SHA1 hash is a good alternative.

    require 'digest/sha1'

    def contents_equal?(path_to_file_1, path_to_file_2)
    Digest::SHA1.file(path_to_file_1).hexdigest =3D=3D
    Digest::SHA1.file(path_to_file_2).hexdigest
    end


    --=20
    Rick DeNatale

    Blog: http://talklikeaduck.denhaven2.com/
    Twitter: http://twitter.com/RickDeNatale
    WWR: http://www.workingwithrails.com/person/9021-rick-denatale
    LinkedIn: http://www.linkedin.com/in/rickdenatale
     
    Rick DeNatale, Sep 18, 2009
    #2
    1. Advertising

  3. Max Williams

    Max Williams Guest

    Max Williams, Sep 18, 2009
    #3
  4. Max Williams wrote:
    > Hey all
    >
    > I've been using File.compare to see if two files are the same, but now i
    > have the case where one is a renamed copy of the other. I'd like to
    > treat these two as the same in this case. Can anyone suggest a test
    > that doesn't care about names?
    >
    > The files that have been renamed are xml (ie text format) if that makes
    > life easier but a solution that works with binaries as well would be
    > ideal.
    >
    > I've been trying to google checksums in ruby but haven't seen a useful
    > example, maybe i was just being blind. Will a checksum ignore the name
    > of the file?
    >
    > thanks
    > max


    Hmm? File.compare does not care about names, the content of the files is
    compared.

    require 'ftools'
    File.compare(path_to_file_1, path_to_file_2)

    Works fine for me (Ruby 1.8.6, WinXP). Are there issues I'm not aware
    of?

    Kind regards,

    Siep
    --
    Posted via http://www.ruby-forum.com/.
     
    Siep Korteling, Sep 18, 2009
    #4
  5. Max Williams

    Max Williams Guest

    Hmmm (tests) - no Siep, you're right. Looks like the two files i
    thought were the same weren't after all. Which is a problem, since the
    second one is supposed to be a renamed copy of the first. Looks like i
    need to go back a step here. :/

    thanks
    max
    --
    Posted via http://www.ruby-forum.com/.
     
    Max Williams, Sep 18, 2009
    #5
    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. Paddy McCarthy
    Replies:
    3
    Views:
    737
    Anthony J Bybell
    Sep 24, 2004
  2. Bob
    Replies:
    1
    Views:
    403
    Lucas Tam
    Jul 30, 2004
  3. Carl
    Replies:
    0
    Views:
    535
  4. Ola Natvig
    Replies:
    12
    Views:
    1,157
    Nick Craig-Wood
    Feb 11, 2005
  5. Ralph Smith
    Replies:
    6
    Views:
    202
    Pit Capitain
    Oct 26, 2005
Loading...

Share This Page