1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Batch file that compares contents of two folders

Discussion in 'DOS/Other' started by Reconsniper1, Apr 17, 2008.

Thread Status:
Not open for further replies.
Advertisement
  1. Reconsniper1

    Reconsniper1 Thread Starter

    Joined:
    Apr 16, 2008
    Messages:
    15
    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
     
  2. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
  3. Elvandil

    Elvandil

    Joined:
    Aug 1, 2003
    Messages:
    51,988
    Use the "delta" command.
     
  4. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    That DirMatch would probably work as well. Good finds.
     
  5. devil_himself

    devil_himself

    Joined:
    Apr 7, 2007
    Messages:
    4,910
    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]
    
     
  6. Reconsniper1

    Reconsniper1 Thread Starter

    Joined:
    Apr 16, 2008
    Messages:
    15
    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




     
  7. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    There is a download for the utility right in the link he gave you.
     
  8. devil_himself

    devil_himself

    Joined:
    Apr 7, 2007
    Messages:
    4,910
    Open The Command Prompt And Run The Batch It Will Echo The Results On The Screen !
     
  9. Reconsniper1

    Reconsniper1 Thread Starter

    Joined:
    Apr 16, 2008
    Messages:
    15
    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.
     
  10. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    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.
     
  11. Reconsniper1

    Reconsniper1 Thread Starter

    Joined:
    Apr 16, 2008
    Messages:
    15
    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.
     
  12. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    batchfile.bat >> logfile.txt
     
  13. Reconsniper1

    Reconsniper1 Thread Starter

    Joined:
    Apr 16, 2008
    Messages:
    15
    awesome!!! worked like a charm!
     
  14. devil_himself

    devil_himself

    Joined:
    Apr 7, 2007
    Messages:
    4,910
    Glad You Got It Working !

    :)
     
  15. TheOutcaste

    TheOutcaste

    Joined:
    Aug 7, 2007
    Messages:
    9,028
    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
     
  16. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/704816

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice