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
232 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
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 83

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 83

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 83

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
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 Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

911 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

16 Experts available now in Live!

Get 1:1 Help Now