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

Author Topic: Issues come from comparing two trees  (Read 3429 times)

0 Members and 1 Guest are viewing this topic.

Stan Huang

    Topic Starter


    Beginner

    • Experience: Experienced
    • OS: Windows 7
    Issues come from comparing two trees
    « on: May 30, 2013, 08:06:00 PM »
    Below is my program for comparing all files and folders under two specified folders, dir1 & dir2. It works if two folders contain only one layer. That is, it succeeds if each folder contains only ordinary files and no sub-folders. In case each dir1/2 contains sub-folders, it goes wrong. For example, suppose dir1 contains one ordinary file 1.txt and dd sub-folder containing 11.txt. This program will compare dir1\1.txt to dir2\1.txt. That's fine. But it will further compare dir1\11.txt to dir2\11.txt. That's surely wrong because what it is supposed to do is to compare dir1\dd\11.txt to dir2\dd\11.txt.
    The trouble comes from that 'for' in DOS Command can only offer below variable substitutions:
    %~p: full path
    %~f: full path and file name
    %~nz: file name

    So, there is no easy way to transform \dir1\dd\11.txt to \dir2\dd\11.txt. Using variable substitutions, for dir1\dd\11.txt,  we can only get \dir1, \dir1\dd\11.txt. What I need is to get dd\11.txt and then append it to \dir2\. This way, I can get \dir2\dd\11.txt. But how can I get dd\11.txt from all possible variable substitutions?


    :cmpdir  <dir1> <dir2>
    setlocal ENABLEEXTENSIONS
    @echo off

    setlocal enabledelayedexpansion
    FOR /R "%1\" %%G in (*) DO (
       if not exist %2\%%~nxG (
          echo "%2\%%~nxG disappeared"
          exit /b 255
       ) else (
          if not exist "%1\%%~nxG\" (
             echo "%1\%%~nxG compared to %2\%%~nxG"
             fc /b %1\%%~nxG %2\%%~nxG > nul
             if !errorlevel! neq 0 (
                echo "%2\%%~nxG corrupted"
                exit /b 255
             )
          )
       )
    )

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: Issues come from comparing two trees
    « Reply #1 on: May 30, 2013, 08:31:53 PM »
    There is an old command line program called Comptree (ct.exe) which binary compares trees of files.

    http://allan.hoiberg.dk/eng/prog_ct.htm

    Stan Huang

      Topic Starter


      Beginner

      • Experience: Experienced
      • OS: Windows 7
      Re: Issues come from comparing two trees
      « Reply #2 on: June 12, 2013, 11:53:05 PM »
      I tested the program: http://allan.hoiberg.dk/eng/prog_ct.htm
      It works, but there are two issues: it works only at 32-bit computer and it's executable one so there is no way to modify it if necessary. I finished a script program as below and verified it. Anyone can use it, modify or spread it.

      REM ::::::::::::::::::::::::::::::::::::::::::::::::::::::
      REM Subroutines
      REM ::::::::::::::::::::::::::::::::::::::::::::::::::::::
      :cmpdir dir1 dir2
      setlocal enabledelayedexpansion

      pushd %2
      for /f "delims=" %%A in ('cd') do set dir2=%%A
      popd

      pushd %1
      FOR %%G in (*) DO (
           echo "%%~fG" ***vs*** "%dir2%"\"%%~nxG"
         if not exist "%dir2%"\"%%~nxG" (
            echo "%dir2%"\"%%~nxG" disappeared
            goto :cmpdir_err
         )
         fc /b "%%~fG"   "%dir2%"\"%%~nxG" > nul
         rem comp "%%~fG"   "%dir2%"\"%%~nxG" < nnnn > nul 2> nul
         if !errorlevel! neq 0 (
            echo "%dir2%"\"%%~nxG" corrupted
            goto :cmpdir_err
         )
      )
      FOR /d %%G in (*) DO (
         echo === %%~fG
         if not exist "%dir2%"\"%%~nxG" (
            echo "%dir2%"\"%%~nxG" disappeared
            exit /b 255
         )
         call :cmpdir "%%~fG" "%dir2%"\"%%~nxG"
         if !errorlevel! equ 255 goto :cmpdir_err
      )
      :cmpdir_ok
      popd
      exit /b 0

      :cmpdir_err
      popd
      exit /b 255

      Salmon Trout

      • Guest
      Re: Issues come from comparing two trees
      « Reply #3 on: June 16, 2013, 02:34:36 AM »
      What does this do...

      for /f "delims=" %%A in ('cd') do set dir2=%%A

      that this...

      set dir2=%cd%

      ...does not?

      Stan Huang

        Topic Starter


        Beginner

        • Experience: Experienced
        • OS: Windows 7
        Re: Issues come from comparing two trees
        « Reply #4 on: June 16, 2013, 07:07:15 PM »
        What you said is right. I need study script language deeper.

        patio

        • Moderator


        • Genius
        • Maud' Dib
        • Thanked: 1769
          • Yes
        • Experience: Beginner
        • OS: Windows 7
        Re: Issues come from comparing two trees
        « Reply #5 on: June 16, 2013, 08:23:37 PM »
        It's only been 2 Months...be patient.
        " Anyone who goes to a psychiatrist should have his head examined. "

        Stan Huang

          Topic Starter


          Beginner

          • Experience: Experienced
          • OS: Windows 7
          Re: Issues come from comparing two trees
          « Reply #6 on: June 16, 2013, 08:29:59 PM »
          What does it mean?