批处理文件进行比较的文本文件的内容 [英] Batch file to compare contents of a text file

查看:651
本文介绍了批处理文件进行比较的文本文件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含以下数字两个文本文件

I have two text files which contain the below numbers

File1
00000
11111

File2
00000
11111
22222

我需要一个code其中比较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.

总之我想删除文件2的内容,并把非匹配的内容在文件2。下面是code我已经试过,但它只是删除了文件2整个事情。

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 选项errorlevels。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