Nishant Kansal
Nishant Kansal

Reputation: 601

Batch File to compare value in the first line in each of the 2 different txt files

I have 2 files (say A.txt abd B.txt) having a numeric value in the first line of each (say "8" in A.txt and "9" in B.txt).

I have to compare the value at the first line only between the 2 files and based upon whether numeric value in A.txt is greater than B.txt or not, have to call another batch file.

Could anyone please help me in achieving it?

Upvotes: 0

Views: 367

Answers (1)

rojo
rojo

Reputation: 24466

One way to set a variable to a line in a text file is to use set /P with a redirect.

@echo off
setlocal

set /P "a=" <"A.txt"
set /P "b=" <"B.txt"

if %a% gtr %b% call anotherbatchfile.bat
goto :EOF

If it wasn't the first line you wanted to set, but, say, the 5th, you'd just use several set /P commands within the same redirection, something like this:

<"A.txt" (
    set /P "a="
    set /P "a="
    set /P "a="
    set /P "a="
    set /P "a="
)

rem # or...

<"A.txt" (
    for /L %%I in (1,1,5) do set /P "a="
)

Or you could use for /f to read a text file. help for in a console window for more info. Just for giggles, I'll also demonstrate how to use call to define a function within a batch script.

@echo off
setlocal

for %%I in (a b) do call :setfirstline "%%I.txt" %%I

if %a% gtr %b% call anotherbatchfile.bat
goto :EOF

:setfirstline <txtfile> <var_to_return>
for /f "usebackq delims=" %%I in ("%~1") do (set "%~2=%%I" & goto :EOF)

Upvotes: 1

Related Questions