Compare Files In Vbscript
It would be easy to just put a textreader on either file, then read it into a string. You can compare both lines of code for equality. That's barely any code, but then that only compares that the files have the exact same contents. Check out this Visual Basic Script for an automated and simplified process of comparing files. Why Do I Need To Compare Documents? Good question, and the answer will depend on your particular circumstances. In my case, the need to compare documents arose when I wanted to find the differences in various code modules. Hi I want a vbscript which will output the difference between two text files and write the difference in 3rd text file.
There is a great command line tool that can be used to compare files to see if there are any content or binary code differences that you can access if you are using a PC. File Compare or FC as we will refer to is from here on out, is a simple program that will compare the contents of text or binary files and is capable of comparing both ASCII and Unicode text. You can use this tool to display any lines from two files or two sets of files that do not match up with the others.
File Compare’s Switches and Parameters
- /B – This switch will perform a binary comparison.
- /C – If you need to do a case insensitive comparison, use this switch.
- /A – This switch will make FC show only the first and last lines for each group of differences.
- /U – Use this switch to compare files as Unicode text files.
- /L – This will compare your files as ASCII text.
- /N – This switch can only be used with ASCII but it will show all the corresponding line numbers.
- /LBn – Replace the “n” with a number to limit the amount of consecutive different lines that FC will read before it will abort. The default, if you do not specify a number is 100 lines of mismatched text.
- /nnnn – Replacing the “n’s” here will tell FC that when it finds mismatched lines, it can only continue if it finds “n” consecutive matching lines after the mismatch. This is useful if you want to prevent two files from becoming extremely out of sync.
- /T – This switch will tell FC not to expand tabs to spaces.
- /W – If you use this switch, FC will compress white space (tabs and spaces) during its comparison of your files.
There is only one parameter you will need to specify, but you will need to enter two instances of it. This is the Pathname parameter in which you will state the location of your files.
FC’s Syntax
Like every tool in command prompt, you will need to know how to enter your commands with the proper syntax. There are two main options for the File Compare tool that you can use. If you want to compare two sets of files instead of two individual files, you can use wildcards (? and *).
Depending on your command, you will receive one of four %errorlevel% responses.
- -1 – Your syntax is incorrect.
- 0 – Both files are identical.
- 1 – The files are different.
- 2 – At least one of the files can’t be found.
Let’s Practice
Before we get started, you should download our three sample text documents which we will use for the test. These documents each contain a paragraph of text with a few similar word groupings. Once you have downloaded these three documents, you can copy them into any folder on your computer. For the purposes of this tutorial, we will put all the text documents on the desktop.
Now you will need to open up an elevated command prompt window. Open the start menu in Windows 7 and 10 or open the search function in Windows 8 and search for CMD. Next, right-click on it and then press “Run as administrator.” While you don’t need to open an elevated command prompt window, it will help you to avoid any pesky confirmation dialog boxes.
Our tutorial today will cover several simple scenarios which will be elaborated on below.
- Compare two text files in the same folder using File Compare.
- Compare files in the same folder using File Compare using the “/lbn” switch.
- Compare two identical files.
- Perform a binary comparison of two different files and two identical files.
Scenario 1 – Compare two text files using File Compare.
Now that you have your command prompt window open and you have your text files on your desktop, we are ready to do a simple file comparison. In this section, we will do a basic comparison, and then add a few different options. Begin by entering the following command to compare the contents of “FCsample” and “FCexercise.” Remember to substitute the pathname with the name that matches your computer, and remember that command prompt is not case sensitive.
In this case, all the text from both documents is shown because they do not match up properly.
Scenario 2 – Compare files in the same folder using File Compare using the “/lbn” switch.
Now, let’s try another comparison in which we will tell FC to stop after 2 lines of mismatched data. Do this, by adding the “/lbn” switch.
As you can see, you receive an error message which says “Resync Failed. Files are too different.” This is because there are more than two consecutive lines of mismatched data. Try changing the numbers or editing the files yourself and play with the file compare tool to see what results you get.
Scenario 3 – Compare two identical files.
In the files you downloaded, you will see two files called “FCexercise” and “FCexercise2.” These two files have exactly the same content, so we will perform a comparison and see what results we get.
As you can see from the image above, FC reports that no differences were encountered. If you were to edit one file, add a single letter, and try the command again, your results would show up as in the image below. Note that the only thing that was changed was the addition of the letter “a.”
Scenario 4 – Perform a binary comparison of two different files and two identical files.
Eme program downstate volleyball club. For this example, we will be performing a binary comparison of the “FCexercise” and “FCsample” files.
You will notice that the function begins by informing you that the two files are being compared. Next, a bunch of binary digits scroll by, which is where the files are being compared, side by side, and lastly, you receive a report that says that, FCexercise is longer than FCsample. For this next example, we will be performing a binary comparison of the “FCexercise” and “FCexercise2” files.
In this comparison of two identical files, FC reports that there is no difference between the two files. Now that you know the basics of the FC tool, feel free to play with the switches and test out some new ideas. Remember that while you are playing with files, it is best to use dummy samples like the ones provided here, in order to avoid any accidental data loss.
Image Credit: Nikki on Flickr
READ NEXT- › Six Common Smarthome Mistakes Beginners Make
- › What Can I Do with My Old iPhone?
- › How to Stream UFC 242 Khabib vs. Poirier Live Online
- › Free Download: Microsoft’s PowerToys for Windows 10
- › How to Overclock Your Computer’s RAM
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:
Compare File Name In Vbscript
$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'
$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 scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy