Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

comparing directories

Posted on 2004-08-26
4
Medium Priority
?
170 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 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 1000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
The decision to migrate to the cloud is not a simple one—many factors, such a cost, ease of use, and ongoing maintenance come into play. The goal is always for cloud platforms to make storage and backups more seamless and effective. Here’s a look at…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

783 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