@echo off
:start
set string=
set lo=1
set a=0
set b=0
set cl=1
set cloop=
set google=0
set k=0
set r=0
set id=
set t=0
set f=0
set /p string=?
if defined string (
echo %string%
goto loop
) else (
echo please enter a string
goto start
)
:loop
set a=
for /f "tokens=%lo%" %%G IN ("%string%") DO echo %%G
if defined a (
echo %a%
set google=0
set /p cloop=<greetings.txt
pause
:cloop
set b=
for /f "tokens=%cl%" %%g IN ("%cloop%") DO set b=%%g
if defined string (
if %a%==%b% goto greetings
set /a cl=%cl%+1
goto cloop
) else (
set cl=0
set /a lo=%lo%+1
goto loop
)
) else (
goto google
)
:greetings
set f=0
set k=0
set r=0
set /p id=<greetingtone.dat
for /f "tokens=%cl%" %%g IN ("%id%") DO set t=%%g
start greeting.bat
call greeting.bat
goto talk
:google
echo not done yet
pause
goto start
我把它缩小到这条线
如果%a%==%b%转到问候语
当我删除它运行
我看了,但我不知道为什么它不起作用
请帮助 greetings.txt有“你好你好咕噜”
我认为这可能是变量
答案 0 :(得分:2)
如果%a%
或%b%
为空值,则比较可能不完整,并且表示尚未预料到goto
。例如,如果您在C:\
提示符下键入以下内容:
c:\>if a== echo ok
c:\>if ==a echo ok
echo was unexpected at this time.
c:\>if == echo ok
ok was unexpected at this time.
c:\>
如果将每个值括在引号中,则即使其中一个或两个值为空,比较仍然有效。例如:
if "%a%"=="%b%" goto greetings
答案 1 :(得分:1)
IF
语句中出现意外字词的正常原因是IF
具有非常具体的语法IF item1 operator item2 actionstatement(s)
。
可能发生的事情是item1 AND item2似乎缺失,因此IF
将其解析为IF == goto greetings
。由于goto
不是其已知的operators
==
,equ
,neq
,leq
,lss
,{{ 1}},gtr`)然后抱怨。
此处的问题是 - 为什么geq
和%a%
似乎是空的?
在块语句%b%
中,解析整个块并执行然后。在执行块之前,块中的任何(a parenthesised series of statements)
将被解析块时的变量值替换。
因此,%var%
将在遇到IF (something) else (somethingelse)
时使用%variables%
的值执行。在您的情况下,这意味着最外面的IF
- 位于IF
。
解决此问题的两种常见方法是1)使用if defined string
并使用setlocal enabledelayedexpansion
代替!var!
来访问已更改的%var%
或2}值以进行调用一个子程序,用于使用更改的值执行进一步处理。
下一个问题是在块中使用标签。不是个好主意。在某些版本中,标签将终止块。改为调用子程序。
var
(请注意, call :cloop
...
goto start
:cloop
(whatever needs to be done)
goto :eof
和:cloop
在<{1}}上有必需冒号。这意味着“这是一个内部子程序 - 它位于当前批处理文件中。 “:EOF
是由cloop
理解为:EOF
的预定义标签。)