I have 2 folders with files. These files are the product of some build process. The first folder is the outputs of previous build while the latter is the output of the current build.

I would like to somehow diff these 2 folders from the command line/build script and copy only files that were added or files that are new into some other folder.

For example:

**FOLDER1** (new build)
b.txt (modified)

**FOLDER2** (prev build)

Based on this, the copy operation i am interested in, will copy only b.txt and d.txt into some other folder (not into folder2).

Basically, i am only after the "deltas" and i would like to store these some place.

Is this possible using standard command line tools that come with Windows? (suppose Windows 7/Server 2008 or higher)

  • This is a long-roundabout way to do it, but maybe just xcopy with the date flag set. Though, if b in F1 is newer than newfolder, it will copy and if b in F2 is newer than in F1 - it will copy too. So with large filesizes, it degrades quickly (hence the non-answer). It may be worth researching and doing some logic for a .bat with xcopy /c/h/e/v/y/d – nerdwaller Dec 17 '12 at 15:22
  • i am not after copying the deltas to FOLDER2. I want to create a folder containing ONLY THE DELTAS (copy them to FOLDER3 somehow). – liortal Dec 17 '12 at 15:41
  • That's what I suggested... You would first check Folder1 vs Folder3 then Folder2 vs Folder 3. You would always have the most recent revision in Folder3 of all files. However, as I said, with large file sizes this is extremely inefficient - so I did not post it as an answer. – nerdwaller Dec 17 '12 at 15:45
  • Folder3 is always initialized as empty. I want to compare F1 and F2, and copy newest stuff to F3. F3 at the end would contain only the latest stuff. – liortal Dec 17 '12 at 15:55
  • If you compare F1 to F3 and F2 to F3, you essentially do that, since if F3 is empty you get all of F1 then you could compare F2 to F3 (i.e. F1) and you'd have the latest. Again, very inefficient - so it's not an answer... – nerdwaller Dec 17 '12 at 16:00

Use XCOPY with /D and /L options, "copying" from folder 1 to folder 2.

The /D option restricts the copy to only new or modified files.

The /L option causes the command to list the files that would be copied, but prevents any copies from actually being made.

Then use FOR /F to process those results and actually copy the files to folder 3.

for /f "eol=: delims=" %F in ('xcopy /dl "folder1\*" "folder2"') do @copy "%F" "f3" >nul

If put in a batch file, then all % must be doubled as %%.

The XCOPY also prints out a summary of how many files are (would be) affected. The above will attempt to copy a file with the summary count "name", and of course fail. The redirection of output to nul prevents the error message from displaying. I tested on Windows 7 and was surprised the error message was on stdout and not stderr. If the error message on other versions is appearing on stderr, then the command should also get 2>nul to prevent the error message.

But there is a risk in hiding the error messages - what if a copy fails for some reason?

Here is a script that will list all files that were successfully copied, as well as indicate which (if any) failed. It uses FINDSTR to filter out the unwanted file count. It assumes the file count is in English, with the form NNN File(s), so it would have to be modified for other languages.

@echo off
for /f "eol=: delims=" %%F in (
  'xcopy /dl "folder1\*" "folder2" ^| findstr /v "^[0-9][0-9]* File(s)"'
) do copy "%%F" "folder3" >nul && echo "%%F"|| echo FAILED: "%%f"

Here is a more complex variant that works with any language. It saves each file name to a variable and delays the copy until the next loop iteration. The last entry (the count) never gets copied.

@echo off
setlocal disableDelayedExpansion
set "file="
for /f "eol=: delims=" %%F in ('xcopy /dl "folder1\*" "folder2"') do (
  if defined file (
    setlocal enableDelayedExpansion
    copy "!file!" "folder3" >nul && echo "!file!" || echo FAILED: "!file!"
  set "file=%%F"
  • Thanks, i guess i will go down with this approach or similar. I think XCOPY will still copy the files in case they have the same date though (f1 and f2 contain exactly the same files and dates -- all files will still be copied). – liortal Dec 18 '12 at 5:53
  • @liortal - Not quite. I believe it does exactly what you want. The file is copied only if either the file exists in f1 and not f2, or if the last modified timestamp in f1 is newer than in f2. If the timestamp is the same or older in f1, then the file is not copied. – dbenham Dec 18 '12 at 12:11
  • try to setup 2 folders with exact same files and copy using xcopy /d. From what i've seen it copies the files in this case. – liortal Dec 18 '12 at 13:13
  • I did that test case and it worked. I created F1 folder with files, and then copied those files to F2. Then I modified one file in F1. COPY /D only copies the modified file. – dbenham Dec 18 '12 at 14:40
  • What's the reasoning for not just using xcopy directly? If it can list the necessary files, can't it also copy the necessary files? – trlkly May 13 '14 at 20:11

I like ROBOCOPY instead. Used XCOPY for years then found a need for a restartable copy operation over a slow/twitchy internet connection.

ROBOCOPY src dest /s /e /xo

or mirror

ROBOCOPY src dest /mir

Use the Logging options to get your list for examination later.

ROBOCOPY src dest /s /e /xo /log:file

  • According to robocopy help, the /s option should be redundant if you use /e. Other than that, I think this is a good solution! – Rikki-Tikki-Tavi Jan 8 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.