Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
241 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 85

Accepted Solution

by:
oBdA earned 2000 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
On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

 
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 85

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 85

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

719 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