如果变量开头,Windows批处理检查结尾,并包含一个特定的字符串 [英] Windows Batch check if variable starts with, ends with and contains a specific string
问题描述
我想检查是否在批处理文件中的一个变量启动
包含试用版
的地方,并结束与)
。
这可能吗?
如果是,可能有人帮助我吗?
@ECHO OFF
SETLOCAL
SET VAR = ABC&放大器; CALL:检查
SET VAR =ABC和放大器; CALL:llcheck
SET VAR =)及CALL:检查
SET VAR =)&放大器; CALL:llcheck
SET VAR = ABC)与& CALL:llcheck
SET VAR = xyzbetazyx&放大器; CALL:检查
SET VAR =xyzbetazyx&放大器; CALL:llcheck
SET VAR = xyzbetazyx)和安培; CALL:llcheck
SET VAR = xyzbetazyx)和安培; CALL:检查
SET VAR =xyzbetazyx)及CALL:检查
GOTO:EOF
:: LOP最后,再检查
:llcheck
SET VAR =%VAR:〜0,-1%
:检查
SET结果= N
SET VAR2 =%VAR%
SET varvar =%VAR%> test1.txt的ECHO%VAR:0.1〜VAR %%:〜-2%
>的test2.txt ECHO)
FC名为test1.txt的test2.txt> NUL
IF ERRORLEVEL 1 GOTO完成
SET VAR | FINDSTR / BVAR =| FINDSTR /我测试版GT&; NUL
IF ERRORLEVEL 1 GOTO完成
SET结果= Y
:完成
ECHO%VAR%开工有BETA和目的):%结果%
GOTO:EOF
哪里有遗嘱......
不平衡可能会非常棘手我只是将它设置平衡,砍掉最后一个字符(
设置变量:llcheck
项:LOP最后,检查
Essentiall,我复制到变量 VAR2
和 varvar
在奥德试玩会发生什么情况,这些应该variablenames进行设置。
这两个文件的生成,然后。 test1.txt的
包含 VAR
和的test2.txt 的第一个和最后2个字符code>简单
)
比较两个 - 如果他们不相同,那么该变量不会启动和结束
)
另外还有 - 可以简单地写成 VAR
到一个文件,并使用 FINDSTR
找到测试版
,但我还是决定送 SET VAR
的输出应该是所有$ C $的内容C>在表单中的
VAR *
变量
VAR = ABC
VAR2 = ABC
varvar = ABC
进入FINDSTR,发现启动一( / B
) VAR =
和发现是否的 THAT 的包含字符串测试版
的 / I
选择不区分大小写。如果你想具体试用版
在大写,只需更改测试版
到试用版
和删除 / I
。
结果
设置为是
或 N
I'm trying to check if a variable in a batch file starts with "
contains BETA
somewhere and ends with ")
.
Is it possible? And if yes, may somebody help me with that?
@ECHO OFF
SETLOCAL
SET var=abc&CALL :check
SET var="abc"&CALL :llcheck
SET var="")&CALL :check
SET var=")"&CALL :llcheck
SET var=abc")"&CALL :llcheck
SET var=xyzbetazyx&CALL :check
SET var="xyzbetazyx"&CALL :llcheck
SET var=xyzbetazyx")"&CALL :llcheck
SET var=xyzbetazyx")"&CALL :check
SET var="xyzbetazyx")&CALL :check
GOTO :eof
:: Lop last, then check
:llcheck
SET var=%var:~0,-1%
:check
SET result=N
SET var2=%var%
SET varvar=%var%
>test1.txt ECHO %var:~0,1%%var:~-2%
>test2.txt ECHO "")
FC test1.txt test2.txt >nul
IF ERRORLEVEL 1 GOTO done
SET var|FINDSTR /b "var="|FINDSTR /i "beta" >nul
IF ERRORLEVEL 1 GOTO done
SET result=Y
:done
ECHO %var% starts " has BETA and ends ") : %result%
GOTO :eof
Where there's a will...
Setting variables with unbalanced "
can be tricky. I just set it up balanced and lopped off the last character (:llcheck
entry : lop last and check.
Essentiall, I've copied the variable into var2
and varvar
in oder to demo what happens should these variablenames be set.
Two files are then generated. TEST1.TXT
contains the first and last 2 character of var
and TEST2.TXT
simply "")
Compare the two - if they're not identical, then the variable does NOT start "
and end ")
Otherwise well - could simply have written var
out to a file and used findstr
to find beta
, but I decided to send the output of SET var
which should be the contents of ALL
of the var*
variables in the form
var=abc
var2=abc
varvar=abc
into findstr, finding the one that starts (/b
) var=
and finding whether THAT contains the string beta
The /i
selects case-insensitive. If you want specifically BETA
in UPPER-CASE, simply change beta
to BETA
and remove the /i
.
result
is set to Y
or N
这篇关于如果变量开头,Windows批处理检查结尾,并包含一个特定的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!