Link to home
Start Free TrialLog in
Avatar of LuckyLucks
LuckyLucks

asked on

Checking two folders A and B and placing all files not in A but in B into a third folder C

Hi EEE,

  I have two folders on my hard drive A and B. I want to compare all files in B to A. Any file thats in B and not in A, shall be copied into a third folder C. All this to be done in the batch. I want to keep A , B and C as variables so set A=C:/A for example.
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Can I clarify a few things, though probably won't have time for a while to write.

1) What about if the files are different sizes or contents but the same names?
2) Is it just one dir, or subdirs too, in which case are subdirs needed in C?
Avatar of Santosh Gupta
Santosh Gupta

Hi,
Assuming you have no sub directories.

for /R c:\b %%f in (*.*) 

{

if notexist c:\a\%%F then "xcopy /y c:\a\%%f c:\c\

}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
for /R c:\b %%f in (*.*) 

{

if notexist c:\a\%%F then "xcopy /y c:\b\%%f c:\c\"

}

Open in new window

Just for the fun of it, here's a more complex version where you can specify how to handle duplicate but differing files, and how to handle subfolders. It requires robocopy.exe which is part of the OS since Vista/2008, and came as part of the Resource Kit Tools for XP/W2k3 (http://www.microsoft.com/en-us/download/details.aspx?id=17657).
@echo off
setlocal enabledelayedexpansion
set SourceA=D:\Temp\A
set SourceB=D:\Temp\B
set Target=D:\Temp\C
REM ************* How to process files that exist in A, but differ from the ones in B
REM *** Copy if the file in B and the file in A have different sizes (but the same date):
set CopyIfDiffer=0
REM *** Copy if the file in B is newer than the file in A:
set CopyIfNewer=0
REM *** Copy if the file in B is older than the file in A:
set CopyIfOlder=0
REM ************* How to process subfolders
REM *** Set this to 1 if subfolders in B should be processed as well:
set RecurseSource=0
REM *** If RecurseSource==1, set this to 1 if B's subfolder structure should be created in C; if 0, the file structure will be flattened (no duplicate check, last writer wins!):
set RecurseTarget=1
REM *************
set RCOptions=  
if "%CopyIfDiffer%"=="0"  set RCOptions=%RCOptions% /XC
if "%CopyIfNewer%"=="0"   set RCOptions=%RCOptions% /XN
if "%CopyIfOlder%"=="0"   set RCOptions=%RCOptions% /XO
if "%RecurseSource%"=="1" set RCOptions=%RCOptions% /S
set TargetFolder=%Target%
if not exist "%TargetFolder%" md "%TargetFolder%"
for /f "tokens=*" %%a in ('robocopy.exe "%SourceB%" "%SourceA%" /l /njh /njs /ndl /ns /nc /fp /xx %RCOptions%') do (
	call :Trim %%a
	echo Processing '!TrimmedPath!' ...
	for %%t in ("!TrimmedPath!") do (
		if "%RecurseSource%%RecurseTarget%"=="11" (
			set SourceFolder=%%~dpt
			set TargetFolder=%Target%!SourceFolder:%SourceB%=!
			if not exist "!TargetFolder!" md "!TargetFolder!"
		)
		copy "%%~t" "!TargetFolder!"
	)
)
echo ... done.
goto :eof

:Trim
set TrimmedPath=%*
goto :eof

Open in new window

Avatar of LuckyLucks

ASKER

Thanks, reviewing...

odBA, what does the '%%~nxa' stand for , also what do the individual % and ~, nxa do?

taken from echo Processing '%%~nxa' ...
"%%a" is the "for" loop variable. The tilde directly after "%%" tells cmd.exe that I want some special expansion, in this case name and extension of the file in the loop variable.
In other words: %%~nxa expands to the full file name (but without path) of the file currently being processed.
There are other special expansion commands; enter "help call" in a command prompt for details (the description uses the command line argument %1 as variable, but it works just the same way for "for" loop variables).