Tuesday, September 15, 2009

File Comparison Batch Script

at work, i needed to compare the files in the mounted ISO image to the extracted raw files. there are several tools which compares directories like the built-in fc.exe of windows and other freewares but what i needed is something simple which would only check for the files on the the same folder location which is just relative to the root location. for example, the mounted ISO image is my reference subject and the files in it should be found on the extracted file structure of the same relative folder location.

here's what i've came up. posting it here for future reference and for those who might have the same needs.

listing: compdir.bat
---------------
::reference dir
@set REFDIR=%~1
::this is the copied directory which should be checked
@set CMPDIR=%~2
::call the root first
@call :CMPPROC
@goto XXX

:CMPPROC
@set MRGPATH=%REFDIR%%~1
@echo RefDir: %MRGPATH%
@echo TarDir: %CMPDIR%%~1
@echo ------------------------------------
::check on the files
@for /f "usebackq tokens=* delims=" %%i in (`@dir /b/a:-d "%MRGPATH%"`) do @if exist "%CMPDIR%%~1\%%i" (@call :CMPFILE "%%i" "%MRGPATH%\%%i" "%CMPDIR%%~1\%%i" ) else (@echo [NG] "%%i")
@echo ------------------------------------
::iterate on the sub directories
@for /f "usebackq tokens=* delims=" %%i in (`@dir /b/a:d "%MRGPATH%"`) do @call :CMPPROC "%~1\%%i"
@goto :EOF

:CMPFILE
@fc /B "%~2" "%~3" >nul
@if %ERRORLEVEL%==0 (@echo [OK] "%~1") else (@echo [NG] "%~1")
@goto :EOF

:XXX
@echo Done

---------------

No comments: