Search code examples
batch-filecomparedir

Windows batch compair two path directories and write out the difference


I want to parse a directory and when new files show up it shoud be listed in a queue for a other batch.

I tried it like that:

@echo off
:START
set ftpdir=Z:\FTP\home\
set xdir=Z:\NewDir\
set vtree=v.txt
set vtreecomp=vcomp.txt

if not exist %vtree% (dir %ftpdir% /S/B > %vtree%) else ( goto VCOMPTREE )
exit

:VCOMPTREE
dir %ftpdir% /S/B > %vtreecomp%
fc %vtree% %vtreecomp% > nul
if errorlevel 1 fc %vtree% %vtreecomp% >> queue.txt & exit 
goto START

But i don't get only the different lines. What i get:

Vergleichen der Dateien v.txt und VCOMP.txt
***** v.txt
Z:\FTP\home\22.10.2013 #111
Z:\FTP\home\23.10.2013 #222
***** VCOMP.txt
Z:\FTP\home\22.10.2013 #111
Z:\FTP\home\22.10.2013 #111 - Kopie
Z:\FTP\home\23.10.2013 #222
*****

***** v.txt
***** VCOMP.txt
Z:\FTP\home\23.10.2013 #222 - Kopie
*****

What i want:

Z:\FTP\home\22.10.2013 #111 - Kopie
Z:\FTP\home\23.10.2013 #222 - Kopie

Can someone help me with that?


Solution

  • fc command output show the differences in sets that include the first and last equal lines. If you want to get just the different lines, you must achieve the comparison of the files by yourself. The Batch program below do that:

    @echo off
    setlocal EnableDelayedExpansion
    
    set ftpdir=Z:\FTP\home\
    set xdir=Z:\NewDir\
    set vtree=v.txt
    set vtreecomp=vcomp.txt
    
    :START
    if not exist %vtree% dir %ftpdir% /S/B > %vtree%
    
    :VCOMPTREE
    dir %ftpdir% /S/B > %vtreecomp%
    fc %vtree% %vtreecomp% > nul
    if errorlevel 1 (
       set "vtreeLine="
       < %vtree% (
       for /F "delims=" %%a in (%vtreecomp%) do (
          if not defined vtreeLine set /P vtreeLine=
          if "!vtreeLine!" equ "%%a" (
             set "vtreeLine="
          ) else (
             echo %%a
          )
       )) > queue.txt
       exit
    )
    goto START
    

    This program just manage the case when new files are added to previous tree; it will fail if any file is deleted.