| Author |
Binary file compare
|
|
| Robert 2005-08-29, 6:55 pm |
| How can I do a binary file compare in Visual basic 6.0?
| |
| Jeff Johnson [MVP: VB] 2005-08-29, 6:55 pm |
|
"Robert" <Robert@discussions.microsoft.com> wrote in message
news:B73278AA-4FF9-413E-900B-653182DE94B4@microsoft.com...
> How can I do a binary file compare in Visual basic 6.0?
Open both files in Binary mode, read all (or a portion, if they're too big)
into two binary arrays, and then compare them byte-by-byte.
| |
| Robert 2005-08-29, 6:55 pm |
| The files could be any type of files not just text files. What type of open
statement would I use?
"Jeff Johnson [MVP: VB]" wrote:
>
> "Robert" <Robert@discussions.microsoft.com> wrote in message
> news:B73278AA-4FF9-413E-900B-653182DE94B4@microsoft.com...
>
>
> Open both files in Binary mode, read all (or a portion, if they're too big)
> into two binary arrays, and then compare them byte-by-byte.
>
>
>
| |
| Larry Serflaten 2005-08-29, 6:55 pm |
|
"Jeff Johnson [MVP: VB]" <i.get@enough.spam> wrote
> "Robert" <Robert@discussions.microsoft.com> wrote
>
>
> Open both files in Binary mode, read all (or a portion, if they're too big)
> into two binary arrays, and then compare them byte-by-byte.
For an exact match, he wouldn't have to go byte by byte. If the lengths
are equal, StrComp will compare the data:
Dim A() As Byte, B() As Byte
A = "Hello"
B = "Hello"
Debug.Print StrComp(A, B) ' 0 (Equal)
B = "hello"
Debug.Print StrComp(A, B) ' -1 (A < B)
A = "hello "
Debug.Print StrComp(A, B) ' 1 (A > B)
LFS
| |
| Jeff Johnson [MVP: VB] 2005-08-29, 6:55 pm |
|
"Larry Serflaten" <serflaten@usinternet.com> wrote in message
news:usPfY9KrFHA.3352@TK2MSFTNGP14.phx.gbl...
>
> For an exact match, he wouldn't have to go byte by byte. If the lengths
> are equal, StrComp will compare the data:
I always forget about using StrComp() on things that aren't actually
Strings....
| |
| Jeff Johnson [MVP: VB] 2005-08-29, 6:55 pm |
|
"Robert" <Robert@discussions.microsoft.com> wrote in message
news:1050A632-BE2F-4232-A4AB-E38B302C4CB8@microsoft.com...
> The files could be any type of files not just text files. What type of
> open
> statement would I use?
Binary mode deals with files as a stream of bytes and does not treat any
bytes as "special." On the other hand, a text mode (like Input) treats CR/LF
as "special." So Binary (which is what I suggested in the first place) is
the way to go.
| |
|
|
"Larry Serflaten" <serflaten@usinternet.com> wrote in message
news:usPfY9KrFHA.3352@TK2MSFTNGP14.phx.gbl...
>
> "Jeff Johnson [MVP: VB]" <i.get@enough.spam> wrote
>
big)[color=darkred]
>
> For an exact match, he wouldn't have to go byte by byte. If the lengths
> are equal, StrComp will compare the data:
>
> Dim A() As Byte, B() As Byte
>
> A = "Hello"
> B = "Hello"
> Debug.Print StrComp(A, B) ' 0 (Equal)
> B = "hello"
> Debug.Print StrComp(A, B) ' -1 (A < B)
> A = "hello "
> Debug.Print StrComp(A, B) ' 1 (A > B)
>
>
> LFS
>
You also want to remember to set the 'compare' argument or review the
Compare Option.
I've been burnt.
-ralph
| |
| Karl E. Peterson 2005-08-29, 6:55 pm |
| Larry Serflaten wrote:
> "Jeff Johnson [MVP: VB]" <i.get@enough.spam> wrote
>
> For an exact match, he wouldn't have to go byte by byte. If the
> lengths are equal, StrComp will compare the data:
Ever tried this?
Private Declare Function EqualMemory Lib "ntdll" Alias "RtlCompareMemory"
(Destination As Any, Source As Any, ByVal Length As Long) As Long
I haven't, but it sure looks useful.
--
Working Without a .NET?
http://classicvb.org/petition
| |
| mr_unreliable 2005-08-29, 6:55 pm |
| if you still have a dos capability on your system, you could
run the "fc.exe" (fc = filecompare) utility, and capture the
output.
cheers, jw (the ancient-dos-bat-programmer)
________________________________________
____________________
You got questions? WE GOT ANSWERS!!! ..(but,
no guarantee the answers will be applicable to the questions)
Robert wrote:
> How can I do a binary file compare in Visual basic 6.0?
| |
| Larry Serflaten 2005-08-29, 9:55 pm |
|
"Ralph" <nt_consulting64@yahoo.com> wrote
> You also want to remember to set the 'compare' argument or review the
> Compare Option.
>
> I've been burnt.
I haven't... <g>
But yeah, why not set it and be explicit....
Good catch.
LFS
| |
|
| Adding to the pile. You could hash the two files and compare the hashed
values. MD5 hash would work great in this type of application..
Large Archive of Cryptographic Algorithms for VB (v2.0)
http://www.veign.com/vrc_codeview.asp?type=app&id=76
Keep in mind the hash will only tell you if the files are the same or not...
--
Chris Hanscom - Microsoft MVP (VB)
Veign's Resource Center
http://www.veign.com/vrc_main.asp
--
"Robert" <Robert@discussions.microsoft.com> wrote in message
news:B73278AA-4FF9-413E-900B-653182DE94B4@microsoft.com...
> How can I do a binary file compare in Visual basic 6.0?
| |
| Randy Birch 2005-08-29, 9:55 pm |
| That seems to work nicely ...
Option Explicit
Private Declare Function EqualMemory Lib "ntdll" _
Alias "RtlCompareMemory" _
(Destination As Any, _
Source As Any, _
ByVal Length As Long) As Long
Private Sub Command1_Click()
Dim sizeOfArray As Long
Dim a(0 To 3) As Long '4 elements
Dim b(0 To 3) As Long '4 elements
a(0) = 25 'same
a(3) = 50 'same
b(0) = 25 'same
b(3) = 50 'same
sizeOfArray = (UBound(a) - LBound(a) + 1) * 4
Debug.Print EqualMemory(ByVal VarPtr(a(0)), ByVal VarPtr(b(0)), ByVal
sizeOfArray)
'> 16, the size of the above array that matches
'--------------------------------------------------------
a(0) = 25 'same
a(3) = 30 'different
b(0) = 25 'same
b(3) = 50 'different
sizeOfArray = (UBound(a) - LBound(a) + 1) * 4
Debug.Print EqualMemory(ByVal VarPtr(a(0)), ByVal VarPtr(b(0)), ByVal
sizeOfArray)
'> 12, indicating the data is the same up to byte position 12: (a(0)=b(0)
(4 bytes), a(1)=b(1) (8 bytes), a(2)=b(2) (12 bytes), a(3)<>b(3) (16 bytes
position))
'--------------------------------------------------------
Dim dd(0 To 3) As Byte
Dim ee(0 To 3) As Byte
dd(0) = 56 'same
dd(3) = 255 'same
ee(0) = 56 'same
ee(3) = 255 'same
sizeOfArray = (UBound(dd) - LBound(dd) + 1) * 1 'multiplication actually
superfluous here
Debug.Print EqualMemory(ByVal VarPtr(dd(0)), ByVal VarPtr(ee(0)), ByVal
sizeOfArray)
'>4, size of array, one byte per byte (duh)
'--------------------------------------------------------
dd(0) = 56 'different
dd(3) = 255 'same
ee(0) = 38 'different
ee(3) = 255 'same
sizeOfArray = (UBound(dd) - LBound(dd) + 1) * 1 'multiplication actually
superfluous here
Debug.Print EqualMemory(ByVal VarPtr(dd(0)), ByVal VarPtr(ee(0)), ByVal
sizeOfArray)
'>0, meaning data is different starting with the first byte
End Sub
--
Randy Birch
MS MVP Visual Basic
http://vbnet.mvps.org/
----------------------------------------------------------------------------
Read. Decide. Sign the petition to Microsoft.
http://classicvb.org/petition/
----------------------------------------------------------------------------
"Karl E. Peterson" <karl@mvps.org> wrote in message
news:u8Vp33NrFHA.1128@TK2MSFTNGP11.phx.gbl...
: Larry Serflaten wrote:
: > "Jeff Johnson [MVP: VB]" <i.get@enough.spam> wrote
: >> "Robert" <Robert@discussions.microsoft.com> wrote
: >>
: >>> How can I do a binary file compare in Visual basic 6.0?
: >>
: >> Open both files in Binary mode, read all (or a portion, if they're
: >> too big) into two binary arrays, and then compare them byte-by-byte.
: >
: > For an exact match, he wouldn't have to go byte by byte. If the
: > lengths are equal, StrComp will compare the data:
:
: Ever tried this?
:
: Private Declare Function EqualMemory Lib "ntdll" Alias "RtlCompareMemory"
: (Destination As Any, Source As Any, ByVal Length As Long) As Long
:
: I haven't, but it sure looks useful.
: --
: Working Without a .NET?
: http://classicvb.org/petition
:
:
| |
| Robert 2005-08-30, 6:55 pm |
| Byte to byte compare and StrComp take a long time. Are there any FSO methods
that would speed things up?
"Jeff Johnson [MVP: VB]" wrote:
>
> "Robert" <Robert@discussions.microsoft.com> wrote in message
> news:1050A632-BE2F-4232-A4AB-E38B302C4CB8@microsoft.com...
>
>
> Binary mode deals with files as a stream of bytes and does not treat any
> bytes as "special." On the other hand, a text mode (like Input) treats CR/LF
> as "special." So Binary (which is what I suggested in the first place) is
> the way to go.
>
>
>
| |
| Robert 2005-08-30, 6:55 pm |
| My mistake. The input is what is taking a long time not the ocmpare.
"Jeff Johnson [MVP: VB]" wrote:
>
> "Robert" <Robert@discussions.microsoft.com> wrote in message
> news:1050A632-BE2F-4232-A4AB-E38B302C4CB8@microsoft.com...
>
>
> Binary mode deals with files as a stream of bytes and does not treat any
> bytes as "special." On the other hand, a text mode (like Input) treats CR/LF
> as "special." So Binary (which is what I suggested in the first place) is
> the way to go.
>
>
>
| |
| Jeff Johnson [MVP: VB] 2005-08-30, 6:55 pm |
|
"Robert" <Robert@discussions.microsoft.com> wrote in message
news:BEB23E4D-6983-4051-B3EF-2D66F08469AA@microsoft.com...
> Byte to byte compare and StrComp take a long time. Are there any FSO
> methods
> that would speed things up?
HAHAHAHAHAHAHA!! You used "FSO" and "speed up" in the same sentence.
Hilarious!!
| |
| Bob Butler 2005-08-30, 6:55 pm |
| "Robert" <Robert@discussions.microsoft.com> wrote in message
news:ED07497D-88B3-4D9B-935D-6AC8F02BF38E@microsoft.com
> My mistake. The input is what is taking a long time not the ocmpare.
If you are reading them byte-by-byte then it would be slow. You need to
read large chunks of data and then compare the data in memory.
--
Reply to the group so all can participate
VB.Net: "Fool me once..."
| |
| Robert 2005-08-30, 6:55 pm |
| I am reading using the input function into a variable. Is there any faster
way?
"Bob Butler" wrote:
> "Robert" <Robert@discussions.microsoft.com> wrote in message
> news:ED07497D-88B3-4D9B-935D-6AC8F02BF38E@microsoft.com
>
> If you are reading them byte-by-byte then it would be slow. You need to
> read large chunks of data and then compare the data in memory.
>
> --
> Reply to the group so all can participate
> VB.Net: "Fool me once..."
>
>
| |
|
|
"Jeff Johnson [MVP: VB]" <i.get@enough.spam> wrote in message
news:uxLy8bXrFHA.304@TK2MSFTNGP11.phx.gbl...
>
> "Robert" <Robert@discussions.microsoft.com> wrote in message
> news:BEB23E4D-6983-4051-B3EF-2D66F08469AA@microsoft.com...
>
>
> HAHAHAHAHAHAHA!! You used "FSO" and "speed up" in the same sentence.
> Hilarious!!
>
I believe the OP feels that "Fewer_lines_of_code == Faster_code", which is
generally true - but results will depend heavily on what those lines are
chewing on.
Robert,
I can appreciate how the FSO can suck one it. It looks simple, is very
intuitive, and you can do some relatively neat stuff right-out off the bat
without too much thought or typing. It is alluring.
However, it is a siren's song and its simplicity comes at a terrible cost.
The FSO is only useful for ad hoc projects on a single box - The scripting
runtime will not distribute well. It is slow, and can be an amazingly
resource hog.
All of these are good reasons to avoid it, but the real terror is, unlike
Odysseus, you can't see the danger coming. All you will know is than sooner
or later you ARE going to go aground. Sometimes with only minor damage,
other times with the lost of all hands.
If you love suspense then use the FSO.
If you wish to avoid thrills then remove the FSO from your tool box.
-ralph
| |
| Duane Bozarth 2005-08-30, 6:55 pm |
| Robert wrote:
>
> I am reading using the input function into a variable. Is there any faster
> way?
Get
| |
| Robert 2005-08-30, 6:55 pm |
| Visual Basic 6.0 does not recognize Get.
"Duane Bozarth" wrote:
> Robert wrote:
>
> Get
>
| |
| Ken Halter 2005-08-30, 6:55 pm |
| "Robert" <Robert@discussions.microsoft.com> wrote in message
news:F468E784-A5DE-4A52-B005-B2E9C56B9310@microsoft.com...
> Visual Basic 6.0 does not recognize Get.
>
Sure it does... unless you have a missing reference somewhere, this should
load a file and show it in a textbox
'=========
Option Explicit
Private Sub Command1_Click()
Dim bt() As Byte
bt = ReturnBytes("C:\Temp\Your File.Any")
Text1.Text = StrConv(bt, vbUnicode)
End Sub
Private Function ReturnBytes(FileName As String) As Byte()
Dim iFile As Integer
iFile = FreeFile
Open FileName For Binary As iFile
ReDim ReturnBytes(LOF(iFile))
Get #iFile, , ReturnBytes
Close iFile
End Function
'=========
--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..
| |
| Jeff Johnson [MVP: VB] 2005-08-30, 6:55 pm |
|
"Ralph" <nt_consulting64@yahoo.com> wrote in message
news:YuCdnc-t9KM0GoneRVn-rA@arkansas.net...
> I can appreciate how the FSO can suck one it.
For those playing the home game, I believe this was supposed to be
I can appreciate how the FSO can suck one in
and not
I can appreciate how the FSO can suck on it
If you have any other humorous permutations, please share them.
| |
| Larry Serflaten 2005-08-30, 6:55 pm |
|
"Robert" <Robert@discussions.microsoft.com> wrote
> Visual Basic 6.0 does not recognize Get.
Yes it does.
Type Get in the code window,
place the cursor on it
and hit F1.
LFS
| |
| Robert 2005-08-30, 6:55 pm |
| Your correct it does. I was using a variable instead of a byte array. Thank
you.
"Ken Halter" wrote:
> "Robert" <Robert@discussions.microsoft.com> wrote in message
> news:F468E784-A5DE-4A52-B005-B2E9C56B9310@microsoft.com...
>
> Sure it does... unless you have a missing reference somewhere, this should
> load a file and show it in a textbox
> '=========
> Option Explicit
>
> Private Sub Command1_Click()
> Dim bt() As Byte
>
> bt = ReturnBytes("C:\Temp\Your File.Any")
>
> Text1.Text = StrConv(bt, vbUnicode)
>
> End Sub
>
>
> Private Function ReturnBytes(FileName As String) As Byte()
> Dim iFile As Integer
>
> iFile = FreeFile
> Open FileName For Binary As iFile
> ReDim ReturnBytes(LOF(iFile))
> Get #iFile, , ReturnBytes
> Close iFile
>
> End Function
>
> '=========
>
> --
> Ken Halter - MS-MVP-VB - http://www.vbsight.com
> DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
> Please keep all discussions in the groups..
>
>
>
| |
| Karl E. Peterson 2005-08-30, 6:55 pm |
| Jeff Johnson [MVP: VB] wrote:
> "Ralph" <nt_consulting64@yahoo.com> wrote in message
> news:YuCdnc-t9KM0GoneRVn-rA@arkansas.net...
>
>
> For those playing the home game, I believe this was supposed to be
>
> I can appreciate how the FSO can suck one in
>
> and not
>
> I can appreciate how the FSO can suck on it
>
> If you have any other humorous permutations, please share them.
FSO can suck hind tit?
--
Working Without a .NET?
http://classicvb.org/petition
| |
| Karl E. Peterson 2005-08-30, 6:55 pm |
| Robert wrote:
> Your correct it does. I was using a variable instead of a byte
> array. Thank you.
Get works with single variables, too.
[color=darkred]
> "Ken Halter" wrote:
<ahem>
ReDim ReturnBytes(0 To LOF(iFile) - 1)
[color=darkred]
Carry on...
--
Working Without a .NET?
http://classicvb.org/petition
| |
| Robert 2005-08-30, 6:55 pm |
| Company will not purchase .net for me. Does not matter, I am reitreing at
the end of November.
"Karl E. Peterson" wrote:
> Robert wrote:
>
> Get works with single variables, too.
>
>
> <ahem>
>
> ReDim ReturnBytes(0 To LOF(iFile) - 1)
>
>
> Carry on...
> --
> Working Without a .NET?
> http://classicvb.org/petition
>
>
>
| |
|
|
"Robert" <Robert@discussions.microsoft.com> wrote in message
news:29DD7B29-3C26-4FDF-B604-587597432BA6@microsoft.com...
> Company will not purchase .net for me. Does not matter, I am reitreing at
> the end of November.
>
Well, in that case, I take everything I said, back.
I would spread as many FSOs about as possible.
-ralph
| |
| Karl E. Peterson 2005-08-30, 6:55 pm |
| Robert wrote:
> Company will not purchase .net for me. Does not matter, I am
> reitreing at the end of November.
I take it this is in response to:
[color=darkred]
If so, I *really* urge you to click that link. It's certainly not proselytizing
..NET!
--
Working Without a .NET?
http://classicvb.org/petition
| |
| Jeff Johnson [MVP: VB] 2005-08-30, 6:55 pm |
|
"Robert" <Robert@discussions.microsoft.com> wrote in message
news:29DD7B29-3C26-4FDF-B604-587597432BA6@microsoft.com...
> Company will not purchase .net for me. Does not matter, I am reitreing at
> the end of November.
Do you actually think Karl was recommending .NET? That's even funnier than
the FSO thing! (Follow the link in his .sig.)
| |
|
|
"Jeff Johnson [MVP: VB]" <i.get@enough.spam> wrote in message
news:O8C3uOarFHA.464@TK2MSFTNGP15.phx.gbl...
>
> "Robert" <Robert@discussions.microsoft.com> wrote in message
> news:29DD7B29-3C26-4FDF-B604-587597432BA6@microsoft.com...
>
at[color=darkred]
>
> Do you actually think Karl was recommending .NET? That's even funnier than
> the FSO thing! (Follow the link in his .sig.)
>
You and Karl need to get together. Both can seemingly project amazing
authoritative power, but Karl's apparently works in reverse.
-ralph
| |
|
| "Veign" <NOSPAMinveign@veign.com> wrote in message
news:uj7jZ4PrFHA.464@TK2MSFTNGP15.phx.gbl...
> Adding to the pile. You could hash the two files and compare the hashed
> values. MD5 hash would work great in this type of application..
>
> Large Archive of Cryptographic Algorithms for VB (v2.0)
> http://www.veign.com/vrc_codeview.asp?type=app&id=76
>
> Keep in mind the hash will only tell you if the files are the same or
not...
hash may say: "files are different", but may not say: "files are the same"
[different hash means different files, but same hash doesn't mean same
files]
>
> --
> Chris Hanscom - Microsoft MVP (VB)
> Veign's Resource Center
> http://www.veign.com/vrc_main.asp
> --
>
>
> "Robert" <Robert@discussions.microsoft.com> wrote in message
> news:B73278AA-4FF9-413E-900B-653182DE94B4@microsoft.com...
>
>
| |
|
|
"Karl E. Peterson" <karl@mvps.org> wrote in message
news:u4zyjMZrFHA.3340@TK2MSFTNGP15.phx.gbl...
> Jeff Johnson [MVP: VB] wrote:
>
> FSO can suck hind tit?
What's a "hind tit"? Breasts on the back?
--
Mike
Microsoft MVP Visual Basic
| |
| Duane Bozarth 2005-08-30, 6:55 pm |
| MikeD wrote:
....
>
> What's a "hind tit"? Breasts on the back?
>
You obviously aren't a farm boy... :)
Need I say more, specifically?
| |
|
|
|
|
"Duane Bozarth" <dpbozarth@swko.dot.net> wrote in message
news:4314EDDD.555AB3EE@swko.dot.net...
> MikeD wrote:
> ...
>
> You obviously aren't a farm boy... :)
Nope. Lived in cities all my life.
>
> Need I say more, specifically?
I guess not. Apparently, not being a "farm boy", I missed that one. I doubt
I'm alone in not understanding it. <g>
--
Mike
Microsoft MVP Visual Basic
| |
| Duane Bozarth 2005-08-30, 9:55 pm |
| MikeD wrote:
>
> "Duane Bozarth" <dpbozarth@swko.dot.net> wrote in message
> news:4314EDDD.555AB3EE@swko.dot.net...
>
> Nope. Lived in cities all my life.
>
>
> I guess not. Apparently, not being a "farm boy", I missed that one. I doubt
> I'm alone in not understanding it. <g>
Know how a sow (female hog) is plumbed for the little 'uns?
The last two spigots typically do not provide nearly the output of the
remaining--hence the expression.
| |
|
| "Karl E. Peterson" <karl@mvps.org> wrote in message
news:%23S1e3ybrFHA.2588@tk2msftngp13.phx.gbl...
> MikeD wrote:
>
> Not back, but closest to rear, and generally least developed.
> http://www.urbandictionary.com/defi...ucking+hind+tit
> --
> Working Without a .NET?
> http://classicvb.org/petition
>
I'm not recognizing any of this as vb6...are you sure you're not talking
..Net here?
:-)
<vbg>
....whatever it is yer makin me thirsty
| |
| J French 2005-08-31, 7:55 am |
| On Tue, 30 Aug 2005 06:48:02 -0700, =?Utf-8?B?Um9iZXJ0?=
<Robert@discussions.microsoft.com> wrote:
>Byte to byte compare and StrComp take a long time. Are there any FSO methods
>that would speed things up?
In VB Byte to Byte compare is astonishingly fast
- but only when the App is compiled
To give you an idea, it is as fast as Delphi, and it is darn difficult
to beat Delphi's optimizing compiler using raw ASM
| |
| Ken Halter 2005-08-31, 6:55 pm |
| "Karl E. Peterson" <karl@mvps.org> wrote in message
news:ewyi2NZrFHA.1204@TK2MSFTNGP15.phx.gbl...
>
> <ahem>
>
> ReDim ReturnBytes(0 To LOF(iFile) - 1)
>
> Carry on...
> --
> Working Without a .NET?
> http://classicvb.org/petition
>
>
oops... snip mechanic fixed that last night <g>
--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..
| |
| Karl E. Peterson 2005-08-31, 6:55 pm |
| MP wrote:
> "Karl E. Peterson" <karl@mvps.org> wrote in message
> news:%23S1e3ybrFHA.2588@tk2msftngp13.phx.gbl...
>
> I'm not recognizing any of this as vb6...are you sure you're not
> talking .Net here?
> :-)
> <vbg>
> ...whatever it is yer makin me thirsty
LOL! Well, yeah, now that you mention it, VFred sucks hind tit, too!
--
Working Without a .NET?
http://classicvb.org/petition
|
|
|
|