2012-07-12 177 views
1

我有包含以下数字批处理文件来比较文本文件的内容

File1 
00000 
11111 

File2 
00000 
11111 
22222 

我需要哪些文件2的内容进行比较,以该文件1的代码两个文本文件和不匹配的数字,在这种情况下,'22222'是file2中唯一的内容。

总之我想擦除file2的内容并将不匹配的内容放在file2中。下面是我试过的代码,但它只是擦除file2中的所有内容。

setlocal enabledelayedexpansion 

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

pause 

底线我需要实现以下结果

File1 
00000 
11111 

File2 
22222 

请帮助!

回答

3

这样做的工作。它取决于使用findstr /m选项和错误级别。

它也在处理(并清除它)期间写入临时文件。


@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 
+0

Azhrei的脚本的作品完美... Thnks! – 2012-07-13 18:06:56

2

不同的方法:

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

它具有以下特点:
- 扫描每个文件的唯一一次
- 在文件1副本将被有效地忽略
- 你可以加载file1到内存中,然后运行几个file_x对其进行测试。

注:
我只是附和独特NUMS安慰,你需要更改写入文件
假设你比较并有每行一个字
它是由限制可以定义的最大变量数量。我不知道那是什么号码是我在文件1 20条000线(所以20个000瓦尔定义)

6

尝试它,我觉得这是最简单和最快的原生批量解决方案

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

注我使用了findstr /i不区分大小写的选项。这很重要,因为findstr bug在搜索多个文字字符串时会导致错过匹配,除非使用/i/r选项。由于您正在处理数字,因此不区分大小写的解决方法不应影响您。

+0

+1用于包含有关错误的注释。 – wmz 2012-07-14 09:13:31

+0

Awsome !!谢谢dbenham ... – 2012-07-16 14:55:43

0

这可以成为解决方案之一:

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