批处理文件有条件地将项目从一个文本复制到另一个文本

时间:2014-08-06 14:14:08

标签: batch-file

我只想在file1.txt中包含file2.txt中不存在的其他行时才从file1.txt复制到file2.txt。

说,file1.txt如下所示。

*createbutton(5,5,0,6,BUTTON,"",EvalTcl,"Bolt.tcl")
*createbutton(5,"Help",5,6,4,BUTTON,"",EvalTcl,"help.tcl")
*createbutton(5,newline)

和file2.txt如下

*createbutton(5,5,0,6,BUTTON,"",EvalTcl,"Bolt.tcl")
*createbutton(5,"Help",5,6,4,BUTTON,"",EvalTcl,"help.tcl")

然后它应该只复制file1.txt中的第3行,即.." * createbutton(5,newline)"到file2.txt,因为file2.txt中已存在前2行。

我希望我明白我的要求。欢迎任何帮助。

谢谢你, 问候, Prasad Poduval

@echo off
setlocal disableDelayedExpansion
set "file=%~1"
set "line=%file%.line"
set "deduped=%file%.deduped"
::Define a variable containing a linefeed character
set LF=^


::The 2 blank lines above are critical, do not remove
>"%deduped%" (
for /f usebackq^ eol^=^%LF%%LF%^ delims^= %%A in ("%file%") do (
set "ln=%%A"
setlocal enableDelayedExpansion
>"%line%" (echo !ln:\=\\!)
>nul findstr /xlg:"%line%" "%deduped%" || (echo !ln!)
endlocal
)
)
>nul move /y "%deduped%" "%file%"
2>nul del "%line%"

2 个答案:

答案 0 :(得分:0)

您所描述的内容基本上与diff utility完全相同。

答案 1 :(得分:0)

@ECHO OFF
SETLOCAL
FOR /f "delims=" %%a IN ('type q25162700_1.txt^|findstr /g:q25162700_2.txt /v ') DO (
 >>q25162700_2.txt ECHO %%a
)

GOTO :EOF

我使用了包含您的数据的q25162700_1.txtq25162700_2.txt文件进行测试。