|
Answer» 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 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 ) ) ) )There is an old command line program called Comptree (ct.exe) which binary compares trees of files.
http://allan.hoiberg.dk/eng/prog_ct.htmI 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 255What does this do...
for /f "delims=" %%A in ('cd') do set dir2=%%A
that this...
set dir2=%cd%
...does not?What you said is right. I need study script language deeper.It's only been 2 Months...be patient.What does it mean?
|