Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Comparing two directories  (Read 6812 times)

0 Members and 1 Guest are viewing this topic.

Yaro67

    Topic Starter


    Rookie

  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
Comparing two directories
« on: July 17, 2008, 01:36:56 PM »
I want to be able to compare two directories from a command line like if I wrote a batch file for copying a whole directory and then comparing them.  I have found "comp" and "fc"but those only compare specific files and there are a TON of files in the directories I want to compare.  Is there a command to be able to compare two directories and all their sub directories? 

Basically I will use xcopy to copy my files where I want them to go for back up, then I want to compare the two directories, including all the sub directories, to make sure everything is their, and finally then delete the original directory.

BTW... it will be running on Windows Server 2000.

Thanks
« Last Edit: July 17, 2008, 02:56:01 PM by Yaro67 »
Call me Ishmael

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: Comparing two directories
« Reply #1 on: July 17, 2008, 04:18:04 PM »
It's déjà vu all over again. I posted this in another thread, but it may help you out too:

Code: [Select]
@echo off
for /f "tokens=* delims=" %%v in ('dir c:\FolderA\*.* /b') do (
if exist "c:\FolderB\%%v" echo File %%v in FolderA; File %%v in FolderB
if not exist "c:\FolderB\%%v" echo File %%v in FolderA; File %%v NOT in FolderB
)

for /f "tokens=* delims=" %%v in ('dir c:\FolderB\*.* /b') do (
if exist "c:\FolderA\%%v" echo File %%v in FolderA; File %%v in FolderB
if not exist "c:\FolderA\%%v" echo File %%v NOT in FolderA; File %%v in FolderB
)

You may have to change the names of FolderA and FolderB.

Good luck. 8)
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

