2批次字符串问题

Geo*_*Geo 8 string batch-file

1)是否有任何内置可以告诉我变量的内容是否只包含大写字母?

2)有什么方法可以查看变量是否包含字符串?例如,我想看看变量%PATH%是否包含Ruby.

pax*_*blo 15

对于第1部分,findstr答案是肯定的.您可以使用正则表达式功能以及errorlevel:

> set xxokay=ABC
> set xxbad=AB1C
> echo %xxokay%|findstr /r "^[A-Z]*$" >nul:
> echo %errorlevel%
0
> echo %xxbad%|findstr /r "^[A-Z]*$" >nul:
> echo %errorlevel%
1
Run Code Online (Sandbox Code Playgroud)

在这种情况下,在管道字符之间没有空格是很重要的,因为这将导致输出的空间不是您可接受的字符之一.echo %xxokay%|


对于第2部分,findstr也是答案(/i忽略可能是您想要的情况 - 如果案例必须匹配则将其关闭):

> set xxruby=somewhere;c:\ruby;somewhere_else
> set xxnoruby=somewhere;somewhere_else
> echo %xxruby%|findstr /i ruby >nul:
> echo %errorlevel%
0
> echo %xxnoruby%|findstr /i ruby >nul:
> echo %errorlevel%
1
Run Code Online (Sandbox Code Playgroud)

然后你可以使用:

if %errorlevel%==1 goto :label
Run Code Online (Sandbox Code Playgroud)

在这两种情况下更改脚本的行为.

例如,ruby检查的代码段可能类似于:

:ruby_check
    echo %yourvar%|findstr /i ruby >nul:
    if %errorlevel%==1 goto :ruby_check_not_found
:ruby_check_found
    :: ruby was found
    goto :ruby_check_end
:ruby_check_not_found:
    :: ruby was NOT found
:ruby_check_end
Run Code Online (Sandbox Code Playgroud)