如何检查参数(或变量)是否为批处理脚本中的数字 [英] How to check if a parameter (or variable) is a number in a Batch script

查看:206
本文介绍了如何检查参数(或变量)是否为批处理脚本中的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查传递给Windows批处理文件的参数是否为数字值.检查也适用于变量会很好.

I need to check if a parameter that is passed to a Windows Batch file is a numeric value or not. It would be good for the check to also works for variables.

我找到了 answer 与类似的问题,其中findstr命令与正则表达式一起使用.

I found an answer to a similar question, where the findstr command is used with a regular expression.

我确实尝试过该解决方案,但是它并没有达到我的期望(至少在Windows 7上如此).

I did try that solution, but it’s not working like I hoped (at least on Windows 7).

我的测试方案如下:

AA  # not a valid number
A1  # not a valid number
1A  # not a valid number

11  # a valid number

推荐答案

SET "var="&for /f "delims=0123456789" %%i in ("%1") do set var=%%i
if defined var (echo %1 NOT numeric) else (echo %1 numeric)

%1替换为%yourvarname%

这篇关于如何检查参数(或变量)是否为批处理脚本中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