Reputation:
@echo off
setlocal EnableDelayedExpansion
set numb=99999
for /f "tokens=*" %%l in (numbers.txt) do (
if not !numb! equ 0 (
if %%l LSS !numb! set "numb=%%l"
) else (
set "numb=%%l"
)
)
echo %numb%
I want a more elegant or better way for getting the lowest number in a textfile. The textfile looks like this(numbers.txt):
42142
242
4242
2421
152
321
1214
123
3424
1221
Upvotes: 0
Views: 75
Reputation: 6568
Actually what you have posted would always show 0
as the result because you are initializing the variable to 0
which is already lower than any value you are comparing to.
That aside, batch doesn't really have "query" functionality, so you essentially have to iterate through every value. Your code is pretty much right on, but if you want to clean it up a bit, you could try this:
@echo off
setlocal EnableDelayedExpansion
REM Initialize to a high value.
set numb=99999
for /f "tokens=*" %%l in (numbers.txt) do (
REM Only need to compare the current against the processing value.
if %%l LSS !numb! set "numb=%%l"
)
echo %numb%
endlocal
Alternately, you could shorten your entire FOR
statement:
@echo off
setlocal EnableDelayedExpansion
set numb=99999
for /f %%l in (numbers.txt) do if %%l LSS !numb! set "numb=%%l"
echo %numb%
endlocal
Upvotes: 1