-1

I'm new to batch script and have trouble with a very simple code.

I'm trying to print the changes of variables in the nested loop as below but it doesn't work. (e.g. n_total : 5->10->15->...)

Also, ratio is the result of adding two numbers in a string form. (e.g. "5" + "5" = "55")

@echo off
setlocal enabledelayedexpansion

set /A n_total=5
set /A step=100

for /L %%A in (step,100,1000) do call(
    for /L %%B in (n_total,5,30) do call(
        set n_c=%%B/2
        set n_d=%%B - !n_c!
        set /A ratio=!n_d! + !n_c!
        echo %%B
        echo %n_c%
        echo %ratio%

Only

Some kind of error?
%B
%B/2

shows up in the executed result. Please let me know what's wrong with my code. Thank you!

  • 2
    You don't have nested loops. What's `call(` supposed to do? – Stephan Jun 21 '22 at 07:04
  • 1
    To be clear: your first `for` does ten times `call(` (which basically sets errorlevel to zero). The loop ends then. The second `for` does the same thing six times. The loop ends then. The following `set` command gives an error, because `%%B` doesn't exist anymore (because the defining loop has ended). Etc. – Stephan Jun 21 '22 at 07:37
  • 1
    Replace both `call(` by just `(`. Remove `/A` on `set /A n_total=5` and on `set /A step=100` whereby it would be even better to use `set "n_total=5"` and `set "step=100"`. For the reason of this change see my answer on [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/a/26388460/3074564) `(step,100,1000)` must be changed to `(%step%,100,1000)` to reference the value of environment variable `step` as defined above. `(n_total,5,30)` must be changed to `(%n_total%,5,30)` for the same reason. – Mofi Jun 21 '22 at 07:39
  • `set n_c=%%B/2` misses the option `/A` (arithmetic expression) to really evaluate the arithmetic expression, i.e. there must be used `set /A n_c=%%B/2`. `set n_d=%%B - !n_c!` must be changed to `set /A n_d=%%B - n_c` to evaluate this arithmetic expression. I strongly recommend to open a [command prompt](https://www.howtogeek.com/235101/), run `for /?` and read the entire output usage help and run `set /?` and read again the entire output usage help explaining when and how to use [delayed expansion](https://ss64.com/nt/delayedexpansion.html) and how to use an arithmetic expression. – Mofi Jun 21 '22 at 07:42
  • `echo %n_c%` must be coded `echo !n_c!` or `set n_c` to get output this environment variable with name and current value. `echo %ratio%` must be coded `echo !ratio!` or `set ratio` for the same reason. In other words, the only correct command line is `@echo off` which is the only command line which should not be used on developing a batch file and [debugging](https://stackoverflow.com/a/42448601/3074564) it on not working as expected. The second line should be `setlocal EnableExtensions EnableDelayedExpansion` to completely define the __required__ execution environment. – Mofi Jun 21 '22 at 07:45

1 Answers1

2

Problem 1 : call( is nonsense - to call a routine, you need either call :internallabel or call otherbatchfile. To nest the fors, change call( to ( and add the closing ) for each - so two )s at the end.

Problem 2 : the syntax (step,100,1000) is incorrect. You need (%step%,100,1000) - use the contents of step.

Problem 2a: The syntax of for /L is for /L (startvalue,step,endvalue)... Your variable name appears to assume otherwise.

Problem 3 : The set var=value syntax is for a string assignment, and is preferably set "var=value" to avoid assignment of unwanted trailing spaces. To perform a calculation, you need set /A and quotes are not required.

Problem 4 : The ever-faithful delayed expansion trap

Having read & digested the information in (4), there's a few little things that can be noted:

In summary, %var% will be interpreted as the value of var at the time the entire compound statement (starting in this case at for...%%A...) was encountered. !var! is the value as it has been changed during the loop (the instantaneous or current value).

set /a always uses the current value of variables, so using !var! within a set /a formula is superfluous - var will suffice. %var% however will substitute the starting value of var.

Mofi
  • 46,139
  • 17
  • 80
  • 143
Magoo
  • 77,302
  • 8
  • 62
  • 84