Solved

comparing directories

Posted on 2004-08-26
4
161 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
  • 2
  • 2
4 Comments
 
LVL 83

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 83

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Server Hard Drive Expansion 2 142
Windows 2000 Kerberos problem 5 312
Windows 2003 server: List of EVENT IDs 1 714
My production physical server goes down, Please help. 11 92
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…
When we talk about DevOps toolchains, I sometimes wonder how many people really get what we’re talking about. I don’t know if it’s just semantics or tone or something else, but sometimes I think it just sounds like buzzword sausage. So it’s always …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

914 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now