1

I have been searching through StackOverflow but could not find an answer that hits the mark. I have 2 .txt files to compare and return a 3rd one where differences exist.

However, only the first column of the first 2 files need a comparison.

E:\Compare_flie\file_1.txt

GND ZERO
22XC44    XXYYZZ
33XC55    YYUUTT

E:\Compare_file\file_2.txt

GND ZERO
22XC44    KK77UU
33XC55    88JJ66
66NN77    HHOO99
99CC88    UU77RR

E:\Compare_file\file_3.txt (intended output)

66NN77    HH0099
99CC88    UU77RR

Tried the code below but it is only good at picking out the differences of all the strings in the line

%echo on
findstr /v /i /g:E:\Compare_files\file_1.txt E:\Compare_files\file_2.txt 
> E:\Compare_files\file_3.txt

Refined it further but not hitting the mark yet.

%echo on
for /f "tokens=1 delims= " %%I in ("E:\Compare_files\file_1.txt") do 
findstr /v /i "%%I"/g:"D:\Compare_files\file_2.txt" 
> "D:\Compare_files\file_3.txt"

Appreciate if anyone can assist.

WebDevBooster
  • 14,674
  • 9
  • 66
  • 70
Muzzy Izzy
  • 13
  • 4

3 Answers3

1
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q48816766.txt"
SET "filename2=%sourcedir%\q48816766_2.txt"
SET "tempfile=%temp%\q48816766.tmp"
SET "outfile=%destdir%\outfile.txt"
(FOR /f "usebackq" %%a IN ("%filename1%") DO ECHO %%a )>"%tempfile%"
FINDSTR /b /v /g:"%tempfile%" "%filename2%">"%outfile%"
REM DEL "%tempfile%" /F /Q

GOTO :EOF

I've set up names to suit my system, but with the two files containing your data.

Obviously, the usebackq on the for/f is only required if the filename is quoted. The parentheses around the command permit the echoed output to be accumulated into the temporary file. What's important here is the space between the %%a and ). This ensures that the temporary file contains trailing spaces.

Then apply the temporary file to the second data file via /g as in OP's code. The presence of the trailing spaces in the tempfile ensure that the only lines selected for omission are those where the first column exactly matches so for instance had 66NN7 appeared in the first file, first column, then this would not match 66NN77 in the second file.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Thank you @Magoo. Your code works great and if I were to add in more words or spaces within each lines, they still work. – Muzzy Izzy Feb 17 '18 at 11:20
0

Here's a method using batch with the type command piping the first file's contents over to the findstr command then passing the arguments accordingly to redirect those results into a temp file.

Using a for /f loop with "usebackq tokens=1 delims= " it will iterate through the temp file and for each line in that file parsing accordingly, it will append the column one lines with an echo command using >> to redirect the results over to file_3.txt with the expected results.

Please note the addition of the if exist "%srcdir%\file_3.txt" del /q /f "%srcdir%\file_3.txt" to delete that file if it exists since the for /f echo commands will append one after the other to it.

@echo on
set srcdir=E:\Compare_files
set tmpfile=%temp%\%~N0.tmp

type "%srcdir%\file_1.txt" | findstr /vig:"%srcdir%\file_2.txt">"%tmpfile%"

if exist "%srcdir%\file_3.txt" del /q /f "%srcdir%\file_3.txt"
for /f "usebackq tokens=1 delims= " %%I in ("%tmpfile%") do (
    echo %%~I>>"%srcdir%\file_3.txt"
)

Further Resources

Bitcoin Murderous Maniac
  • 1,209
  • 1
  • 14
  • 27
  • Thank you @Bitcoin. The code works just as I needed. However I do find it a bit challenging if I were to add more words and spaces within the lines. Nonetheless, this works great. – Muzzy Izzy Feb 17 '18 at 11:17
0

As I understand your problem, you want the lines from file_2.txt whose first column are not contained in first column of file_1.txt, that is: file_2.txt minus file_1.txt. There is a simpler approach to get such result:

@echo off
setlocal

rem Fill "line" array with lines from file_2.txt
rem use the first column for the array keys
for /F "delims=" %%a in (file_2.txt) do for /F %%b in ("%%a") do set "line[%%b]=%%a"

rem Delete array elements with same key from file_1.txt
for /F %%b in (file_1.txt) do set "line[%%b]="

rem Show remaining elements
(for /F "tokens=1* delims==" %%a in ('set line[') do echo %%b) > file_3.txt
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Thank you @Aacini. The code works great and as per intended. Quick one though, why does the file directory always precede each line in the out output result? Could it have been added in to array? – Muzzy Izzy Feb 17 '18 at 11:19
  • No. The `for /F` command just read the contents of the file. Did you removed the `@echo off` line? – Aacini Feb 17 '18 at 18:18