Reputation: 151
text1.txt
aaa aaa aa1
bbb bbb bb2
ccc ccc cc3
ddd ddd dd4
text2.txt
000004
aaa aaa aa1
bbb bbb bb2
ccc ccc cc3
ddd ddd dd4
END
I got two files. Let focus on text2.txt first. 000004 means there are 4 effective lines.
And what i want to do is to use batch to skip first line untill END and compare the middle four lines with text1.txt contents.
It may involve loops or nested loop to compare each line line by line. And if the middle 4 lines match the text1.txt exactly,
echo valid file
else (echo invalid file)
Any ideas? Really Thank you in advance.
Upvotes: 0
Views: 240
Reputation: 151
Here is my own coding by referring to google, i used array method. Everyone, let's have a look, i have tried it and it can verify whether two files are the same.I add some extra echo to show that some arrays are null.Make sure two text files are in the same directory.
@echo off
setlocal enabledelayedexpansion
set textfile1=text1.txt
set textfile2=text2.txt
rem Verify textfile1 and textfile2
echo Now check whether text1.txt is the same as the text2.txt.
echo.
echo Read in textfile2 into array2
set arr_counter2=0
for /f "skip=1 delims==" %%a in (%textfile2%) do (
if "%%a" equ "END" (
echo You reach "END"
goto :break1
) else (
echo !arr_counter2!
set arr2[!arr_counter2!]=%%a
rem echo %%array2[!index!]%%
set /a arr_counter2+=1
echo !arr_counter2!
pause
)
)
:break1
echo number of elements in array = !arr_counter2!
echo !arr2[0]!
echo !arr2[1]!
echo !arr2[2]!
echo !arr2[3]!
echo !arr2[4]!
echo !arr2[5]!
echo !arr2[6]!
echo !arr2[7]!
echo !arr2[8]!
echo !arr2[9]!
pause
echo.
echo Read in textfile1 into array1
set arr_counter1=0
for /f "delims==" %%b in (%textfile1%) do (
rem if "%%a" equ "END" (
rem echo You reach "END"
rem goto :break1
rem ) else (
echo !arr_counter1!
set arr1[!arr_counter1!]=%%b
rem echo %%array1[!index!]%%
set /a arr_counter1+=1
echo !arr_counter1!
pause
)
)
echo number of elements in array = !arr_counter1!
echo !arr1[0]!
echo !arr1[1]!
echo !arr1[2]!
echo !arr1[3]!
echo !arr1[4]!
echo !arr1[5]!
echo !arr1[6]!
echo !arr1[7]!
echo !arr1[8]!
echo !arr1[9]!
pause
echo.
set /a final_index=!arr_counter1!-1
rem compare each element from [0] to [final_index]
for /l %%n in (0,1,!final_index!) do (
rem echo !arrayline[%%n]!
if !arr1[%%n]! equ !arr2[%%n]! (
echo same content.
pause
) else (
echo wrong!
pause
goto :eof
)
)
echo all contents are the same
pause
Upvotes: 0
Reputation: 1061
Answer to KenyKeny question.
call :Next_loop Next_content !skip_count! !access_bool! check_lines_in_loop
Basically it's calling :Next_loop
with 4 arguments: Next_content, !skip_count!, etc...
Arguments with no "! !" signs
1. Arguments like this is variable that haven't been assigned yet, like an unknown variable.
2. You can assign a value to it in :Next_loop
by set %1=hello
(%1 will be discuss later on)
Arguments with "! !" signs
1. Arguments like !skip_count!
is an assigned variable, which has value in it.
2. You can use this argument in :Next_loop
like echo %2
since this argument had been assigned before.
3. You can also assign a new value to it.
What is %1, %2, %~1, etc.. ?
1. It represents value passed by arguments. %1
indicates the first argument(Next_content), where %2
indicates the second argument(!skip_content!)
2. %~1
is basically a quotation(" ") mark filter. E.g :
@echo off
call :function "hello"
:function
echo %1
echo %~1
First line echo out "hello" where second line filtered out quotation marks and left only hello.
Upvotes: 1
Reputation: 1061
I see, now I know the use of 000004. Make sure the "4" is indicating lines available, if 5th row from text2.txt and text1.txt is matched but the first line is 000004, it may returns Invalid file.
@echo off
setlocal enabledelayedexpansion
set bool=1
set /a count=0
set /a count_true=0
set /a skip_count=0
set access_bool=0
for /f "tokens=*" %%a in (text2.txt) do (
if !bool! == 0 (
call :Next_loop Next_content !skip_count! !access_bool! blank_param
set /a skip_count+=1
if %%a == !Next_content! (
set /a count_true+=1
)
)
if !bool! == 1 (
set first_line=%%a
set lines_to_skip=!first_line:~-1,1!
call :Next_loop Next_content !skip_count! !access_bool! check_lines_in_loop
if !check_lines_in_loop! gtr !lines_to_skip! goto result
if !check_lines_in_loop! lss !lines_to_skip! goto result
set bool=0
)
)
:result
if !count_true! == !lines_to_skip! (
echo Valid file!
) else (echo Invalid file!)
pause >nul
:Next_loop
set /a check_lines
set /a Next_count=0
for /f "tokens=*" %%b in (text1.txt) do (
if %3 == 0 (
set /a check_lines+=1
)
if !Next_count! == %2 (
set %1=%%b
)
set /a Next_count+=1
)
if %3 == 0 (
set %4=!check_lines!
set %3=1
)
Upvotes: 1