[visual-studio] Compare two files in Visual Studio



Answers

You can try VSCommands extension from Visual Studio Gallery. Latest release allows you to select two file and compare them:

Question

I saw new comparsion tool in VS 2012 for comparing two files or two versions of file. I like it. But when I tried to find it I can't because I don't use TFS. Is there a way how can I just compare two files with builtin feature in VS but without TFS?




If you are working with TFS connected then right click on file which you need to compare (through source control explorer) and it presents you a window like this -

Now change path of source file in 'Souce Path:' and you get comparision through VS comparision tool.

Similarly you can compare folder also which compares all files of a folder at once.




File Comparer VS Extension by Akhil Mittal. Excellent lightweight tool that gets the job done.




To compare any two files and merge it to one file Here are the following steps you can follow if you have visual studio(Any version) installed.

Step 1: Open Visual studio command prompt. If you do not find visual studio command prompt then choose visual studio tools

Start -> Visual studio command prompt

Step 2: Enter the command vsdiffmerge.exe

Ignore the switch /m if you need just comparison.

Syntax 1:
vsdiffmerge <file1> <file2> <file1> <outputfile> /t /m

Syntax 2:
vsdiffmerge <basefilename> <CompareFilename> <basefilename> <OutputFilename> /t /m

Example 1:
vsdiffmerge test1.js test2.js test1.js output.js /t /m
Example 2:
vsdiffmerge.exe "C:\Users\livingston\Downloads\wa\wa\Files\pre\Test.js" "C:\Users\livingston\Downloads\wa\wa\Files\Prod\Test.js" "C:\Users\livingston\Downloads\wa\wa\Files\pre\Test.js" "C:\Users\livingston\Downloads\wa\wa\Files\output\samp.js" /t /m


Step 3: Merge the files

Please note that if file name does not exists in the location, it will not open the comparer.

Also you can beautify the file before you do the comparison. In visual studio Ctrl + K + D.

There are lot of beautifier sites available online.




NO install, NO extension, NO TFS for VS2017.

Ctrl+Alt+A (View->Other Windows->Command Window)

Tools.DiffFiles File1 File2



Inspired by the accepted answer above, I found a very comfortable way how you can instantly compare two files with Visual Studio by using drag and drop or via "Send To" context menu. It only requires a little preparation which you need to do once and then it is useful like a Swiss army knife.

File compare using drag & drop

Preparation:

1. Create a new batch file using your favorite text editor. Type the following:

@echo off
setlocal
set vspath=C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE
start "Compare files" /B /MIN "%vspath%\devenv.exe" /diff %2 %1 First:'%2' Second:'%1'

You might notice that I have reversed %1 and %2 parameters in the batch, this is because I noticed that the file explorer passes the 2nd file first, then the 1st file.

  1. Save this code as VS_FileCompare.cmd to use it, modify vspath if required to match the location of devenv.exe (depending on the Visual Studio version you're currently using).

  2. Either create a shortcut named "File Compare" for VS_FileCompare.cmd and place it on the desktop (as used in the animation below), so it is always available to drag & drop files onto it or directly place the batch file on the desktop. That's all!

Usage:

  1. Open the Windows explorer via Win + E
  2. Select two files to compare in the explorer
  3. Drag and drop them as shown in the animation below:

  4. After a few seconds (depending on the launch time of Visual Studio), the results will be shown in Visual Studio:

Note: It does not harm if Visual Studio is already open. In this case it will just open up a new window within the running instance of Visual Studio. So you can compare multiple file pairs, but please ensure you have selected only 2 files at a time.


Alternative way: SendTo context menu

Here's an alternative how you can use the batch file. It allows to use the context menu's Send To folder to compare the files.

Preparation:

  1. Place the shortcut into the Send to folder. Open the Windows explorer via Win + E
  2. Open the Send To folder by entering shell:sendto into the file explorer's address bar (as described here). Then, put the prepared shortcut into this folder.

Usage:

  1. Open the Windows explorer via Win + E
  2. Select two files to compare in the explorer
  3. Assuming the shortcut is named "Compare2Files VS", you can select the two files, right-click and select Send To --> Compare2Files VS to invoke the compare as shown below:

  4. After a few seconds (depending on the launch time of Visual Studio), the results will be shown in Visual Studio:


MSDN References:
- Usage of diff window
- Visual Studio's Diff parameter




There is also a Visual Studio extension called CompareFiles, which does nothing else but adding the "Compare Files" entry to the solution explorer context menu. It invokes the built-in Visual Studio diff tool.

Just in case that someone (like me) doesn't want to install an all-in-one extension like VSCommands...




Related