Arithmetic inside a batch file for loop

I have a for loop in a batch file that looks like this:

for %%y in (100 200 300 400 500) do (
    set /a x = y/25     
    echo %x%
)

Line:

set /a x = y/25

It does not seem to make any separation. What is the correct syntax for dividing each y by 25? I need only the integer result from this division.

+5
source share
2 answers

Environment variables should not be expanded for use in the SET / A statement. But FOR variables must be extended.

, , ECHO , , FOR . , % x% , . , , .

, . .

@echo off
setlocal enableDelayedExpansion
for %%A in (100 200 300 400 500) do (
  set n=%%A

  REM a FOR variable must be expanded
  set /a x=%%A/25

  REM an environment variable need not be expanded
  set /a y=n/25

  REM variables that were set within a block must be expanded using delayed expansion
  echo x=!x!, y=!y!

  REM another technique is to use CALL with doubled percents, but it is slower and less reliable
  call echo x=%%x%%, y=%%y%%
)
+10

, "y" - . .

set /a x = %%y/25
+1

All Articles