批处理文件,用于搜索两个文本文件中的常用字符

时间:2015-06-13 12:11:54

标签: batch-file cmd

我有两个文本文件, all_codes.txt downloaded_codes.txt 我想比较两个文本文件并记录 downloaded_codes.txt中是否缺少任何字符串

> all_codes.txt 将包含以下字符串

A1
A2
A3

> Downloaded_codes.txt 将包含以下字符串

A1
A3

在此示例中, downloaded_codes.txt 中缺少A2,因此我想记录"否"在 log.txt 中 如果A2出现在 downloaded_codes.txt 中,那么我想记录"是"在 log.txt

这是我尝试的,但没有得到正确的结果:

@echo off
setlocal enabledelayedexpansion

for / f "delims=~" % % h in (All_codes.txt) do
    for % % a in (downloaded_codes.txt) do(
        set found = false
        for / f "skip=2 tokens=*" % % b in ('findstr /i "%%h" "%%a"') do(
          if "!found!" == "false" ((echo YES > log.txt
            else(echo NO > log.txt)
            set found = true
          ))
        )

1 个答案:

答案 0 :(得分:0)

@echo off

findstr /V /G:Downloaded_codes.txt all_codes.txt > NUL
(if errorlevel 1 (echo YES) else echo NO) > log.txt

编辑:这是一个示例会话:

C:\> type test.bat
@echo off

findstr /V /G:Downloaded_codes.txt all_codes.txt > NUL
(if errorlevel 1 (echo YES) else echo NO)

C:\> type all_codes.txt
A1
A2
A3

C:\> type Downloaded_codes.txt
A1

C:\> test
NO

C:\> echo A3>> Downloaded_codes.txt

C:\> type Downloaded_codes.txt
A1
A3

C:\> test
NO

C:\> echo A2>> Downloaded_codes.txt

C:\> type Downloaded_codes.txt
A1
A3
A2

C:\> test
YES