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
239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39935176
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
ID: 39935297
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 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 39935298
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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 13

Expert Comment

by:Santosh Gupta
ID: 39935304
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 84

Expert Comment

by:oBdA
ID: 39935560
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
ID: 39935578
Thanks, reviewing...

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

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

Expert Comment

by:oBdA
ID: 39935600
"%%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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

739 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