356

I know that there is a post similar to this : here.
I tried using the comp command like it mentioned, but if I have two files, one with data like "abcd" and the other with data "abcde", it just says the files are of different sizes. I wanted to know where exactly they differ. In Unix, the simple diff tells me which row and column, the comp command in windows works if I have something like "abd" and "abc". Not otherwise. Any ideas what I can use for this?

Community
  • 1
  • 1
wittythotha
  • 3,956
  • 4
  • 19
  • 18

13 Answers13

556

Run this in the CMD shell or batch file:

FC file1 file2

FC can also be used to compare binary files:

FC /B file1 file2
Andriy M
  • 76,112
  • 17
  • 94
  • 154
  • 3
    How do you do this with all changed files in two folders? Like, I compare folder1's contents to folder2's contents: any changed lines in folder2 show up? – Wolfpack'08 Jan 19 '14 at 07:05
  • 21
    @Wolfpack'08": `FC path1\* FC path2\*` would compare all the files in the folders `path1` and `path2`, provided they (the files) have identical names. If there are files with mismatching names, FC would report a missing file, but only for the first folder's files that aren't found in the second folder and not the other way round. To catch all the mismatches, a different technique would probably be needed. – Andriy M Jan 19 '14 at 11:25
  • @AndriyM So, new files would cause FC to error out without returning any actual lines? Only the error message? – Wolfpack'08 Jan 19 '14 at 22:22
  • 1
    @Wolfpack'08: Yes, just the message about the missing counterpart, no lines from the existing party. (But that wouldn't terminate processing of other, existing, file pairs and their comparison would still be carried out, if that's what you were asking.) – Andriy M Jan 20 '14 at 02:15
  • @AndriyM So, you mean to say: "If error: `files in both directories are diff'd; files in only one directory throw errors.`" Interesting. But surely there's a 3rd-party program equivalent to diff? – Wolfpack'08 Jan 21 '14 at 00:27
  • @Wolfpack'08: No, if there are files with identical names, they are compared line by line normally and differences are reported accordingly; if a file in the first dir doesn't have a match (a file with the same name) in the second dir, a missing file is reported; finally, the second dir's files that have no matches in the first dir aren't reported in any way. Re: diff equivalent – well, this is what this question was about :) and you can see alternative suggestions in other answers. – Andriy M Jan 21 '14 at 06:15
  • I tried it in a command shell on Windows 10 and it does not recognize 'fc' – user118967 Jul 08 '18 at 02:15
  • @user118967: It has worked for me in all Windows versions I've used, including [Windows 10](https://i.imgur.com/jKJNfDd.png). I don't know, maybe old command-line utilities are starting to be excluded from newer releases of Win 10 if you install it from scratch, but they certainly weren't deleted for me in the course of upgrades, whether major or minor. My Win 10 was installed a long time ago, and `fc` & co. have been working for me from day one. – Andriy M Jul 08 '18 at 22:21
  • I had to do `fc.exe f1 f2` on Windows 10 powershell. Note the addition of the `.exe`. – Austin Cory Bart Aug 07 '19 at 18:38
  • `FC` is the correct command on Windows, unfortunately it has a horrible bug and it can't be used with files containing lines longer than 127 characters – stenci Jan 13 '22 at 20:51
  • In Command Prompt I can't scroll up to see the rest of the FC output, any tips ? – Cheetaiean Aug 02 '22 at 21:21
  • 1
    @Cheetaiean: You have some options here. (1) You can increase Command Prompt's screen buffer height to fit the entire output. (2) You can redirect the output to a file (`FC ...your params... >filename`) and then open it with e.g. Notepad (any plain text editor will do). (3) You can pipe the output to `MORE` (`FC ...your params... | MORE`) and then view the output page by page or line by line. You could also try submitting this as a question (not sure if here would be appropriate, perhaps [su] would be more suitable) to see if there are better alternatives. – Andriy M Aug 05 '22 at 23:04
