Using "set /a" statements in Windows batch file...

Having a lot of trouble trying to numerically modify variables

Page 1 of 1

3 Replies - 94593 Views - Last Post: 16 January 2008 - 12:32 AM Rate Topic: -----

#1 enpey  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 75
  • Joined: 02-May 07

Using "set /a" statements in Windows batch file...

Posted 13 January 2008 - 09:34 PM

Hello all,

I am trying to write a short batch file that will change a few variables each time it loops. I am just having a lot of trouble with the "set /a" assignment statements. If someone could please outline the correct syntax for the set statement and/or suggest how I could correct my code it would be greatly appreciated...

Thanks, Nathan

rem echo off
set mFourMIN=0.0
set mFourMAX=3.0
rem set umMIN=0.0
rem set umMAX=1.0
rem set phiMIN=0.0
rem set phiMAX=10

set mFour=0.0

FOR /L %%G IN (%mFourMIN%,1,%mFourMAX%) DO (
	echo mFour = %mFour%
		
	
	rem SET um=1
	rem SET phi=10

	SET /a tope = %mFour% - 1
	SET /a tope = %tope% * 9
	SET /a bote = %mFour% + 1
	SET /a bote = %bote% * 10
	SET /a e = tope / bote
	echo e = %e%

	echo e = %e%
	rem set outFile="e%e%_umass%um%_phi%phi%.d"
	rem echo outFile = %outFile%
	rem ..\..\gawk\gawk "BEGIN { ORS = \"\n\";  } {if (NR == 7) { print \"\tmFourVal=%mFour%\" >> \"tmpsetupFile\"; next } if (NR == 8) { print \"\tumassVal=%um%\" >> \"tmpsetupFile\"; next } if (NR == 9) { print \"\tphiVal=%phi%\" >> \"tmpsetupFile\"; next } else { print $0 >> \"tmpsetupFile\" } }" setupFile
	 
	rem ..\..\gawk\gawk -f tmpsetupFile source.d
	SET /a mFour= %mFour% + 1
	echo.
)
PAUSE


This post has been edited by enpey: 13 January 2008 - 09:41 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Using "set /a" statements in Windows batch file...

#2 Nayana  Icon User is offline

  • DIC Hawk - 나야나 नयन:
  • member icon

Reputation: 31
  • View blog
  • Posts: 824
  • Joined: 14-November 07

Re: Using "set /a" statements in Windows batch file...

Posted 14 January 2008 - 07:06 PM

Ah, the joys of batch file programming...

Seeing as you are using windows, you could probably do the job better with windows scripts (VBScript or JScript)

But anyway, your problem has an explaination and a solution.

Firstly, in batch files (at least on XP/Vista) variables inside IF blocks or FOR blocks do not get updated until after the block has closed. For instance:

set a = 0

set /a a = a + 1
echo a

set /a a = a + 1
echo a



Will first echo "1", then a "2"

However

set /a a = 1

if 1 == 1 (
  set /a a = a + 1
  set /a a = a + 1
  
  echo %a%
)
echo %a%



will first echo "1", as all variables in the block were expanded before the code was executed
The second statement will echo a "2". As both the set statements were working on an 'a' that equalled 1.

This is rather strange behaviour, but there is a way around it: delayed environment variable expansion.

To enable this, start cmd.exe with the "/v" switch. i.e. like this: "cmd /v"

so to run the file it could be: cmd /v "c:\users\me\test.bat"

This will not work, straight away, the code needs to be updated to utilise delayed env. var. expansion.

@echo off
set /a a = 1

if 1 == 1 (
  set /a a = !a! + 1
  set /a a = !a! + 1
  
  echo !a!
)
echo !a!


Will echo 3 twice, as would be expected.

So to echo the first 5 factorials (with cmd started with /v switch):

@echo off
set /a n = 1

for /L %%i in (1,1,5) do (
  set /a n = n * %%i
  echo !n!
)



Hope that explained and helped.
Was This Post Helpful? 1

#3 enpey  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 75
  • Joined: 02-May 07

Re: Using "set /a" statements in Windows batch file...

Posted 15 January 2008 - 08:51 PM

Thanks Nayana.
One question though, do you only need to surround the variable with !'s when you want it's value to be updated and for all subsequent access to that variable its value will have been updated? I assume this because of the way you have written your factorial batch.

Thanks again, Nathan
Was This Post Helpful? 0
  • +
  • -

#7 Nayana  Icon User is offline

  • DIC Hawk - 나야나 नयन:
  • member icon

Reputation: 31
  • View blog
  • Posts: 824
  • Joined: 14-November 07

Re: Using "set /a" statements in Windows batch file...

Posted 16 January 2008 - 12:32 AM

Actually I meant to write the last example like this:

@echo off
set /a n = 1

for /L %%i in (1,1,5) do (
  set /a n = !n! * %%i
  echo !n!
)


but it seemed that
@echo off
set /a n = 1

for /L %%i in (1,1,5) do (
  set /a n = n * %%i
  echo !n!
)


worked anyway. But if I moved the "echo !n!" outside the loop it still worked. So I will now assume that inside the "set" command, expansion is done when the switch is on, regardless of whether or not !! is used.

But you still need to use !! when echoing inside a command block.

This post has been edited by Nayana: 16 January 2008 - 12:34 AM

Was This Post Helpful? 1

Page 1 of 1