2

I have two folders and have files inside both folders. I want to compare both folder’s file names and want to move/copy unmatched files to a third folder.

How can I do this without any tools?

I want to perform this with cmd.

Example:

Folder 1:

Filename1.pdf
Filename2.pdf
Filename3.tiff
Filename4.jpg

Folder 2:

Filename1.pdf
Filename2.pdf
Filename3.tiff
Filename4.jpg
Filename5.pdf
Filename6.jpg

Result:

Folder 3:

Filename5.pdf
Filename6.jpg
Mofi
  • 46,139
  • 17
  • 80
  • 143
Mr UK
  • 21
  • 1
  • 4

2 Answers2

1

As Stephan pointed out, this code is for the linux command line, not the windows cmd. so it would need git bash terminal or wsl to run.

The question is about cmd (the Windows command line). Running Unix/Linux code would require installing additional tools, which explicitly is not wanted by the asker.

This code will copy all unmatched files from folder1 and folder2 into folder 3.

This first line gets all the directories and files of folder1 & folder2 and lists them in a file.

(ls -d folder1/* && ls -d folder2/*) > dir.txt

This line gets the unmatched files that do not exist in both directories and lists them in a file.

diff -r <(ls folder1) <(ls folder2) | sed 's/^< //' | sed 's/^> //' > unique.txt

This line greps the full file paths from the directory file that exist in the unmatched files and lists them.

grep -wFf unique.txt dir.txt | sed 's/^< //' | sed 's/^> //' > tocopy.txt

Now that we have a list of all the filepaths we can just copy them to folder3.

cp -r $(cat tocopy.txt) folder3
micah
  • 838
  • 7
  • 21
  • 1
    The question is about `cmd` (the Windows command line). Running Unix/Linux code would require installing additional tools, which explicitly is not wanted by the asker. – Stephan Aug 17 '22 at 18:13
  • @Stephan oh dang yeah you're right, my bad – micah Aug 17 '22 at 18:46
  • Getting below msg when I run this command in cmd 'ls' is not recognized as an internal or external command, operable program or batch file. – Mr UK Sep 23 '22 at 19:05
  • @MrUK you'd need to install and use the git bash terminal in my solution as Stephan pointed out, I didn't realize it was a windows terminal question when writing my solution. – micah Sep 23 '22 at 19:07
0

The task can be done with a batch file with following code for files not having the hidden attribute set:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "SourceFolder1=C:\Full Path\Folder 1"
set "SourceFolder2=C:\Full Path\Folder 2"
set "TargetFolder=C:\Full Path\Folder 3"

if not exist "%SourceFolder1%\" echo ERROR: Missing source folder 1: "%SourceFolder1%"& goto ErrorExit
if not exist "%SourceFolder2%\" echo ERROR: Missing source folder 2: "%SourceFolder2%"& goto ErrorExit

md "%TargetFolder%" 2>nul
if exist "%TargetFolder%\" goto CopyFiles
echo ERROR: Failed to create target folder: "%TargetFolder%"

:ErrorExit
echo/
pause
exit /B 1

:CopyFiles
rem Copy all files being unique in source folder 1 to target folder.
for %%I in ("%SourceFolder1%\*") do if not exist "%SourceFolder2%\%%~nxI" copy "%%I" "%TargetFolder%\"

rem Copy all files being unique in source folder 2 to target folder.
for %%I in ("%SourceFolder2%\*") do if not exist "%SourceFolder1%\%%~nxI" copy "%%I" "%TargetFolder%\"

rem Remove the target folder on being still empty and restore previous environment.
rd "%TargetFolder%" 2>nul
endlocal

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • copy /?
  • echo /?
  • endlocal /?
  • exit /?
  • for /?
  • goto /?
  • if /?
  • md /?
  • pause /?
  • rd /?
  • rem /?
  • set /?
  • setlocal /?

See also single line with multiple commands using Windows batch file for an explanation of the unconditional command operator &.

Mofi
  • 46,139
  • 17
  • 80
  • 143