Telson Alva
Telson Alva

Reputation: 862

Batch file to compare contents of a text file

I have two text files which contain the below numbers

File1
00000
11111

File2
00000
11111
22222

I need a code which compares the contents of file2 to that of file1 and the numbers which are not matched , in this case '22222' be the only content in file2.

In short i want to erase the content of file2 and put the non matched content in file2. Below is the code i have tried but it just erases the entire thing in the file2.

setlocal enabledelayedexpansion

for /f "tokens=1" %%a in (file1) do (type file2 | findstr /v %%a > file2)

pause

Bottom line i need to achieve the below results

File1
00000
11111

File2
22222

Help please !

Upvotes: 1

Views: 14125

Answers (4)

Shirish
Shirish

Reputation: 47

This can be one of the solution:

findstr /V /G:file1.txt file2.txt

Upvotes: -1

dbenham
dbenham

Reputation: 130919

I think this is the simplest and fastest native batch solution

findstr /vixg:"File1" "File2" >"File2.new"
move /y "File2.new" "File2"

Note that I used the findstr /i case insensitive option. This is important because of a findstr bug that can lead to missed matches when searching for multiple literal strings unless the /i or /r options are used. The case insensitive workaround should not impact you since you are dealing with numbers.

Upvotes: 7

wmz
wmz

Reputation: 3685

Different approach:

@echo off
setlocal 
for /f %%i in (file1) do set %%i=%%i
for /f %%j in (file2) do if not defined %%j echo %%j 

It has following characteristics:
- scans each of the files only once
- duplicates in file1 will be effectively ignored
- you may load file1 into memory and then run several file_x against it to test.

Notes:
I simply echo unique nums to console, you need to change to to write to file
Assumes you compare words and there is one word per line
It's limited by maximum number of variables possible to define. I have no clue what that number is... I tried it with 20 000 lines in file1 (so 20 000 vars defined)

Upvotes: 2

azhrei
azhrei

Reputation: 2323

This does the job. It hinges around using findstr /m option and errorlevels.

Also it writes to a temporary file during processing (and cleans it).


@echo off
setlocal enabledelayedexpansion
echo Stripping duplicates in file2 (%2) compared to lines in file1 (%1)

if not exist "%1" (
  echo That first file doesn't exist.
  exit /b 1
)

if not exist "%2" (
  echo That second file doesn't exist.
  exit /b 2
)

REM Create empty temporary file
echo. 2> %2.tmp

for /f "tokens=1" %%a in (%2) do (
  echo Processing %%a
  >nul call findstr /m "%%a" %1
  if errorlevel 1 (
    echo OK
    >> %2.tmp echo %%a
  ) else (
    echo Duplicate!
  )
)


echo Writing results to %2
xcopy /y /q %2.tmp %2
del /q %2.tmp

Upvotes: 4

Related Questions