Compare two files in Visual Studio

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Compare two files in Visual Studio



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?





Just as comment for further use: If you are working with vs2015, use this link: visualstudio.com/en-us/docs/tfvc/compare-files
– Elnaz
Apr 9 '17 at 12:08





Note this is still applicable for VS 2017. Hence, I have provided an answer how to deal with this issue comfortably.
– Matt
Aug 23 '17 at 15:47







None of the answers can compare two files already opened in Visual Studio.
– user3454439
May 8 at 6:39




16 Answers
16



You can invoke devenv.exe /diff list1.txt list2.txt from the command prompt or, if a Visual Studio instance is already running, you can type Tools.DiffFiles in the Command window, with a handy file name completion:


devenv.exe /diff list1.txt list2.txt


Tools.DiffFiles



Using Tools.DiffFiles from Visual Studio Command window





Does this diff tool provide any useful feature like copy to left, copy to right, etc? When I diff working copy with the server version I sometimes wish to undo some of the changes and has to do manual copy pasting.
– Samuel
Feb 12 '14 at 10:24





Why is this feature hidden from the VS UI ? This is the easiest and effective way of comparing two files...
– eka808
Feb 14 '14 at 15:23





If your path has spaces, you can wrap either the entire path or just the part part with spaces in quotes.
– pennstatephil
Jun 24 '14 at 15:16





For those of you who are new to Visual Studio the Command Window can be opened by going to: View > Other Windows > Command Window (VS Pro 2013) or with CTRL+ALT+A
– Cleanshooter
Jun 26 '14 at 13:30





such a shame VS doesn't have a menu item for this very basic functionality. many IDE's/ editors do.
– dewd
Sep 16 '14 at 9:09



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



enter image description hereenter image description here





I installed the extensions, but this option isn't available for me.
– PeterX
Jan 7 '16 at 0:57





VSCommands 14 add neither of the items on the screenshot in VS 2015 Update 1 Community
– mtman
Feb 15 '16 at 18:44





There is not update for this extension on vs 2017!!
– Reza M.A
Jul 7 at 8:03



I have always been a fan of WinMerge which is an open source project. You can plug it into Visual Studio fairly easily.



http://blog.paulbouwer.com/2010/01/31/replace-diffmerge-tool-in-visual-studio-team-system-with-winmerge/



will show you how to do this





Also able to diff folders - very powerful
– Jaans
Dec 3 '15 at 11:02





The only disadvantage of WinMerge is its limited ablitiy to recognize changes (such as whitespace, lines divided into more lines, etc)
– user586399
May 18 '16 at 7:58




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 the "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.



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 Studio2017EnterpriseCommon7IDE
start "Compare files" /B /MIN "%vspath%devenv.exe" /diff %2 %1 First:'%2' Second:'%1'



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


%1


%2



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).


VS_FileCompare.cmd


vspath


devenv.exe



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!


VS_FileCompare.cmd



Usage:



Drag and drop them as shown in the animation below:
DragDropDemo



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



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.



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:


shell:sendto



Usage:



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:
SendTo



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



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





This is really a very good and elaborated answer, definitely should be voted up. I am from the group who keeps looking for out of the box answer even if answer is marked in a thread so i am absolutely giving it heads up.
– Div Tiwari
Feb 16 at 17:48





@DivTiwari - Thank you, glad to hear that!
– Matt
Feb 19 at 11:32





Edit: This is the script for VS 2013, thanks again. @echo off setlocal set vspath=C:Program Files (x86)Microsoft Visual Studio 12.0Common7IDE start "Compare files" /B /MIN "%vspath%devenv.exe" /diff %2 %1 First:'%2' Second:'%1'
– Fernando Aguilar
May 11 at 15:55





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...





NB - this extension is only for VS2012, but you can edit the manifest file to support VS2013 easily enough - see my comment on the Q & A tab of the extension for details. (I wanted this feature, but can't use the free version of VSCommands at work due to it's licence)
– James S
Jul 1 '14 at 11:30







Visual Studio Comparison Tools is available for 2013, visualstudiogallery.msdn.microsoft.com/…
– Aligned
Jul 18 '14 at 16:26



In Visual Studio 2012, 2013, 2015, you can also do it with Web Essentials, just right click the files and from context menu > Web Essential >> Diff selected files:



Edit: It's now available as a separate extension



enter image description here





I don't see Diff selected files or Compile Markdown to HTML in Web Essential - VS 2015
– usefulBee
Nov 3 '16 at 18:51





I have installed new version for VS 2015 but I am not able to see Diff selected files option why ?
– stom
Aug 25 '17 at 12:35


VS 2015


Diff selected files





@stom install the extension I mentioned.
– Hamid Mosalla
Oct 1 '17 at 1:27



If you have VS installed, you could also call


"%VS110COMNTOOLS%..IDEvsdiffmerge.exe" "File1" "File2"



or for VS 2013


"%VS120COMNTOOLS%..IDEvsdiffmerge.exe" "File1" "File2"



Source: http://roadtoalm.com/2013/10/22/use-visual-studio-as-your-diff-and-merging-tool-for-local-files/





Thanks worked perfectly for me. c:temp>"%VS120COMNTOOLS%..IDEvsdiffmerge.exe" 1256.txt 1256_PROD_Original.txt
– Craig B
Mar 16 '15 at 1:20





This is the no install, extension or TFS method for VS2017


VS2017



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


Ctrl+Alt+A


Tools.DiffFiles File1 File2





This is the same as another answer
– niico
Jun 6 at 1:08





@nico no, no its much more concise and immediately useful than the other answers.
– patrick
Jun 6 at 2:42







Maybe - though personally I'd rather use a GUI to select the files than type have to construct two paths in any case - I mean it's not 1984.
– niico
Jun 6 at 13:23





@nico I agree. Its very aggravating that the question even needed to be asked. It should be a context menu right click on a file item.
– patrick
Jun 6 at 13:31



I had this problem as well. No TFS, but I found this article helpful.



Specifically, step 1b.



Open a Visual Studio command prompt and navigate to the Common7/IDE folder and type


tf diff /configure



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



In Visual Studio Code you can:


Explorer


Select for compare


Compare with '[NAME OF THE PREVIOUSLY SELECTED FILE]'



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



enter image description here



enter image description here



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:UserslivingstonDownloadswawaFilespreTest.js" "C:UserslivingstonDownloadswawaFilesProdTest.js" "C:UserslivingstonDownloadswawaFilespreTest.js" "C:UserslivingstonDownloadswawaFilesoutputsamp.js" /t /m



enter image description here




Step 3: Merge the files



enter image description here
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.



Visual Studio code is great for this - open a folder, right click both files and compare.



For VS 2017 Install
https://marketplace.visualstudio.com/items?itemName=vs-publisher-457497.FileComparer2017



The problem is that you can't compare files in diferent proyects, but you can copy the files in the same project to compare...



Context Menu



File Diference



I believe this to be one of the better extension for Visual Studio 2012, it's called Code Compare and can be found here.



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 - enter image description here



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.





If you read the original post, he specifically says, "Is there a way how can I just compare two files with builtin feature in VS but without TFS?" Your answer requires him to have TFS.
– CigarDoug
Sep 9 '15 at 12:47





though he attempted to answer .. although, his attempt was wrong +1
– Irfan
Nov 27 '17 at 7:29








By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

'Series' object is not callable Error / Statsmodels illegal variable name