40

Well, on Windows I happily run diff and many other of the GNU tools. You can do it with cygwin, but I personally prefer GnuWin32 because it is a much lighter installation experience.

So, my answer is that the Windows equivalent of diff, is none other than diff itself!

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    The base install of Cygwin does not seem to include diff. What additional packages do I need to install to get it? – Warren Dew Apr 03 '15 at 17:42
  • 11
    I don't recommend cygwin. I'd use a native Win32 port of diff – David Heffernan Apr 03 '15 at 17:49
  • 1
    @WarrenDew The Cygwin package you need is `diffutils`. See https://cygwin.com/cgi-bin2/package-cat.cgi?file=x86_64%2Fdiffutils%2Fdiffutils-3.3-2&grep=diff – Steve Pitchers Jan 12 '16 at 15:35
  • 1
    now with Windows Subsystem for Linux (WSL, aka Bash for Windows) it's even easier to get and use diff, if on Windows 10 – Zim May 20 '21 at 21:37
29

Another alternative is to download and install git here. Then you have 2 options:

Option 1 (preferred)

Use diff from Git Bash(installed from above) and diff happily like a native *nix user on Windows.

Option 2

Add the path to Git\bin\ to your PATH variable. This will give you not only diff, but also many other linux commands that you can use from the windows command line.

You can set the PATH variable by right clicking on Computer and selecting Properties. Then you can click on Advanced System Settings on the left side of the screen. In the pop up, click Environment Variables and then either add or update the PATH variable in your user variables with Git\bin\

Git diff documentation

jumping_monkey
  • 5,941
  • 2
  • 43
  • 58
Tim Hutchison
  • 3,483
  • 9
  • 40
  • 76
  • @firelight - thank you for your answer. Can you elaborate what you mean here: "Then, add the path to Git\bin\ to your PATH variable." – BenKoshy Feb 12 '16 at 05:41
  • 2
    @BKSpurgeon - If you right click on Computer and select Properties then you can click on Advanced System Settings on the left side of the screen. In the pop up, click Environment Variables and then either add or update the PATH variable in your user variables with Git\bin\ – Tim Hutchison Feb 12 '16 at 17:03
  • @JustinCormack Do you have any references for this? I just tried diff in Git Bash and it worked – Tim Hutchison May 29 '18 at 17:25
  • 3
    @Tim Hutchison, from your answer: "This will give you not only diff, but also many other linux commands that you can use from the windows command line." - which ones do you mean? When one installs Git, he/she should have just `git...` executables available from the PATH. Moreover, the answer should probably include one important piece of information: that you need to launch the diff tool by writing `git diff`, not just `diff`. – Petr Bodnár Sep 15 '18 at 07:43
  • dude, that's totally another `diff` – vintprox Jan 17 '20 at 08:15
29

Winmerge has a command line utility that might be worth checking out.

Also, you can use the graphical part of it too depending on what you need.

Robert Greiner
  • 29,049
  • 9
  • 65
  • 85
  • 3
    Although this is not a built-in Windows utility, I highly recommend it - it is open-source, light-weight, and in my experience bug-free. – david.barkhuizen May 09 '14 at 09:51
16

FC works great by in my case it was not helpful as I wanted just the lines that are changed. And FC give additional data like file name, same lines and bilateral comparison.

    >fc data.txt data.txt.bak   
    ***** DATA.TXT
    ####09
    ####09
    ####09
    ***** DATA.TXT.BAK
    ####09
    ####08
    ####09

but in my case I wanted only the lines that have changed and wanted those lines to be exported to different file, without any other header or data.

So I used "findstr" to compare the file :

findstr /V /G:data.txt.bak data.txt >DiffResult.txt

where :

data.txt.bak is the name of old file

data.txt is the name of new file

DiffResult.txt contains the data that is changed i.e just one line ####09

