Welcome to Dream.In.Code
Become an Expert!

Join 137,429 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,902 people online right now. Registration is fast and FREE... Join Now!




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

 
Reply to this topicStart new topic

Using "set /a" statements in Windows batch file..., Having a lot of trouble trying to numerically modify variables

enpey
13 Jan, 2008 - 08:34 PM
Post #1

D.I.C Head
**

Joined: 2 May, 2007
Posts: 59



Thanked: 1 times
My Contributions
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

CODE

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 Jan, 2008 - 08:41 PM
User is offlineProfile CardPM
+Quote Post

Nayana
RE: Using "set /a" Statements In Windows Batch File...
14 Jan, 2008 - 06:06 PM
Post #2

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
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:

CODE

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

CODE

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.

CODE

@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):

CODE

@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.
User is offlineProfile CardPM
+Quote Post

enpey
RE: Using "set /a" Statements In Windows Batch File...
15 Jan, 2008 - 07:51 PM
Post #3

D.I.C Head
**

Joined: 2 May, 2007
Posts: 59



Thanked: 1 times
My Contributions
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
User is offlineProfile CardPM
+Quote Post

Nayana
RE: Using "set /a" Statements In Windows Batch File...
15 Jan, 2008 - 11:32 PM
Post #4

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
Actually I meant to write the last example like this:

CODE

@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
CODE

@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: 15 Jan, 2008 - 11:34 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/5/08 04:54AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month