ghostdog74



    Specialist

    Thanked: 27
    Re: Comparing two directories
    « Reply #2 on: July 17, 2008, 07:50:31 PM »
    if you can download stuffs, see this

    Yaro67

      Topic Starter


      Rookie

    • Certifications: List
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 7
    Re: Comparing two directories
    « Reply #3 on: July 18, 2008, 09:00:28 AM »
    Hey Sidewinder could you do me a favor and explain that code a little better.  I have a basic understanding of coding so I have the jest of what you are doing there are just a few things that are batch related I am new too.  For example...



    I understand that the "for statement" is for, for "given code" do "this."  But could you explain  "tokens=* delims=" %%v in ('dir c:\FolderA\*.* /b'), specifically tokens, delims and what the %%v is doing?  I am not new to coding in general just new to batch files.  If their are files in both we get the first message but if it does not we get the second message, will this work all the way through the directory tree?

    Quote

         @echo off
         for /f "tokens=* delims=" %%v in ('dir c:\FolderA\*.* /b') do (
         if exist "c:\FolderB\%%v" echo File %%v in FolderA; File %%v in FolderB
         if not exist "c:\FolderB\%%v" echo File %%v in FolderA; File %%v NOT in FolderB
         )
       
         for /f "tokens=* delims=" %%v in ('dir c:\FolderB\*.* /b') do (
         if exist "c:\FolderA\%%v" echo File %%v in FolderA; File %%v in FolderB
         if not exist "c:\FolderA\%%v" echo File %%v NOT in FolderA; File %%v in FolderB

    Call me Ishmael

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Comparing two directories
    « Reply #4 on: July 18, 2008, 09:27:43 AM »
    Quote
    I understand that the "for statement" is for, for "given code" do "this."  But could you explain  "tokens=* delims=" %%v in ('dir c:\FolderA\*.* /b'), specifically tokens, delims and what the %%v is doing?

    tokens=* tells the interpreter there is a single variable (%%v) and all the data on each line from the dir command is to be stuffed into that variable

    delims=" overrides the default delimiters which are space and tab

    If you run the dir command from the prompt, you'll see the output appears as:

    filename1.ext
    filename2.ext
    file name3.ext
    .
    .
    .

    By overriding the default delimiters and and declaring a single variable, each iteration of the for loop puts a filename into %%v (including filenames with embedded spaces)

    Quote
    If their are files in both we get the first message but if it does not we get the second message, will this work all the way through the directory tree?

    Nope; as written compares folderA to folderB, then FolderB to FolderA. Did you check out rSync? Might be a better solution with recursed directories.

    Good luck, 8)
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    Yaro67

      Topic Starter


      Rookie

    • Certifications: List
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 7
    Re: Comparing two directories
    « Reply #5 on: July 18, 2008, 10:04:55 AM »
    rsync might work but sense this is a network server and not a personal computer for myself I would rather not install anything new.  Plus this is not sync per say.  I am copying the files from one location to another, confirming they are there, and then deleting the original directory.  The copy is easy I just use xcopy

    xcopy C:\FolderA\*.* G:\ /E


    Which copies everything from the first directory to the second directory including all sub directories.

    Then after a little manipulation I will run your code to compare the two directories before the deletion.

    My next question is if I use how do I delete everything below a certain point in the directory?  I have tried del but it wants a specific file and I have tried rmdir but it deletes that directory and I want to keep it just remove everything below it so in the end it is a empty directory.  Could I do something like this?

         for /f "tokens=* delims=" %%v in ('dir c:\FolderA\*.* /b') do (del "c:\FolderA\%%v )


    Thanks for your help!   ;)
    Call me Ishmael

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Comparing two directories
    « Reply #6 on: July 18, 2008, 12:20:26 PM »
    Quote
    My next question is if I use how do I delete everything below a certain point in the directory?

    Quote
    Could I do something like this?

         for /f "tokens=* delims=" %%v in ('dir c:\FolderA\*.* /b') do (del "c:\FolderA\%%v )

    Del works only with files. RD works with folders.

    One way to approach this would be to get the collection of files in FolderA and delete them. Then go back and get the collection of folders in FolderA and remove the sub-folders and their files.

    Code: [Select]
    for /f "tokens=* delims=" %%v in ('dir c:\FolderA\*.* /a:-d /b') do (
    del "c:\FolderA\%%v
    )
    for /f "tokens=* delims=" %%v in ('dir c:\FolderA\*.* /a:d /b') do (
    rd "c:\FolderA\%%v /s /q
    )

    The snippet should leave you with an empty FolderA. I would encourage you to test this before committing to production.

    Good luck. 8)

    There seems to be a massive fascination with batch files this week. Must be something in the water.
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    Yaro67

      Topic Starter


      Rookie

    • Certifications: List
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 7
    Re: Comparing two directories
    « Reply #7 on: July 18, 2008, 04:15:40 PM »
    Thanks!  I did do a bit of testing with some random files and folders I created myself but here is the some what finished product.

    @echo off

    set /p inputdir=Which directory are you moving files from (Collateral, Design, or Production)?

    echo.


    ::If directory is not found it goes to the DirNotFound section
    if not exist "F:\macdata\MonthlyDisks\CurrentHOData\%inputdir%" goto :DirNotFound

    ::If the directory is found it will execute the xcopy command
    if exist "F:\macdata\MonthlyDisks\CurrentHOData\%inputdir%" xcopy "F:\macdata\MonthlyDisks\CurrentHOData\%inputdir%\*.*" G:\ /E

    pause



    ::Comparing the two directories to make sure both match for the files that have moved
    for /f "tokens=* delims=" %%v in ('dir F:\macdata\MonthlyDisks\CurrentHOData\%inputdir% /b') do (
       if exist G:\%%v echo File %%v in %inputdir%; File %%v in G:
       ::If they do not both match it will go to the exit section
       if not exist G:\%%v goto :exit
       )

    pause


    ::Deleteing the old directory and files   
    for /f "tokens=* delims=" %%v in ('dir F:\macdata\MonthlyDisks\CurrentHOData\%inputdir%\*.* /a:-d /b') do (
       del "F:\macdata\MonthlyDisks\CurrentHOData\%inputdir%\%%v"
       )
    for /f "tokens=* delims=" %%v in ('dir F:\macdata\MonthlyDisks\CurrentHOData\%inputdir%\*.* /a:d /b') do (
       rd "F:\macdata\MonthlyDisks\CurrentHOData\%inputdir%\%%v" /s /q
       )
    echo The orginal files have finshed being moved and deleted.
    pause   

    EXIT

    :DirNotFound
    echo ***That directory does not exist***
    @ping 127.0.0.1 -n 5 -w 1000 > nul
    pause

    :exit
    echo File %%v in %inputdir%; File %%v NOT in G:
    pause
    exit


    It's really basic right now and I am sure I will do further tweaking but it gets the job done and makes sure it's done easier and right.
    Call me Ishmael

    kalasha



      Rookie

      Re: Comparing two directories
      « Reply #8 on: July 19, 2008, 01:43:03 AM »
      folders...files...


      Code: [Select]
      @echo off
      for /f "tokens=* delims=" %%v in ('dir c:\FolderA\*.* /b') do (
      if exist "c:\FolderB\%%v" echo File %%v in FolderA; File %%v in FolderB
      if not exist "c:\FolderB\%%v" echo File %%v in FolderA; File %%v NOT in FolderB
      )

      for /f "tokens=* delims=" %%v in ('dir c:\FolderB\*.* /b') do (
      if exist "c:\FolderA\%%v" echo File %%v in FolderA; File %%v in FolderB
      if not exist "c:\FolderA\%%v" echo File %%v NOT in FolderA; File %%v in FolderB
      )


      i like this code, simple and do not have to install anything.  but can this code compare everything in the parent folder that i specified ex. FolderA, has 4 folders, and those may have other folders in them and other files.  this only compares the initial structure in FolderA (in my case 8 folders, and nothing in those folders.)

      thank you,

      jat

      ghostdog74



        Specialist

        Thanked: 27
        Re: Comparing two directories
        « Reply #9 on: July 19, 2008, 06:08:14 AM »
        but take note that it only checks for file existence and not file contents or file modified. if the source is older than destination, the destination will be overwritten with an old file. Best to check for md5 hash and file modified date.

        Sidewinder



          Guru

          Thanked: 139
        • Experience: Familiar
        • OS: Windows 10
        Re: Comparing two directories
        « Reply #10 on: July 19, 2008, 08:26:04 AM »
        Quote
        this only compares the initial structure in FolderA (in my case 8 folders, and nothing in those folders.)
        [

        Yes, you're right.

        1. That's all it was designed to do otherwise it wouldn't conform to the KISS method of coding. ;D

        2. Each situation is different, which is why we frown on hijacking threads.

        Not sure what you asking, but if you want to check sub folders, AND the sub folder structure of FolderA was identical to FolderB, you could do a search and replace of FolderA with FolderB in the path and check that way.

        Otherwise you'd have to gather a list of all the folders in FolderB (including the top level FolderB itself), and check whether each file from FolderA (including subfolders) exists in any of the FolderB structure. Duplicate file names in different FolderB folders could really foul up the validity of your results.

        Finally you'd have to flip all the logic so FolderB is the master and FolderA is the slave.

        Dusty brings up a good point. Files with identical names do not necessarily have identical content.

        You didn't mention your OS <sigh> but there is a PowerToy called SyncToy that may help. You don't actually have to sync anything, it has preview function. XP, VISTA only.

         8)
        The true sign of intelligence is not knowledge but imagination.

        -- Albert Einstein

        Geza



          Rookie

          Re: Comparing two directories
          « Reply #11 on: July 21, 2008, 11:30:37 AM »
          If the folders in question have a lot of files and/or copying permissions is also a must, you might want to consider robocopy (microsoft) instead of xcopy. It is a lot more powerful copy tool than xcopy, it is a 32 bit app while xcopy is a 16 bit dos app and it is also free.

          Depending what you want to do you can use command line switches like /mov, /mir, (/copy is the default one), etc.

          You can see what it can do and how to use it - here:
          http://www.ss64.com/nt/robocopy.html

          and you can download it from here:
          http://www.microsoft.com/downloads/details.aspx?familyid=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displaylang=en

          Geza
          Network Admin

          saintjoe69



            Newbie

            • Experience: Beginner
            • OS: Unknown
            Re: Comparing two directories
            « Reply #12 on: May 08, 2012, 02:30:07 PM »
            It's déjà vu all over again. I posted this in another thread, but it may help you out too:

            Code: [Select]
            @echo off
            for /f "tokens=* delims=" %%v in ('dir c:\FolderA\*.* /b') do (
            if exist "c:\FolderB\%%v" echo File %%v in FolderA; File %%v in FolderB
            if not exist "c:\FolderB\%%v" echo File %%v in FolderA; File %%v NOT in FolderB
            )

            for /f "tokens=* delims=" %%v in ('dir c:\FolderB\*.* /b') do (
            if exist "c:\FolderA\%%v" echo File %%v in FolderA; File %%v in FolderB
            if not exist "c:\FolderA\%%v" echo File %%v NOT in FolderA; File %%v in FolderB
            )

            You may have to change the names of FolderA and FolderB.

            Good luck. 8)

            This works great!  Anybody have any idea what command I would use to list only files found in FolderA and FolderB for today's date only?  Thanks.

            Allan

            • Moderator

            • Mastermind
            • Thanked: 1260
            • Experience: Guru
            • OS: Windows 10
            Re: Comparing two directories
            « Reply #13 on: May 08, 2012, 02:36:27 PM »
            You are posting in a 4 year old thread. I suggest you start a new topic if you have a question.