首页 > 解决方案 > IF "71" GTR "7000" 为真

问题描述

根据我的代码,如果%~1大于7000,请转到ExceedError

IF "%~1" GTR "7000" GOTO ExceedError

内容ExceedError

ECHO Value exceeded the maximum value. See help file.
EXIT /B

但这发生了:

...modules>If "71" GTR "7000" GOTO ExceedError

...modules>Echo Value exceeded the maximum value. See help file.
Value exceeded the maximum value. See help file.

...modules>exit /B

发生了什么?有什么不对?

标签: batch-fileif-statement

解决方案


你已经用双引号括住了参数,这会强制进行字符串比较。要比较数字,请尝试不使用引号:

IF %~1 GTR 7000 GOTO ExceedError

如果您想防止错误,您可以再添加一行:

set /a "_number=%~1" >nul 2>&1 || set "_number=0"
IF %_number% GTR 7000 GOTO ExceedError

如果输入错误,您会将值与0默认值进行比较。如果需要,您可以更改它


推荐阅读