Quote:
Originally Posted by mdmini hi guys
i wanted to write a batch file that would compare 2 folders and give me the difference in another folder.
i got this script but i want the difference to go to a another folder. plsssssssssssss help....
this is the script i managed to get.......
@echo off
setlocal ENABLEDELAYEDEXPANSION
::Set dirA=%1
::Set dirB=%2
Set dirA=C:\1
Set dirB=C:\2
dir /B /O %dirA% >dirA.txt
dir /B /O %dirB% >dirB.txt
Echo Files that are found in BOTH folders %dirA% AND %dirB% >both.txt
Echo. >>both.txt
findstr /G:dirA.txt dirB.txt >>both.txt
echo ------------------------------------- >>both.txt
Echo Files that are found in folder %dirA% but NOT in folder %dirB% >AnotinB.txt
Echo. >>AnotinB.txt
find /V /C "this_is_an_absurd_string" dirB.txt >numlines.txt
for /f "tokens=3 delims= " %%B in (numlines.txt) do set /A maxnum=%%B
for /F "tokens=* delims= " %%A in (dirA.txt) do (
find /V /C "%%A" dirB.txt >numlines.txt
for /f "tokens=3 delims= " %%B in (numlines.txt) do set /A foundnum=%%B
if %maxnum%==!foundnum! (echo %%A >>AnotinB.txt xcopy %%A c:\3) See Note 1
)
echo ------------------------------------- >>AnotinB.txt
Echo Files that are found in folder %dirB% but NOT in folder %dirA% >BnotinA.txt
Echo. >>BnotinA.txt
find /V /C "this_is_an_absurd_string" dirA.txt >numlines.txt
for /f "tokens=3 delims= " %%B in (numlines.txt) do set /A maxnum=%%B
for /F "tokens=* delims= " %%A in (dirB.txt) do (
find /V /C "%%A" dirA.txt >numlines.txt
for /f "tokens=3 delims= " %%B in (numlines.txt) do set /A foundnum=%%B
if %maxnum%==!foundnum! (echo %%A >>BnotinA.txt copy c:\2 /f C:\3) See Note 2
)
echo. >>BnotinA.txt
echo ------------------------------------- >>BnotinA.txt
copy /B both.txt+AnotinB.txt+BnotinA.txt dupli.txt >nul
more dupli.txt
for %%A in (dirA.txt dirB.txt both.txt AnotinB.txt BnotinA.txt numlines.txt) do (
if exist %%A del %%A
) |
A few suggestions:
I would suggest you add a Set dirC=c:\3 line so you can refer to your difference folder by a variable.
also, you need to clear this folder first, or set xcopy to force an overwrite, or it will pause for confirmation each time you run it.
To clear the folder use
del /f/q %dirC%\*.*, or add the
/Y switch to the xcopy command
For the dir commands, you don't really need the /O switch as it will make no difference how the names are sorted. But if you use it, it's best to specify the sort order. If the dircmd environment variable has been set, you could get unexpected results. So use
/O:N for example.
You may also want to add the
/A-D switch so directories will not be listed, just in case one or the other of the source folders has subfolders. It won't affect the files that end up in the difference folder, but the folder will be listed in the results file. If you need to check subfolder contents as well, that will require a bit more code.
If the extension is the same, you only need to specify it in the IF statement:
for %%A in (dirA dirB both AnotinB BnotinA numlines) do if exist %%A.txt del %%A.txt
Note 1
xcopy %%A c:\3)
You need to specify the path to %%A, else it uses the current folder, and it needs to be in quotes in case the file/folder name contains a space, so use:
xcopy "%dirA%\%%A" %dirC% >nul)
Note 2
copy c:\2 /f C:\3)
/f is not a valid switch. This line should match the previous one, just use the 2nd source folder:
xcopy "%dirB%\%%A" %dirC% >nul)
Finally, if you need the result file in the c:\3 folder, you need to specify the path. Replace
dupli.txt with
%dirC%\dupli.txt wherever it occurs.
The following will do the same without using temp files on the hard drive which have to be created and searched, which will speed it up a bit. This avoids creating a new numline.txt file for every filename that exists in dirA.
Code:
@echo off
Set _t0=C:\test1
Set _t1=C:\test2
Set _t2=C:\test3
:clear the results folder. Another option is to use the /Y switch on the Xcopy commands
del /f/q %_t2%\*.*
:set up info lines for each section of the report
Echo Files that are found in BOTH folders %_t0% AND %_t1% >_t0.txt
Echo. >>_t0.txt
Echo Files that are found in folder %_t0% but NOT in folder %_t1% >_t1.txt
Echo. >>_t1.txt
Echo Files that are found in folder %_t1% but NOT in folder %_t0% >_t2.txt
Echo. >>_t2.txt
For /F "delims=" %%I In ('dir /b/o:n /a-d "%_t0%"') Do If EXIST "%_t1%\%%I" (Echo %%I >>_t0.txt) ELSE (
xcopy "%_t0%\%%I" %_t2% >nul & echo %%I >>_t1.txt)
For /F "delims=" %%I In ('dir /b/o:n /a-d "%_t1%"') Do If NOT EXIST "%_t0%\%%I" xcopy "%_t1%\%%I" %_t2% >nul & Echo %%I >>_t2.txt
:write a separator line to each end each section
For /L %%I in (0,1,2) Do Echo ------------------------------------- >>_t%%I.txt
Copy /b _t0.txt+_t1.txt+_t2.txt %_t2%\dupli.txt >nul
:Cleanup
For /L %%C In (0,1,2) Do (del /f/q _t%%C.txt) & set _t%%C= HTH
Jerry