用于比较文本文件内容的批处理文件 [英] 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
我需要一个代码,将 file2 的内容与 file1 的内容以及不匹配的数字进行比较,在这种情况下,'22222' 是 file2 中唯一的内容.
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.
简而言之,我想删除 file2 的内容并将不匹配的内容放入 file2.下面是我尝试过的代码,但它只是删除了 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
请帮忙!
推荐答案
这样就可以了.它取决于使用 findstr/m
选项和错误级别.
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
这篇关于用于比较文本文件内容的批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!