How to compare two folders and copy missing files

$ tree . |-- dir1 | |-- file1 | |-- file2 | |-- file3 | |-- file4 | `-- file5 `-- dir2 |-- file2 |-- file4 `-- file5 2 directories, 8 files $ for f1 in dir1/*; do f2="dir2/${f1#dir1/}"; [ ! -e "$f2" ] && printf '%s\n' "$f2"; done dir2/file1 dir2/file3

This loops through all the names in the first directory, and for each creates the corresponding name of a file expected to exist in the second directory. If that file does not exist, its name is printed.

The loop, written out more verbosely (and using basename rather than a parameter substitution to delete the directory name from the pathname of the files in the first directory):

for f1 in dir1/*; do f2="dir2/$( basename "$f1" )" if [ ! -e "$f2" ]; then printf '%s\n' "$f2" fi done

If the files in the two directories not only have the same names, but also the same contents, you may use diff (note: BSD diff used here, GNU diff may possibly say something else):

$ diff dir1 dir2 Only in dir1: file1 Only in dir1: file3

If the file contents of files with identical names differ, then this would obviously output quite a lot of additional data that may not be of interest. diff -q may quiet it down a bit in that case.

See also the diff manual on your system.

For comparing deeper hierarchies, you may want to use rsync:

$ rsync -r --ignore-existing -i -n dir1/ dir2 >f+++++++++ file1 >f+++++++++ file3

The above will output a line for each file anywhere under dir1 that does not have a corresponding file under dir2. The -n option (--dry-run) makes sure that no file is actually transferred to dir2.

The -r option (--recursive) makes the operation recursive and -i (--itemize-changes) selects the particular output format (the >f and the pluses indicates that the file is a new file on the receiving end).

See also the rsync manual.

The script below represents one way you could approach the task (assuming your 3 directories share a common parent dir). As an aside, your initial attempt failed to handle the $nulls that occur when $Folder1Path or $Folder2Path are empty.

# Files in 1 but not in 2 should wind up in 3 with the same dir structure as occurs in 1 param( $Folder1path = "$PSScriptRoot\folder1", $Folder2path = "$PSScriptRoot\folder2", $Folder3path = "$PSScriptRoot\folder3" ) $ErrorActionPreference = "Stop"; Set-StrictMode -Version 'Latest' Get-ChildItem -Path $Folder1Path -Recurse | Where-Object { [string] $toDiff = $_.FullName.Replace($Folder1path, $Folder2path) # Determine what's in 2, but not 1 [bool] $isDiff = (Test-Path -Path $toDiff) -eq $false if ($isDiff) { # Create destination path that contains folder structure $dest = $_.FullName.Replace($Folder1path, $Folder3path) Copy-Item -Path $_.FullName -Destination $dest -Verbose -Force } }

Answer

  1. There is no built-in Windows 10 tool to compare files in two folders, but there are several third-party tools that you can use.
  2. One option is FileComparison.com, which has a free and a paid version.
  3. You can also use a file comparison tool such as FileZilla or WinDirStat.

How to compare two files and folders in Windows 10 | How to use WinMerge 2022 | Merge files, Folders

How to compare files and folders in Windows 10

How do I compare the contents of two folders in Windows?

To compare the contents of two folders in Windows, open the File Explorer and navigate to the folders you want to compare. Right-click one of the folders and select “Compare Contents.” The files and folders in the two folders will be displayed side by side.

Does Windows 10 have a file compare tool?

Windows 10 does not have a file compare tool.

How do you compare two folders and copy missing files?

There are a few different ways to copy files between folders:-Copy and Paste: This is the easiest way to copy files between folders. Simply select the files you want to copy, click the “copy” button, and paste the files into the destination folder.

-File Copy: You can also use File Copy to copy files between folders. Open File Copy, select the source and destination folders, and click “copy.

How can I find the difference between two folders?

There is no one definitive answer to this question. Depending on the operating system and file management program you are using, you may be able to use different methods to find the difference between two folders. For example, on Windows, you may be able to use the “dir” command to list the contents of both folders.

Where is Windiff EXE?

Windiff EXE is not a program, but rather a file. It can be found in the following locations:Windows 10: %APPDATA%\Microsoft\WindowsApps

Windows 8.

What is the best file comparison tool?

There are many file comparison tools available on the market.

How do I compare two files in tools?

There are a few ways to compare two files in tools:Use the “diff” command. This will compare the file contents and output any differences between the two files.Use the “file compare” tool. This will compare the file content, metadata, and permissions.

Use the “differences” report for a more detailed analysis of the differences between two files.

Is beyond compare free?

Beyond compare is a subscription-based software that offers users access to a variety of features, including tools to compare and contrast different files, folders, and drives.

How do I compare two files in VS code?

There are a few ways to compare files in Visual Studio Code. The simplest way is to use the “File comparison” extension:Open a file in one of the editors and then open the other file in the same editor.Select both files and choose File comparison from the menu bar.

The two files will be displayed side-by-side in the editor window, with differences highlighted in yellow.

How can I compare the contents of two files?

There are a few ways to compare the contents of two files. The simplest way is to use the diff command. diff will show you the differences between the two files and let you decide which file should be used as the source for a new copy. You can also use the cat command to view the contents of a file one line at a time.

How do I compare two folders in Total Commander?

The easiest way to compare two folders is to use the “Compare Folders” command. You can also use the “File Compare” dialog box.

Is WinMerge safe to use?

WinMerge is a safe and reliable tool for merging files. It has been tested and verified by the developers to be safe and effective.

How do I copy or replace only new files in Windows 10?

In Windows 10, you can copy or replace only new files by using the File Explorer. To copy or replace only new files: In File Explorer, select the files you want to copy or replace. On the Home tab, in the Copy group, click Copy. On the Home tab, in the Replace group, click Replace. The files are copied or replaced as needed.

Can Notepad++ compare folders?

Yes, Notepad can compare folders. To do this, open Notepad and select File > Compare Folders. The Compare Folders dialog box will appear. In the left pane, select the folders you want to compare. In the right pane, select the files you want to compare. The files that are identical will have a check mark next to them.

How do I sync folders in Windows 10?

There are a few ways to sync folders in Windows 10. One way is to use the File Explorer context menu and select “Sync Files and Folders.” This will sync all the files and folders in the current folder and any subfolders. Another way is to use the Windows 10 “File History” feature. This will keep a history of all the files that have been changed in the current folder, as well as any subfolders.