comparing directories

Does anyone have a good way in windows 2000, perhaps with the resource kit, or WMI, or VB to compare the files in 2 directories and give the balance of the 2 in a text file?  Even seeing it on the screen would be helpful.

Thanks!

Scott
LVL 5
scottman29Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
oBdAConnect With a Mentor Commented:
Seems like you did it OK.
Let's try a step-by-step.
Open a command prompt, and type
> C:
> cd \
(leave out the next command if you already have a directory "C:\temp")
> md Temp
Now enter
> cd temp
> notepad CompareDir.cmd
Copy the script (without the cutting lines, obviously) into the clipboard, then paste it into notepad. Adjust the paths, save the changes. Go back to the command window, and enter
> comparedir
After it has run, you'll have a file "CompareDir.log" in the temp directory, which will again list the files, but sorted.
Here's an updated version that checks the file size as well:

====8<----[CompareDir.cmd]----
@echo off
:: *** First directory to compare:
set Dir1=D:\Test\A
:: *** Second directory to compare:
set Dir2=D:\Test\B
:: *** Path and name of the log file:
set LogFile=%~dpn0.log

set TempDir1File=%~dpn0-Dir1.tmp
set TempDir2File=%~dpn0-Dir2.tmp
set TempSameFile=%~dpn0-Same.tmp
set TempDiffFile=%~dpn0-Date.tmp
if exist "%LogFile%" del "%LogFile%"

> "%TempDir1File%" echo ===========================================================================
>>"%TempDir1File%" echo == Files only in "%Dir1%"
>>"%TempDir1File%" echo ===========================================================================
> "%TempDir2File%" echo ===========================================================================
>>"%TempDir2File%" echo == Files only in "%Dir2%"
>>"%TempDir2File%" echo ===========================================================================
> "%TempSameFile%" echo ===========================================================================
>>"%TempSameFile%" echo == Identical files in "%Dir1%" and "%Dir2%"
>>"%TempSameFile%" echo ===========================================================================
> "%TempDiffFile%" echo ===========================================================================
>>"%TempDiffFile%" echo == Same name, different date/size in "%Dir1%" and "%Dir2%"
>>"%TempDiffFile%" echo ===========================================================================

for /f "delims=" %%a in ('dir /b /a:-d /o "%Dir1%"') do (
  if not exist "%Dir2%\%%a" (
    echo Dir1: %%a
    >>"%TempDir1File%" echo %%a
  ) else (
    call :process "%Dir1%\%%a"
  )
)

for /f "delims=" %%a in ('dir /b /a:-d /o "%Dir2%"') do (
  if not exist "%Dir1%\%%a" (
    echo Dir2: %%a
    >>"%TempDir2File%" echo %%a
  )
)

for %%a in ("%TempDir1File%" "%TempDir2File%" "%TempSameFile%" "%TempDiffFile%") do (
  >>"%LogFile%" type %%a
  del %%a
)
goto leave

:: ********************************************************************************
:process
set FileName=%~nx1
set File1Date=%~t1
set File1Size=%~z1
for %%a in ("%Dir2%\%FileName%") do (
  set File2Date=%%~ta
  set File2Size=%%~za
)
if not "%File1Date%"=="%File2Date%" (
  echo Diff: %FileName%
  >>"%TempDiffFile%" echo %FileName%
  goto :eof
)
if not "%File1Size%"=="%File2Size%" (
  echo Diff: %FileName%
  >>"%TempDiffFile%" echo %FileName%
  goto :eof
)
echo Both: %FileName%
>>"%TempSameFile%" echo %FileName%
goto :eof
:: ********************************************************************************

:leave
====8<----[CompareDir.cmd]----
0
 
oBdACommented:
Assuming that with "compare the files", you mean the directory listing, something like that should work for you:

====8<----[CompareDir.cmd]----
@echo off
:: *** First directory to compare:
set Dir1=D:\Test\A
:: *** Second directory to compare:
set Dir2=D:\Test\B
:: *** Path and name of the log file:
set LogFile=%~dpn0.log

set TempDir1File=%~dpn0-Dir1.tmp
set TempDir2File=%~dpn0-Dir2.tmp
set TempSameFile=%~dpn0-Same.tmp
set TempDateFile=%~dpn0-Date.tmp
if exist "%LogFile%" del "%LogFile%"

> "%TempDir1File%" echo ===========================================================================
>>"%TempDir1File%" echo == Files only in "%Dir1%"
>>"%TempDir1File%" echo ===========================================================================
> "%TempDir2File%" echo ===========================================================================
>>"%TempDir2File%" echo == Files only in "%Dir2%"
>>"%TempDir2File%" echo ===========================================================================
> "%TempSameFile%" echo ===========================================================================
>>"%TempSameFile%" echo == Identical files in "%Dir1%" and "%Dir2%"
>>"%TempSameFile%" echo ===========================================================================
> "%TempDateFile%" echo ===========================================================================
>>"%TempDateFile%" echo == Different date in "%Dir1%" and "%Dir2%"
>>"%TempDateFile%" echo ===========================================================================

for /f "delims=" %%a in ('dir /b /a:-d /o "%Dir1%"') do (
  if not exist "%Dir2%\%%a" (
    echo Dir1: %%a
    >>"%TempDir1File%" echo %%a
  ) else (
    call :process "%Dir1%\%%a"
  )
)

for /f "delims=" %%a in ('dir /b /a:-d /o "%Dir2%"') do (
  if not exist "%Dir1%\%%a" (
    echo Dir2: %%a
    >>"%TempDir2File%" echo %%a
  )
)

for %%a in ("%TempDir1File%" "%TempDir2File%" "%TempSameFile%" "%TempDateFile%") do (
  >>"%LogFile%" type %%a
  del %%a
)
goto leave

:: ********************************************************************************
:process
set File1Date=%~t1
set FileName=%~nx1
for %%a in ("%Dir2%\%FileName%") do set File2Date=%%~ta
if "%File1Date%"=="%File2Date%" (
  echo Both: %FileName%
  >>"%TempSameFile%" echo %FileName%
) else (
  echo Date: %FileName%
  >>"%TempDateFile%" echo %FileName%
)
goto :eof
:: ********************************************************************************

:leave
====8<----[CompareDir.cmd]----
0
 
scottman29Author Commented:
Thanks,

I copied the code into notepad, and called it comparedir.cmd and placed it in my c drive.

Then I went to the command prompt (cmd in w2k which is what I'm on) and tried to execute the file by typing it's name, typing cmd and then it's name.  I get "the syntax of the command is incorrect".

I did change the names of the directories in the code to reflect what directories I have on my system.

I also tried putting the cmd file in my system32 folder thinking maybe it had to do with my path statement.

Thanks ODBA, can you help with this?  I'm sure it's a simple os issue.

Yours,
Scott
0
 
scottman29Author Commented:
THAT WAS GREAT THANKS!!!!!
0
All Courses

From novice to tech pro — start learning today.