Solved

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

Posted on 2014-03-17
7
231 Views
Last Modified: 2014-03-18
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.
0
Comment
Question by:LuckyLucks
7 Comments
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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?
0
 
LVL 13

Expert Comment

by:Santosh Gupta
Comment Utility
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

0
 
LVL 82

Accepted Solution

by:
oBdA earned 500 total points
Comment Utility
For starters, here's a very basic version, it just checks two folder (no recurse) and copies any file from B whose name is not in A to C:
@echo off
setlocal enabledelayedexpansion
set SourceA=D:\Temp\A
set SourceB=D:\Temp\B
set Target=D:\Temp\C
for %%a in ("%SourceB%\*.*") do (
	echo Processing '%%~nxa' ...
	if not exist "%SourceA%\%%~nxa" (
		copy "%%a" "%Target%"
	)
)
echo ... done.

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 13

Expert Comment

by:Santosh Gupta
Comment Utility
for /R c:\b %%f in (*.*) 

{

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

}

Open in new window

0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
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

0
 

Author Comment

by:LuckyLucks
Comment Utility
Thanks, reviewing...

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

taken from echo Processing '%%~nxa' ...
0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
"%%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).
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now