Batch code to compare numbers

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
bhavya
Posts: 3
Joined: 21 Jun 2013 08:35

Batch code to compare numbers

#1 Post by bhavya » 21 Jun 2013 08:42

I need to compare two numbers in batch script. could any one provide the code? :(
example:

set i=1

if %i%=1 (
echo one
) else (
echo not one
)

i am using the above code in the for loop where "i" value will be incremented.

Thanks

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Batch code to compare numbers

#2 Post by Squashman » 21 Jun 2013 09:03

Please show us all your code. Are you using a FOR /L loop or are you incrementing a variable with a SET command in a normal FOR loop. If you are incrementing with a SET then you need to use delayed expansion.

bhavya
Posts: 3
Joined: 21 Jun 2013 08:35

Re: Batch code to compare numbers

#3 Post by bhavya » 21 Jun 2013 09:27

Thanks Squash,

Below is the part of code:

set i=1

for /l %%G in (1,1,5) do (
if %i%==1 (
echo one
) else (
echo not one
)
set /a i=%i%+1
)

i can use %%G in place of "i" value which gives me incremented value. But i need to do some other operation in the middle of the loop so here i am using 'i" value.

Also the above code loop from 1 to 5, but i value remain the same (i.e., 1) through out.


Thanks

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Batch code to compare numbers

#4 Post by Squashman » 21 Jun 2013 09:55

The Loop variable %%G is always going to be equal to the variable %I% with delayed expansion turned on. I am not understanding why you need to increment a variable inside a FOR LOOP that increments when the two variables are always going to be equal. That makes no logical sense to me. But since you are not showing us your actual code nobody will ever understand.

Please use CODE tags when posting code.

Code: Select all

setlocal enabledelayedexpansion
set i=1

for /l %%G in (1,1,5) do (
if !i!==1 (
echo one
) else (
echo not one
)
set /a i+=1
)

bhavya
Posts: 3
Joined: 21 Jun 2013 08:35

Re: Batch code to compare numbers

#5 Post by bhavya » 21 Jun 2013 10:23

Hi Squash

please check the below code for better understanding of the problem.

set i=1
set temp=5 ( taking count of the file in the folder)


for /l %%G in (1,1,5) do (
if !i!==1 (
echo one
GOTO FIRST
) else (
echo not one
GOTO LAST
)


:FIRST
Doing operation specifically to first file
:LAST
Doing operation for all the file

If %i%=%temp%-2 execute another batch script (i.e.,execute the current code and another batch script parallely)

set /a i+=1
)

i will check the update tomorrow. Thanks for the update

Thanks

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch code to compare numbers

#6 Post by foxidrive » 21 Jun 2013 11:23

@OP: if you don't show your actual code - you are not guaranteed to get the code you need because the task is not clear.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Batch code to compare numbers

#7 Post by Squashman » 21 Jun 2013 11:26

Looking at that code I am just baffled as to why you would do your logic that way. I am not even sure if that works. I know we had a long discussion thread about how FOR /L works. Gotta go look that up.

Post Reply