# Solved: Batch file that compares contents of two folders



## Reconsniper1

Hello All! I have searched the web looking for the answer to several of my questions in a attempt to find the answer to many of my batch questions. Some easy, some on the more difficult side. I came across this website and now it is the first place I search for my answers because the answers are normally spot on. Here is my newest question.

I am attempting to make a batch file that will compares the files of c:\test1 with c:\tests2. 

I know you can do xcopy and have it copy the files that are not in one folder or files that have changed from one folder to the other folder, but there a way for it to produce a .txt showing what those files are without it actually moving the files. 

Thanks, 
Tim


----------



## Squashman

http://www.sourcegear.com/diffmerge/
http://furius.ca/xxdiff/
http://www.componentsoftware.com/Products/CSDiff/


----------



## Elvandil

Use the "delta" command.


----------



## Squashman

That DirMatch would probably work as well. Good finds.


----------



## devil_himself

Here's a Pure Batch Code



Code:


[FONT="Arial"][FONT="Fixedsys"]:bof

    @echo off
    setlocal

:init
   
    set dirA=c:\src
    set dirB=c:\dest

    if not exist "%dirA%" echo dirA not found & goto :EOF
    if not exist "%dirB%" echo dirB not found & goto :EOF

    for /f "delims=" %%a in ('dir /b /a-d "%dirA%" 2^>NUL') do if not exist "%dirB%%%a" echo %%a does not exist in "dirB"   
    for /f "delims=" %%a in ('dir /b /a-d "%dirB%" 2^>NUL') do if not exist "%dirA%%%a" echo %%a does not exist in "dirA"

  
:eof[/FONT][/FONT]


----------



## Reconsniper1

Hello everyone. Thanks for the responses.

I'm not looking for a 3rd party program to do this but if I can't get a batch code to work then I will probably resort to it.

Is Delta a linux command because XP doesn't have it?

I tried your batch Devil but it didn't do anything. I inserted the Source and Dest but i don't see an output file.

I came across this batch and it doesn't seem to be outputting the results. It basically lists all the icons on my desktop into the missing.log.

@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in ('dir/b/a-d') do (
if exist C:\test\%%a (
fc %%a C:\test1\%%a >> compare.log
) else (
echo %%a is missing >> missing.log
)
)

Pause



devil_himself said:


> Here's a Pure Batch Code
> 
> 
> 
> Code:
> 
> 
> [FONT="Arial"][FONT="Fixedsys"]:bof
> 
> @echo off
> setlocal
> 
> :init
> 
> set dirA=c:\src
> set dirB=c:\dest
> 
> if not exist "%dirA%" echo dirA not found & goto :EOF
> if not exist "%dirB%" echo dirB not found & goto :EOF
> 
> for /f "delims=" %%a in ('dir /b /a-d "%dirA%" 2^>NUL') do if not exist "%dirB%%%a" echo %%a does not exist in "dirB"
> for /f "delims=" %%a in ('dir /b /a-d "%dirB%" 2^>NUL') do if not exist "%dirA%%%a" echo %%a does not exist in "dirA"
> 
> 
> :eof[/FONT][/FONT]


----------



## Squashman

Reconsniper1 said:


> Hello everyone. Thanks for the responses.
> 
> I'm not looking for a 3rd party program to do this but if I can't get a batch code to work then I will probably resort to it.
> 
> Is Delta a linux command because XP doesn't have it?


There is a download for the utility right in the link he gave you.


----------



## devil_himself

Open The Command Prompt And Run The Batch It Will Echo The Results On The Screen !


----------



## Reconsniper1

I tried that. It just pops up and closes right away. So I tried to throw PAUSE with it and it still didnt stay up. it like instantly opens / closes.


----------



## Squashman

No you are not doing it correctly

Launch the batch file from with in the CMD box. So open up a cmd prompt first. Change directory to where the batch file is. Then type the batch file name to execute it.


----------



## Reconsniper1

Thank you! That did work. All my other batch files work from just clicking on the icon so I just assumed they would all work that way. Is there any way for to have it output the the file names in a log file rather than on the screen.


----------



## Squashman

batchfile.bat >> logfile.txt


----------



## Reconsniper1

awesome!!! worked like a charm!


----------



## devil_himself

Glad You Got It Working !


----------



## TheOutcaste

Reconsniper1 said:


> I came across this batch and it doesn't seem to be outputting the results. It basically lists all the icons on my desktop into the missing.log.
> 
> @echo off
> setLocal EnableDelayedExpansion
> 
> for /f "tokens=* delims= " %%a in ('dir/b/a-d') do (
> if exist C:\test\%%a (
> fc %%a C:\test1\%%a >> compare.log
> ) else (
> echo %%a is missing >> missing.log
> )
> )
> 
> Pause


The dir command in this program will do a directory of the current folder, so if the batch is on your desktop, it's comparing your desktop to the c:\test folder.
so change *'dir/b/a-d'* to *'dir/b/a-d c:\test'*
The *If exist* statement needs to test the other folder, otherwise the else portion will never execute. (You got a missing.log file because it was getting filenames off the desktop and checking to see if they exist in the test folder).
It also needs quotes in case a file name contains spaces
the *fc* command also needs the path added to do the compare, and needs quotes in case a file name contains spaces.
*"delims="* will do the same as *"tokens=* delims= "*

So, if you actually want to compare the files and not just the names, it should read like this:



Code:


if exist compare.log del compare.log
if exist missing.log del missing.log
for /f "delims=" %%a in ('dir/b/a-d c:\test') do (
if exist "C:\test1\%%a" (
fc "c:\test\%%a" "C:\test1\%%a" >> compare.log
) else (
echo %%a is missing >> missing.log
)
)

Pause

The compare and missing logs will be created in the same folder as the batch file, so if it's on the desktop they will be there as well
This does not check for the case where a file exists in the test1 folder but not in the test folder.

HTH

Jerry


----------

