在 Windows 批处理脚本中添加存储在变量中的数字

rao*_*son 3 scripting windows batch

我在批处理脚本中有一个循环,我想用循环计数器做一些算术运算。我发现了如何评估表达式,如何在此处添加数字:评估 Windows 批处理脚本中的表达式

我如何做同样的事情,但有变量?

例如:

set x = 100
for /L %%i in (1,1,5) do (
    set Result=0
    set /a Result = %%i+%%x
    echo %Result%
)
Run Code Online (Sandbox Code Playgroud)

作为我期望的输出

101 102 103 104 105

谢谢!

Izz*_*zzy 5

你真的应该远离批处理文件。

@echo off

setlocal enabledelayedexpansion

set x=100
set result=0

for /L %%i in (1,1,5) do (

  set /A result=!x! + %%i

  echo !result!
)

endlocal
Run Code Online (Sandbox Code Playgroud)

  • 是的。Powershell == 好。 (3认同)