2

Here is my script:

   @echo off
    echo.|if defined 1 geq 1 echo 1 is geq than 1
    echo.|if defined 2 gtr 100 echo 2 is gtr than 100

    echo.|if 1 gtr gtr 100 echo 1 is gteater than 100
    echo.|if 100 lss gtr 100 echo 100 is gteater and the same time less than 100

the output is :

1 is geq than 1
2 is gtr than 100
1 is gteater than 100
100 is gteater and the same time less than 100

What is going with IF command?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
npocmaka
  • 55,367
  • 18
  • 148
  • 187

2 Answers2

3

The batch parser concatenates the first tokens after the IF.

To gain an insight you can use the cmdcmdline variable.

From a batch file

@echo off
echo pipe | if defined 1 geq 1 echo %%cmdcmdline%%

In the output you can see that defined and 1 are concatenated, so this is the cause for the strange results

C:\Windows\system32\cmd.exe /S /D /c" if defined1 geq 1 echo %cmdcmdline%"

When you test this from the command line you need to modify the %%cmdcmdline%% to %^cmdcmdline% as the cmd-parser works a bit different with percent expansions.

jeb
  • 78,592
  • 17
  • 171
  • 225
2

change this:

if defined 1 geq 1 echo 1 is geq than 1

to this:

if defined 1 if 1 geq 1 echo 1 is geq than 1
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • 2
    In most of the cases I know how to deal with if command :-) . The question is a little bit misleading and I've asked it more for the fun than anything else.The main point is why here `IF` is behaving like that .I think it's a bug in `IF` - http://www.dostips.com/forum/viewtopic.php?f=3&t=5539&p=33837#p33837 - But will be good if can confirm this. – npocmaka Apr 20 '14 at 17:28
  • 3
    I know you are keen on experimenting with syntax, to figure out how it works and new ways to do things. I think it's a little off topic for SO though. Dostips is a better place, but could use some more eyes and testers. – foxidrive Apr 20 '14 at 17:35