Search code examples
batch-filecmdcomparedosfindstr

Compare TXT files and get missing words


I have 2 txt files, the first is the result of a json file and the second must contain the content of the JSON + the content of an other TXT file.

database1.txt

word1
word2
word3
word8

Database2.txt (from JSON)

word1
word5
word7
word8

Database3.txt (Database1+Database2)

Word1
Word2
Word3
Word5
Word7
Word8

Here is my CODE:

@ECHO OFF
setlocal enabledelayedexpansion
IF EXIST "%LOCALAPPDATA%\xxx\xxx\database.json". (

for /f "delims=" %%a in ('type "%LOCALAPPDATA%\xxx\xxx\database.json"') do for %%b in (%%a) do (
ECHO %%b >>json.tmp
)

for /f "tokens=* skip=1 delims= " %%a in (json.tmp) do (
call :sub1 %%a

>> Json_cl.txt echo.!S!
)

set row=
for /F "delims=" %%j in (Json_cl.txt) do (
  if  defined row echo.!row!>>Password_JD.txt
  set row=%%j
)


findstr /V /g:"Password_list.txt" "Password_JD.txt">1.out
        type Password_list.txt 1.out>Updated_PW.txt

del Json_cl.txt
del json.tmp
del Password_JD.txt
del 1.out
goto :eof

:sub1
set S=%*
set S=!S:"=!

goto :eof
)

The code works well but sometimes looks like if the FINDSTR dosnt find missing word.

Can someone help me to fix it or can tell me a better way to compare ?

Thank you


Solution

  • This script uses a robust tool called Uniq.bat by aacini

    @echo off
    copy database1.txt + database2.txt tmp.txt >nul
    type tmp.txt | sort |uniq >database3.txt
    del tmp.txt
    

    UNIQ.BAT

    @if (@CodeSection == @Batch) @then
    
    @CScript //nologo //E:JScript "%~F0" & goto :EOF & Rem aacini 2013
    
    @end
    
    var line, prevLine = "";
    while ( ! WScript.Stdin.AtEndOfStream ) {
       line = WScript.Stdin.ReadLine();
       if ( line != prevLine ) {
          WScript.Stdout.WriteLine(line);
          prevLine = line;
       }
    }