2 Replies - 567 Views - Last Post: 09 December 2012 - 10:06 PM Rate Topic: -----

#1 Parrish  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 13-March 10

How to manipulate variables in batch loop

Posted 09 December 2012 - 08:36 PM

Hey everyone,
I am having some issues on how to manipulate variables in a for loop that I am sending to a jar file... any suggestions would be appreciated. The code runs, but the math doesnt..

@echo off

echo Welcome, this will run Lab1.jar
:: Set the amount of times to run from user input
set /P numToRun= Please enter how many times to run the program: 

set /a numParam = 1000
set /a strParam = 1000

:: Start looping here while increasing the jar pars
:: Loop from 0 to numToRun
for /L %%i in (1 1 %numToRun%) do (
	set /a numParam = %numParam% * 10 
	set /a strParam = %strParam% * 10 
	java -jar Lab1.jar %numParam% %strParam%
	
	:: The two lines below are used for testing
	echo %numParam%  
	echo %strParam%
)

@echo on



whenever I output those two parameters, they just output the original value of 1000

Is This A Good Question/Topic? 0
  • +

Replies To: How to manipulate variables in batch loop

#2 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 856
  • View blog
  • Posts: 2,246
  • Joined: 28-March 11

Re: How to manipulate variables in batch loop

Posted 09 December 2012 - 09:48 PM

It has to do with how the variables are expanded... Try this as an example:
@echo off

echo Welcome, this will run Lab1.jar
:: Set the amount of times to run from user input
set /P numToRun= Please enter how many times to run the program: 

set /a numParam = 1000
set /a strParam = 1000 

setlocal enabledelayedexpansion enableextensions

:: Start looping here while increasing the jar pars
:: Loop from 0 to numToRun
for /L %%i in (1 1 %numToRun%) do (


	set /a numParam = !numParam! * 10
	set /a strParam = !strParam! * 10 

	REM java -jar Lab1.jar %numParam% %strParam%
	
	:: The two lines below are used for testing
	echo numParam !numParam!	
	echo strParam !strParam!
)

@echo on


Notice I enclose the variable in ! and not % in the loop...
Was This Post Helpful? 1
  • +
  • -

#3 Parrish  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 13-March 10

Re: How to manipulate variables in batch loop

Posted 09 December 2012 - 10:06 PM

when i run that, i get
Missing operator.
Missing operator.
1000 !numParam!
1000 !strParam!

edit: I forgot the setlocal enabledelayedexpansion enableextensions

Thanks that is what I wanted!

What does that do exactly?

This post has been edited by Parrish: 09 December 2012 - 10:37 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1