Sync and deleting using batch file

Posted on 2007-10-10
Last Modified: 2008-01-09
I have this batch file. It syns the directories and files from source to destination. I'm trying to modify it so that if a file exist in destination that does not exists in source that file should be deleted off the destination machine in addition to the sync of source and destination which the below command will do. Any help would be greatly appreciated.

@echo off  
if "%3"=="" (  
echo Syncing "C:\New Folder" with "\\ Folder"  
xcopy "C:\New Folder" "\\ Folder" /d /i /y /e
for /D %%d in (%2\\*) do (  
if not exist "%1\\%%~nd" (  
echo Deleting directory %%~nd  
rd "%%d" /s /q  
for %%f in (%2\\*) do (  
if not exist "%1\\%%~nf%%~xf" (  
echo Deleting file %%~nf%%~xf  
del "%%f"  
for /D %%d in (%1\\*) do call Batch3.bat "%1\\%%~nd" "%2\\%%~nd" 0  

Question by:rrisal
    LVL 7

    Accepted Solution

    i would use robocopy:
    LVL 7

    Expert Comment

    ROBOCOPY source dest /MIR
    LVL 7

    Expert Comment

    oh, 1 word of warning, be carefull, it WILL delete all files missing in source from dest and it will do it very quickly
    LVL 35

    Expert Comment

    Second the motion on robocopy.  It seems to me you're reinventing the wheel doing it through batch script.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Windows 10 is here and for most admins this means frustration and challenges getting that first working Windows 10 image. As in my previous sysprep articles, I've put together a simple help guide to get you through this process. The aim is to achiev…
    VM backups can be lost due to a number of reasons: accidental backup deletion, backup file corruption, disk failure, lost or stolen hardware, malicious attack, or due to some other undesired and unpredicted event. Thus, having more than one copy of …
    This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…
    The viewer will learn how to download and install Comodo Backup on Windows 7. Comodo Backup is another solution for backing up your computer. It is free for local backup and online backup has differing amounts depending on storage required. In my op…

    779 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

    17 Experts available now in Live!

    Get 1:1 Help Now