Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Compare 2 files with different names (checksum?)

Reply
Thread Tools

Compare 2 files with different names (checksum?)

 
 
Max Williams
Guest
Posts: n/a
 
      09-18-2009
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/.

 
Reply With Quote
 
 
 
 
Rick DeNatale
Guest
Posts: n/a
 
      09-18-2009
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/pers...-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

 
Reply With Quote
 
 
 
 
Max Williams
Guest
Posts: n/a
 
      09-18-2009
that's great, thanks Rick!
max
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Siep Korteling
Guest
Posts: n/a
 
      09-18-2009
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/.

 
Reply With Quote
 
Max Williams
Guest
Posts: n/a
 
      09-18-2009
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/.

 
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
Converting 'flat' gate level names to hierarchical names Paddy McCarthy VHDL 3 09-24-2004 05:34 PM
table field names vs. display names Bob ASP .Net 1 07-30-2004 05:06 PM
Matching attribute names to element names in a different path Carl XML 0 04-01-2004 01:15 PM
WSDL- Mapping Application Defined Names to XML Names Craig XML 0 02-09-2004 04:14 PM
XSL rules applying to XSD (XML schema) defined type names (as opposed to node names) Lewis G. Pringle, Jr. XML 0 09-30-2003 10:34 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57