Compare 2 Lists

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
julesverne
Posts: 81
Joined: 19 Nov 2013 00:41

Compare 2 Lists

#1 Post by julesverne » 22 Feb 2014 13:17

Hi all, sorry, been busy with work so haven't been as active on the board. Have a few things I've been thinking about adding to my computer for awhile I'll be posting for help with. Here's one I've been working on that isn't working right. Any assistance would be awesome.

What I'm trying to do: Create a double click bat file that will compare 1 of 3 things.
1. Compare 2 directories.
2. Compare 1 text file that is a list of files created using dir cmd to a dir cmd output.
3. Compare 2 text files that are lists of files that have been created using dir cmd.
note: additionally, a log file option (.txt) is available for the user of the differences as well as a job summary.


Why I'm trying to do this: Option 1. find which is different about the two directories. Option 2. Occasionally I will get lists of files from a remote computer or server directory as a text file and I need to compare them to a local directory. Option 3. Occasionally I will get 2 lists of files i need to compare to each other.

What I have so far:

Code: Select all

@echo off

:menuoptionQUESTION
echo Compare Batch Program

echo 1. Compare 2 directories
echo 2. Compare a directory to a text file
echo 3. Compare 2 text files
echo/

set /p menuoption="Please choose an option above "
set /p createmenuoption="Would you like to create a txt file of the matches?" [y/N]

IF /I "%menuoption%"=="" GOTO :menuoptionQUESTION
IF /I NOT "%menuoption%"=="1" IF /I NOT "%menuoption%"=="2" IF /I NOT "%menuoption%"=="3" GOTO :menuoptionQUESTION

IF "%menuoption%"=="1" CALL :COMPARETWODIRECTORIES
IF "%menuoption%"=="2" CALL :COMPAREDIRANDTEXT
IF "%menuoption%"=="3" CALL :COMPARETWOTEXTFILES
IF "%menuoption%"=="3" GOTO :DOCOMPARE2

:DOCOMPARE1

setlocal enabledelayedexpansion
set /a counter=0
for /f %loopinfo% %%a in (%searchcommand%) do (
   for /f %%b in ('dir "%seconddirectory%" /b ^| findstr /I "%%~na"') do (
   set filecheck1=%%~na
   set filecheck2=%%~nb
   if not !filecheck1! EQU !filecheck2! echo !filecheck! is not in %seconddirectory%
   if /I %createmenuoption%==y echo %%a is in both folders >>%~dp0matchingfiles.txt
   echo %%a is in both folders
   set /a "counter+=1"
   pause
   )

)

ECHO Found !counter! matching files
if /I %createmenuoption%==y echo Found !counter! matching files >>%~dp0matchingfiles.txt

pause
GOTO :END

:DOCOMPARE2

copy %searchcommand% %searchcommand%copy.txt
copy %searchcommand2% %searchcommand2%copy.txt

setlocal enabledelayedexpansion
set /a counter=0
for /f %loopinfo% %%a in (%searchcommand%copy.txt) do (
   set loopbreak=n
   for /f %loopinfo% %%b in ('type %searchcommand2%copy.txt | findstr /v /c:"%%a"') do (
      if /I %%a==%%b (
         echo %%a is in both folders
         if /I %createmenuoption%==y echo %%a is in both folders >>%~dp0matchingfiles.txt
         set /a "counter+=1"
         set loopbreak=y
         type %searchcommand%copy.txt | findstr /v /c:"%%a"
      )
   )
   if !loopbreak!==n (
      echo %%a is not found in %searchcommand2%
      if /I %createmenuoption%==y echo %%a is not found in %searchcommand2% >>%~dp0matchingfiles.txt
   )
)

ECHO Found !counter! matching files
if /I %createmenuoption%==y echo Found !counter! matching files >>%~dp0matchingfiles.txt

pause


:END

EXIT


:COMPARETWODIRECTORIES

set /p firstdirectory="What is the path of the first directory? "
set searchcommand='dir "%firstdirectory%" /b'
set loopinfo="tokens=1"
set /p seconddirectory="What is the path of the second directory? "
GOTO :EOF

:COMPARETWOTEXTFILES

set /p menuoptionname=What is the path and filename of the first txt file?
set searchcommand=%menuoptionname%
set loopinfo="usebackq tokens=1"
set /p menuoptionname2=What is the path and filename of the second txt file?
set searchcommand2=%menuoptionname2%
set loopinfo2="tokens=*"
GOTO :EOF

:COMPAREDIRANDTEXT
set /p menuoptionname=What is the path and filename of the first txt file?
set searchcommand=%menuoptionname%
set loopinfo="usebackq tokens=1"
set /p seconddirectory="What is the path of the second directory? "
GOTO :EOF


What isn't working?: I don't believe I'm getting the differences. I believe the for loop inside the for loop is the issue.

Thanks in advance!

Post Reply