Telson Alva
Telson Alva

Reputation: 862

Compare numerals in batch

Posting part of the code which am looking for a workaround

if 09 LSS 9 (ECHO YES) ELSE (ECHO NO)

This command always echo's 'Yes' as it considers 09 to be less than 9. Any alternative for this command?

EDIT:

Thanks but the Modulo part is not working in the command i am trying to insert in.

Have a file test.txt which contains "1234 09" below is my command

set actualdate=9
for /f "usebackq Tokens=1,2,3" %%d in (test.txt) do (SET /a x=1000%%e %% 1000 & if %x% LSS %ActualDate% ECHO %%d >> test2.txt)

Upvotes: 2

Views: 542

Answers (2)

jeb
jeb

Reputation: 82257

The problem with your code snippet is the syntax and also the percent expansion.
You can use & for multiple commands in one line (not the pipe |) or split them into multiple lines.
You can't access the variable x with percent expansion inside of a block, but delayed expansion works there

setlocal EnableDelayedExpansion
set actualdate=9
for /f "usebackq Tokens=1,2,3" %%d in (test.txt) do (
   SET /a x=1000%%e %% 1000
   if !x! LSS %ActualDate% ECHO %%d >> test2.txt
)

Upvotes: 3

If you can put your numbers into variables, you can strip off the leading zero using modulo.

Try this sample:

@ECHO OFF

SET a=09
SET b=9

SET /a x=1000%a% %% 1000
ECHO %x%
SET /a y=1000%b% %% 1000
ECHO %y%

if %x% LSS %y% (ECHO YES) ELSE (ECHO NO)

PAUSE

If you try to do SET /a a=09, you'll get the following error:

Invalid number. Numeric constants are either decimal (17), hexadecimal (0x11) or octal (021).

Upvotes: 2

Related Questions