How to diff two folders from a Windows command prompt

In most cases where I need to compare two folders recursively on a Windows system I use my go-to tool Beyond Compare. It is an excellent utility, and one that I think should be among the first utilities any developer should install on a new machine.

However, today I was doing a reconciliation as part of a very large file migration project that required comparing two folders that each contained hundreds of millions of files spread across thousands of sub-folders. BC was having a lot of trouble and choked on many of my comparisons. It just wasn’t the tool for today’s job. I needed another solution.

Necessity mothered some invention and I found an inventive way to use a combination of command switches on  RoboCopy to perform the comparison. If you are not familiar with RoboCopy, and you do a lot of mass copying of files, you need to stop what you are doing and learn about it pronto. It is a supercharged version of XCopy that has been included with Windows since Vista. It has a ton of great features such as multi-threaded file copying, selectively copying changed files, and resumable copies that make it a must especially for big file copy jobs over flaky network connections.

Diff Command Using RoboCopy

So here’s the command to perform a basic comparison of two folders and write a log file listing the differences.

ROBOCOPY “\\FileShare\SourceFolder” “\\FileShare\ComparisonFolder” /e /l /ns /njs /njh /ndl /fp /log:reconcile.txt

Explanation of the command switches used above:

/e  Recurse through sub-directories (including empty ones)
/l  Don’t modify or copy files, log differences only
/fp  Include the full path of files in log (only necessary if you omit /ndl)
/ns  Don’t include file sizes in log
/ndl  Don’t include folders in log
/njs   Don’t include Job Summary
/njh   Don’t include Job Header
/log:reconcile.txt   Write log to reconcile.txt (Recreate if exists)
/log+: reconcile.txt   (Optional variant) Write log to reconcile.txt (Append if exists)

Usage Notes and Warnings Regarding the /NDL Option

The /NDL option is a handy way to suppress the inclusion of every folder checked (regardless of whether it contains differences) in the log, but there because of the way it works it is not a good idea in all circumstances. Consider the following before you use /NDL.

  • Folders that exist only on source or destination are not logged unless at least one mismatched file is present or a source file is missing on destination.
  • Folders that exist only on the destination are not logged at all regardless of contents.

If you omit the /NDL option, it is necessary to include the /FP option if you want full paths listed for each file.

Example Output

(with /NDL option)

*EXTRA File         c:\dest\log.txt
New File              c:\source\newfolder\Blah.txt
Newer                 c:\source\Files\CONCORD.DAT
New File              c:\source\Files\COWCO.DAT

(without NDL Option)

 c:\work\test\source\    (extraneous folder listing)
*EXTRA Dir      c:\dest\newfolderdest\
*EXTRA Dir      c:\dest\newfolderrestempty\
*EXTRA File     c:\dest\log.txt
New Dir           c:\source\newfolder\
New File          c:\source\newfolder\Blah.txt
New Dir           c:\source\newfolderempty\
c:\source\Files\   (extraneous folder listing)
Newer             c:\source\Files\CONCORD.DAT
New File          c:\source\Files\COWCO.DAT
c:\test\source\FilesSame\   (Included despite no diffs)

Windows Console Tricks and Shortcuts

Over the past few days I have been doing a higher than usual amount of work using the Windows console interface. I’ve been at this computing thing for quite some time, easily long enough to be old friends with the venerable DOS style command line, but not so much that I have yet figured out its cryptic emerald emoticon C:\>.

Today, my task involved a considerable amount of copying and pasting to and from the command line. As I am sure you are also painfully aware, the keyboard shortcut Ctrl-v (paste) don’t work at the command line unless your are looking for a shorthand way to enter the 2 character combination “^V” with only the press of two keys. For the first 20 times or so, I simply resorted to my standard approach for cut/paste operations. That is, using the menu from the application icon in the top left corner of the console window as shown here.

Tried and True

Tried and True

This approach is incredibly slow, but I don’t usually do very many clipboard operations at a command line, so I have always just sucked it up and lived with it. Today, however, it was getting extremely tedious and frustrating.

Fed up, I quested a way to use shortcut keys in the console interface for accessing the clipboard. I am sorry to report that I didn’t find it, but did find something almost as good.

Mousing the C prompt? Who Knew?

