DOS Command for Folder Comparison

Posted on 2005-04-06
Last Modified: 2012-08-13
I'm currently using a batch script to run XCOPY to move contents from a source directory to a target directory.  These contents that I'm moving also contain sub folders.   These contents are not static paths and change with whatever folder (contents) I pass into this batch script.  The script runs perfectly fine except today I noticed an error where XCOPY seemed to fail because there was some sort of network glitch between the target directory (another server) and me.  

So what I need to do is put some sort of test in place inside this batch script file that can compare the source directory from the target directory to let me know if the XCOPY failed.  

So here's the problem, I need to find a DOS command or something that can be called from the batch script program that can determine if the source directory is the same as the target directory and if not alert the user that there was a problem with XCOPY after the command runs.  

I have been looking in the Resource Disk for Windows NT and XP but I can't find anything.   Others out there must have run into this problem in the past and needed some sort of catch/test that would determine if XCOPY worked or not.   Can anyone guide me in the right direction of such a test?
Question by:healthshare
    LVL 95

    Expert Comment

    by:Lee W, MVP
    Why not just log the transfer and create an error log?

    For example, append the following to your XCOPY line:

    >> copy.log 2>>&1 copyerr.log
    LVL 95

    Expert Comment

    by:Lee W, MVP
    Otherwise, you can also do a DIR on the source directory and a DIR on the Target directory and redirect output to a text file.  Then run COMP to compare the two files.
    LVL 82

    Assisted Solution

    All you need is the right tool to copy your files with. copy and xcopy are fine if you only copy a few files. If you copy larger amounts of data, especially over a network connection, do yourself a favor and use robocopy ("robust copy") from the Resource Kit. You get a log file, it will by default copy only files that aren't already in the target, and it will retry if it fails.
    Have a look at the file robocopy.doc in the installation directory for a complete explanation.
    The available switches look a bit frightening at the beginning, but you won't need most of them. Pay attention to the /w (wait) and /r (retry) settings and set them to useful values.

    Windows Server 2003 Resource Kit Tools
    LVL 1

    Assisted Solution

    Use WinDiff to compare directories after the robocopy is finished.  It will compare the complete folder structure and files and report if there are any differences.  You can find WinDiff in the 2003 support tools.  

    Author Comment

    The thing is I run a batch script that will copy whatever directory I give it in the parameters and it pastes it to the target directory.   Then I have another batch script that will make multiple calls to that other batch script to copy and paste many many directories to whatever target directory I pick.   so basically I could have a program running for hours just copying and pasting directories and if it fails I want it to either give me the reason why it's failing and stop the rest of the process or after the xcopy is done for that one command to then have another script test whether the 2 directories are either the same or different (if they are different, then they should fail).  

    So basically here's what I have, a main script file that will take these parameters:

    CopyCustomer [SourceDirectory] [Customer] [TargetLocation]

    Then I have another batch script that will make multiple calls like this:

    CALL CopyCustomer \\source1\     Customer1    \\target2\...\
    CALL CopyCustomer \\source2\     Customer2    \\target3\...\
    CALL CopyCustomer \\source1\     Customer3    \\target2\...\
    CALL CopyCustomer \\source2\     Customer4    \\target1\...\
    CALL CopyCustomer \\source1\     Customer5    \\target2\...\
    CALL CopyCustomer \\source1\     Customer6    \\target4\...\
    CALL CopyCustomer \\source2\     Customer7    \\target1\...\

    So I don't want the process to continue the whole way through if there was an error and the source directory and target directory are different.  That's why I was thinking of some sort of error catching that could compare the folders after it was done and tell me if there was a problem and if there was stop the rest of the process.

    But I did manage to find something else I wasn't aware of, XCOPY has exit codes that allow you to catch any error codes that may come out of the copy process and then do something with a IF statement which you could then exit the program via an error in the copy process.   This may be my solution.  I'm still testing it out.  I will follow up if it is.   Thanks for all the feedback up to this point.
    LVL 95

    Accepted Solution

    If you want it to stop, just use:

    IF "%errorlevel%" NEQ 0 GOTO EOF

    With a copy of your code, we could modify it and make it work for you.

    Author Comment

    nope.  I'm all set.  I think I just answered my own question by using the XCOPY's exitcode.    

    I was really hoping for some sort of DOS command that I could call within my script that could compare two directories and tell me if they are the same or not and give me an exitcode or boolean in return.  But it seems as if that sort of command doesn't exist in the DOS world.   I thought it would.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    So you have two Windows Servers and you have a directory/folder/files on one that you'd like to mirror to the other?  You don't really want to deal with DFS or a 3rd party solution like Doubletake. You can use Robocopy from the Windows Server 200…
    ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    728 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

    18 Experts available now in Live!

    Get 1:1 Help Now