Nimish Choudhary
  • 2,048
  • 18
  • 17
  • 1
    **/A** only first line and last line of block that is changed **/N** show line numbers – Lukas Dec 02 '15 at 10:13
15

There's also Powershell (which is part of Windows). It ain't quick but it's flexible, here's the basic command. People have written various cmdlets and scripts for it if you need better formatting.

PS C:\Users\Troll> Compare-Object (gc $file1) (gc $file2)

Not part of Windows, but if you are a developer with Visual Studio, it comes with WinDiff (graphical)

But my personal favorite is BeyondCompare, which costs $30.

Paul Williams
  • 3,099
  • 38
  • 34
10

fc. fc is better at handling large files (> 4 GBytes) than Cygwin's diff.

nerdfever.com
  • 1,652
  • 1
  • 20
  • 41
4

The reason you getting the error with COMP is that the utility assumes the files that you are comparing are of the same size. To overcome that you can use th '/n' option with which you can specify the number of lines you want to compare. (see the options supported by comp by typing 'comp /?' on the command line. so your command would look like :

C:\>comp "filepath1" "filepath2" /a /l /n=(the number of lines you want to compare) /c 

This should solve your problem if you wanna stick to using COMP. But this will be a problem for really large files.

Though comp is an option, but I feel it is primitive and FC is a better option. you can use FORFILES and FC together to probably make a really good filecompare utility if you require one on a frequent basis.

FC is used this way for ref:

C:\>fc /c(case insensistive) /lbn(number of errors allowed before you wanna stop compare) /n(display line number) "filename1" "filename2"

there are many options available which you can see by 'fc /?' hope this helps

proximace
  • 41
  • 1
4

DiffUtils is probably your best bet. It's the Windows equivalent of diff.

To my knowledge there are no built-in equivalents.

mattkelly
  • 626
  • 3
  • 9
  • I used that too, however, today I discovered that the pretty old version 2.8.7 that is provided on that page has a bug (when comparing certain PDF files they are reported as equal). So now preferring the "git diff" as suggested by firelight. – anre Oct 27 '16 at 16:35
3

I've found a lightweight graphical software for windows that seems to be useful in lack of diff command. It could solve all of my problems.

WinDiff http://www.grigsoft.com/download-windiff.htm

Ahmad
  • 5,551
  • 8
  • 41
  • 57
  • thanks! I have to click on the file names in an overview before it actually shows the difference in detail. It shows a unified view and does not offer a side-by-side view of both compared files. Which makes it hard to associate which version is in which file. – klaus thorn Nov 18 '21 at 15:07
0

The windows equivalent to the diff command is the fc (File Comapre) command.

Here are the basic steps to do so:
1. Keep the two files in a folder (Example file1.html and file2.html)
2. Launch command prompt
3. Type fc file1Location file2Location

Have found a detailed tutorial on the same:

http://www.howtogeek.com/206123/how-to-use-fc-file-compare-from-the-windows-command-prompt/

Vishal Kotak
  • 422
  • 4
  • 11
0

I don't know if the following tool is exatly what you need. But I like to use, for specific files, some online tool. This way I can use it regardless of the operational system. Here is a example: diffchecker.com

But for my needs, I guess the best tool to track changes and logs of my project's files is GIT. If you work in a team, you can have some repo online in a server of yours, or use it with Bitbucket or Github.

Hope it helps somebody.

Pedro Acácio
  • 139
  • 1
  • 5
0

If you have installed git on your machine, you can open a git terminal and just use the Linux diff command as normal.

falsePockets
  • 3,826
  • 4
  • 18
  • 37
  • You don't need `git` to use `diff`. Or did you mean to write `git diff` where you wrote `diff`? – Jeff Holt Mar 10 '22 at 15:05
  • You don't need it, I just meant that if you happen to have it installed already, then you don't need to install anything extra for `diff`, as long as you use the `git bash` shell. – falsePockets Mar 13 '22 at 21:23