实施例
(
Line1: echo echo example
Line2: echo echo example
Line3: echo echo example
Line4: echo echo File Example
) > File1.txt
(
Line1: echo echo example
Line2: echo echo example
Line3: echo echo File
Line4: echo echo File
) > File2.txt
想要比较Line4是否相等,但是 丢弃其他线路。在这种情况下返回false
答案 0 :(得分:2)
您可以使用数组来比较这种类型的文件:
@echo off
(
echo Line1:example
echo Line2:example
echo Line3:example
echo Line4:File
)> File1.txt
(
echo Line1:example
echo Line2:example
echo Line3:File
echo Line4:example
)> File2.txt
setlocal enabledelayedexpansion
set i=0
for /f "tokens=* delims=" %%a in ('Type "File1.txt"') do (
set /a i+=1
set File1_Line[!i!]=%%a
)
set File1_Line[4]
pause
::********************************************************
set i=0
for /f "tokens=* delims=" %%a in ('Type "File2.txt"') do (
set /a i+=1
set File2_Line[!i!]=%%a
)
set File2_Line[4]
pause
::********************************************************
echo File1_Line[4]" = !File1_Line[4]!"
pause
echo File2_Line[4] = "!File2_Line[4]!"
pause
::********************************************************
IF /I "!File1_Line[4]!" equ "!File2_Line[4]!" ( echo TRUE
) else (
echo False
)
pause
答案 1 :(得分:1)
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q36252686.txt"
SET "filename2=%sourcedir%\q36252686_2.txt"
:: This part simply establishes the data IN the files
FOR %%a IN ("%filename1%" "%filename2%") DO DEL %%a 2>nul
FOR %%a IN (
"Line1: echo echo example"
"Line2: echo echo example"
"Line3: echo echo example"
"Line4: echo echo File Example"
) DO >> "%filename1%" ECHO(%%~a
FOR %%a IN (
"Line1: echo echo example"
"Line2: echo echo example"
"Line3: echo echo File"
"Line4: echo echo File"
) DO >> "%filename2%" ECHO(%%~a
:: line to match
SET /a matchonline=4
:: lines to skip
SET /a skiplines=matchonline-1
SET "skiplines=skip=%skiplines%"
IF %matchonline%==1 SET "skiplines="
FOR /f "usebackq%skiplines%delims=" %%a IN ("%filename1%") DO (
FOR /f "usebackq%skiplines%delims=" %%b IN ("%filename2%") DO (
IF "%%a"=="%%b" (SET "result=true") ELSE (SET "result=false")
GOTO done
)
)
:done
ECHO match ON line %matchonline%=%result%
GOTO :EOF
您需要更改filename1
和filename2
的设置以适合您的具体情况。我将数据生成到我系统上方便的文件名中进行测试。
读取第一个文件,跳过要匹配的行之前的行,并将其内容分配给%%a
。发生这种情况时,请对第二个文件重复%%b
设置结果并跳出循环。