' ' The user is prompted for the paths of the two ' documents to be compared. The FileSystemObject ' is used to perform validation on the file paths ' provided by the user. If not valid, the script ' exits gracefully. ' ' Note that the CreateObject call uses the generic ' reference, 'Word.Application' with no version ' number. 5 UNIX diff Command Examples of How to Compare Two Text Files The UNIX diff command compares the contents of two text files and outputs a list of differences. If desired, you may instruct it to ignore spacing or case variations.
- Sublime Text 2 Show Difference Between Two Files
- Show Differences Between 2 Text Files
- Excel Show Difference Between 2 Columns
- Find The Difference Text
- Compare Differences In Text Files
Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to compare two files.
Hey, Scripting Guy! I have a script that I wrote to compare two files, but it seems really slow. I am wondering what I can do to speed things up a bit.
—JW
Hello JW,
Microsoft Scripting Guy, Ed Wilson, is here. I looked at the script you supplied, where you use Compare-Object to compare two files. Here is your script:
$fileA = 'C:fsomyfile.txt'
$fileB = 'C:fsoCopyOfmyfile.txt'
$fileC = 'C:fsochangedMyFile.txt'
if(Compare-Object -ReferenceObject $(Get-Content $fileA) -DifferenceObject $(Get-Content $fileB))
Sublime Text 2 Show Difference Between Two Files
{'files are different'}
Else {'Files are the same'}
When I run the script and compare FileA with FileB, the script returns the correct response:
When I change it to use FileC, the script also works:
The three files are shown here:
So JW, this is a very simple test case. What is really going on when using Compare-Object?
I can use the Windows PowerShell ISE to run a portion of the code and look at it. To do this, I highlight the Compare-Object statement and press F-8 to execute only that portion of the code. This is shown here:
PS C:> Compare-Object -ReferenceObject $(Get-Content $fileA) -DifferenceObject $(Get-Content $fileC)
InputObject SideIndicator
———– ————-
Additional values =>
And when I compare FileA with FileB, the following appears:
PS C:> Compare-Object -ReferenceObject $(Get-Content $fileA) -DifferenceObject $(Get-Content $fileB)
PS C:>
This triggers the ELSE portion of the code. Although this works, it can be a bit slow, and on more complex files, I would think it would also be a bit unreliable.
So a better way to do this is to use Get-FileHash and compare the HASH property. Your revised script is shown here:
$fileA = 'C:fsomyfile.txt'
$fileB = 'C:fsoCopyOfmyfile.txt'
$fileC = 'C:fsochangedMyFile.txt'
if((Get-FileHash $fileA).hash -ne (Get-FileHash $fileC).hash)
{'files are different'}
Else {'Files are the same'}
Now, when I look at the portion of the code that executes, I can see that I am dealing with a Boolean, instead of trying to evaluate whether output (which is basically ignored) appears or not (as in your previous script).
In the following, I execute only the Get-FileHash portion of the script:
PS C:> (Get-FileHash $fileA).hash -ne (Get-FileHash $fileC).hash
True
PS C:> (Get-FileHash $fileA).hash -ne (Get-FileHash $fileB).hash
False
In addition, the Get-FileHash code is rather efficient because Windows PowerShell is pretty fast when it comes to getting the file hash. Plus this operation simply obtains the file hashes, and compares the two hashes. Your original script reads in the complete file, and then compares it line-by-line, so it is much less efficient.
JW, that is all there is to using Windows PowerShell to compare two files. Troubleshooting Week will continue tomorrow when I will talk about more cool stuff.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at [email protected], or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to compare two files.
Hey, Scripting Guy! I have a script that I wrote to compare two files, but it seems really slow. I am wondering what I can do to speed things up a bit.
—JW
Hello JW,
Microsoft Scripting Guy, Ed Wilson, is here. I looked at the script you supplied, where you use Compare-Object to compare two files. Here is your script:
$fileA = 'C:fsomyfile.txt'
$fileB = 'C:fsoCopyOfmyfile.txt'
$fileC = 'C:fsochangedMyFile.txt'
if(Compare-Object -ReferenceObject $(Get-Content $fileA) -DifferenceObject $(Get-Content $fileB))
{'files are different'}
Else {'Files are the same'}
When I run the script and compare FileA with FileB, the script returns the correct response:
When I change it to use FileC, the script also works:
The three files are shown here:
So JW, this is a very simple test case. What is really going on when using Compare-Object?
I can use the Windows PowerShell ISE to run a portion of the code and look at it. To do this, I highlight the Compare-Object statement and press F-8 to execute only that portion of the code. This is shown here:
PS C:> Compare-Object -ReferenceObject $(Get-Content $fileA) -DifferenceObject $(Get-Content $fileC)
InputObject SideIndicator
———– ————-
Additional values =>
And when I compare FileA with FileB, the following appears:
PS C:> Compare-Object -ReferenceObject $(Get-Content $fileA) -DifferenceObject $(Get-Content $fileB)
PS C:>
This triggers the ELSE portion of the code. Although this works, it can be a bit slow, and on more complex files, I would think it would also be a bit unreliable.
So a better way to do this is to use Get-FileHash and compare the HASH property. Your revised script is shown here:
$fileA = 'C:fsomyfile.txt'
$fileB = 'C:fsoCopyOfmyfile.txt'
Show Differences Between 2 Text Files
$fileC = 'C:fsochangedMyFile.txt'
if((Get-FileHash $fileA).hash -ne (Get-FileHash $fileC).hash)
Excel Show Difference Between 2 Columns
{'files are different'}
Else {'Files are the same'}
Now, when I look at the portion of the code that executes, I can see that I am dealing with a Boolean, instead of trying to evaluate whether output (which is basically ignored) appears or not (as in your previous script).
In the following, I execute only the Get-FileHash portion of the script:
PS C:> (Get-FileHash $fileA).hash -ne (Get-FileHash $fileC).hash
True
PS C:> (Get-FileHash $fileA).hash -ne (Get-FileHash $fileB).hash
Find The Difference Text
False
In addition, the Get-FileHash code is rather efficient because Windows PowerShell is pretty fast when it comes to getting the file hash. Plus this operation simply obtains the file hashes, and compares the two hashes. Your original script reads in the complete file, and then compares it line-by-line, so it is much less efficient.
JW, that is all there is to using Windows PowerShell to compare two files. Troubleshooting Week will continue tomorrow when I will talk about more cool stuff.
![Differences Differences](/uploads/1/2/6/0/126061459/577603489.png)
Compare Differences In Text Files
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at [email protected], or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy