@echo off
:choose
echo.&echo 请选择操作类型:
echo.&echo 1 - 比较两个文本并将相同内容输出
echo.&echo 2 - 比较两个文本并将不同内容输出
echo.&echo 3 - 两个文本内容合并后将相同内容只保留一个输出
echo.&echo 0 - 退出批处理
echo.&set choose=&set /p choose=选择要执行的序号:
if %choose% equ 0 exit
for /l %%a in (1,1,3) do if %choose% equ %%a (
call :setfile
call :%%a
cls&echo.&echo 处理完毕!继续选择操作类型:
goto :choose
)
cls&echo.&echo 输入的序号有误,请重新选择!&goto :choose
:setfile
echo.&set file1=&set /p file1=请输入要比较的第一个文件名:
set file1=%file1:"=%
echo.&set file2=&set /p file2=请输入要比较的第二个文件名:
set file2=%file2:"=%
echo.&set outfile=&set /p outfile=请输入最后结果保存的文件名:
set outfile=%outfile:"=%
goto :eof
:1
findstr /i /g:"%file1%" "%file2%">"%outfile%"
goto :eof
:2
call :1
findstr /i /v /g:"%outfile%" "%file1%">"%outfile%.bak"
findstr /i /v /g:"%outfile%" "%file2%">>"%outfile%.bak"
sort "%outfile%.bak">"%outfile%"
del "%outfile%.bak"
goto :eof
:3
(type "%file1%"&echo.&type "%file2%")>"%outfile%"
cd.>"%outfile%.bak"
for /f "delims=" %%i in ('type "%outfile%"') do (if not defined %%i set %%i=A & echo %%i>>"%outfile%.bak")
move /y "%outfile%.bak" "%outfile%"
goto :eof
@echo off
Setlocal EnableDelayedExpansion
pushd %~dp0echo.&set /p a=请指定文件a路径:
echo.&set /p b=请指定文件b路径:
set a=%a:"= %
set b=%b:"= %:menu
echo 1、比较a和b或多个文本内容,将按行排序的相同内容输出
echo 2、比较a和b或多个文本内容,将按行排序的不同内容输出
echo 3、合并并比较a和b或多个文本内容,且将按行排序的相同内容只保留一个输出
echo 4、直接退出
echo.&set /p choice=请选择以上操作序号:
echo 1234|find "%choice%">nul||(cls&echo.&echo 输入序号有误,请重新输入&goto :menu)
set n=0
call :%choice%
start temp.txt
echo.&echo 处理完毕,按任意键退出
pause>nul
:4
exit:1
set out=相同内容.txt
if !n! equ 0 cd.>"!out!"
for /f "delims=" %%a in ('type "%a%"') do (
set stra=%%a
call :b
if "!stra!"=="!strb!" (echo.!stra!>>"!out!")
set /a n+=1
)
goto :eof:2
set out=不同内容.txt
if !n! equ 0 cd.>"!out!"
for /f "delims=" %%a in ('type "%a%"') do (
set stra=%%a
call :b
if not "!stra!"=="!strb!" (echo.!stra!>>"!out!")
set /a n+=1
)
goto :eof:3
set out=合并内容.txt
if !n! equ 0 cd.>"!out!"
for /f "delims=" %%a in ('type "%a%"') do (
set stra=%%a
call :b
echo.!stra!>>"!out!"
if "!stra!"=="!strb!" (echo.!strb!>>"!out!")
set /a n+=1
)
goto :eof:b
for /f "delims=" %%b in ('more +!n! "%b%"') do (set strb=%%b&goto :eof)