I don’t know how I didn’t discover this sooner, but you can right-click anywhere on the console window and get a shortcut menu. I suppose the concept of using a mouse at a command line just didn’t occur to me. In any event, using this technique is definitely quicker that my old method because it requires less menu navigation to get to the useful commands.

The copy/cut commands are still a little nonstandard and clumsy given that you must go into “mark” mode before you select the text. Also, marking multiline information is a bit quirky.

Right Clicking in the Console Window Opens a Shortcut Menu

Right Clicking in the Console Window Opens a Shortcut Menu

The “Find” command on this menu is another nice little feature that I had overlooked before. It has two interesting quirks

  • After an unsuccessful find, it emits an extremely loud beep from the built in computer speaker.
  • After a successful find it turns on “Mark” mode to prepare for a copy.

Command History

Most are familiar with using the up/down arrow keys to scroll through previous commands, but how many of you knew you could get a menu of recently used commands by pressing F7? You can reset this history by hitting Alt-F7.

Pressing F7 Opens a Menu of Recently Used Commands

Pressing F7 Opens a Menu of Recently Used Commands

Here are a few more useful keyboard shortcuts that are available in the windows command shell:

  • Up/Down Arrow Keys: Iterate back/forward through command history
  • F1: Paste the previous command one character at a time.
  • F2 (then enter a character):Pastes the previous last command up to, but not including, the first occurance of the provided character.
  • F3: Pastes the previous command in its entirety.

Use Wild cards with the CD command

You are probably used to using wild cards with delete and copy commands, but don’t think about using them to speed up directory navigation.  It can be a real time saver to type “cd doc*” instead of “CD documents and settings.”

You can even use this techniqe when navigating down more than one level, for example “CD win*\system3*” to replace “CD Windows\System32.”

Note:You can’t use a wildcard as the first character with CD for some reason.

Am I the only one who didn’t know about this?

I started to wonder if I was just dense and had missed this feature that everyone in the world knew about except me. I did a quick poll of my peers and discovered that about half of them knew about it, and the other half didn’t know about using my old technique. Out of curiosity, I’d like to see what my readers knew before reading this article.

Do you have any cool command-line tricks to add?

Put them in the comments and I’ll add the good ones to this post.

Further Reading

Quandry: How to get a screen-shot of the Windows login prompt.

The Dilemma

I was working on some documentation recently and needed to grab a screen-shot of the Windows login prompt for an exhibit in a manual. I quickly realized that my normal techniques for capturing screen-shots were impractical.

Method 1: Windows Built in Screen Capture (Failed)

Unless you have been living in a cave, or are new to Windows, you are probably familiar with this technique. Press the PrintScrn key to copy a bitmap of the desktop to the clipboard, or use AltPrintScrn to capture just the active window. It’s a simple and free solution for screen-shots. Here is a good reference, if you would like more information on this approach.

The problem is that it doesn’t work from the login prompt, probably as part of the security mechanism of Windows that correctly doesn’t allow unauthenticated users to monkey with anything including the clipboard.

Method 2: Screen Capture Software (Failed)

The problem with using any screen capture software is that it has to be running and accepting commands (usually via hot keys) to take a picture of what you see on the screen. Because you can’t run an interactive user program before you log in to Windows, this method is dead-on-arrival. I am unaware of any screen capture software that offers a workaround for grabbing the login prompt.


 The Solution

After experimenting with a few ideas, I arrived at a solution that works pretty well. As with most things, the answer was quite simple, it just required some sideways thinking about the problem.

Step 1:Using terminal software, such as Windows Remote Desktop, open a connection to another machine, but don’t go into fullscreen mode as shown here.  And yes, I appreciate the irony of using a screen-shot to demonstrate taking screen-shots.

Using Remote Desktop to get a screen-shot of the login prompt.

Using Remote Desktop to get a screen-shot of the login prompt.

Step 2 (option A): Grab the screen-shot of using the PrintScrn approach. The AltPrintScrn technique, unfortunately, will grab the whole remote desktop window and not just the login dialog, so you will need to do some cropping in a graphics package to get down to just the dialog.

Step 2 (Option B): Take the screen-shot with your screen-capture software, which should allow you to only capture the part of the screen with that contains the dialog.

That’s it! The consumer of your screen-shot  will be none the wiser that they are looking at a capture from  a nested window unless you do a really shoddy job on the cropping.