Solved

comparing directories

Posted on 2004-08-26
4
166 Views
Last Modified: 2010-04-14
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
0
Comment
Question by:scottman29
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 85

Expert Comment

by:oBdA
ID: 11908297
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
 
LVL 5

Author Comment

by:scottman29
ID: 11913451
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
 
LVL 85

Accepted Solution

by:
oBdA earned 250 total points
ID: 11916972
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
 
LVL 5

Author Comment

by:scottman29
ID: 11917159
THAT WAS GREAT THANKS!!!!!
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question