简体   繁体   English

此批处理中不期望goto

[英]goto was not expected at this time batch

@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

i have narrowed it down to this line if %a%==%b% goto greetings when i remove it it runs 如果我删除它时%a%==%b%进入问候语,我将其范围缩小到了这一行
i have looked but i have no idea why it does not work please help the greetings.txt has "hi hello grunt" i think it might be the variables 我看过了,但我不知道为什么它不起作用, 请帮助 greetings.txt发出“嗨你好咕“声”,我认为这可能是变量

If %a% or %b% are empty values, it is likely the compare is incomplete, and it is saying that the goto is not expected yet. 如果%a%%b%为空值,则可能是比较未完成,并且表示尚未实现goto For instance, if you type the following at a C:\\ prompt: 例如,如果在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 you enclose each value in quotes, then the comparison will still work even if one or both of the values are empty. 如果将每个值都用引号引起来,则即使其中一个或两个值都为空,比较仍然可以进行。 For instance: 例如:

if "%a%"=="%b%" goto greetings

The normal reason for that an unexpected word in an IF statement is that IF has a very specific syntax, IF item1 operator item2 actionstatement(s) . IF语句中出现意外单词的正常原因是IF具有非常特定的语法,即IF item1 operator item2 actionstatement(s)

What is likely to be happening is that item1 AND item2 appear to be missing, so IF resolves that as IF == goto greetings . 可能发生的情况是item1 item2似乎丢失了,因此IF将其解析为IF == goto greetings Since goto is not one of its known operators ( == , equ , neq , leq , lss , geq , gtr`) then it complains. 由于goto不是其已知的operators==equneqleqlssgeq ,gtr`),因此会抱怨。

The question from here is - why do %a% and %b% appear to be empty? 这里的问题是-为什么%a%%b%似乎为空?

Within a block statement (a parenthesised series of statements) , the entire block is parsed and then executed. 在块语句(a parenthesised series of statements) ,将分析整个然后执行。 Any %var% within the block will be replaced by that variable's value at the time the block is parsed - before the block is executed. 解析该块时-执行该块之前,该块中的任何%var%都将由该变量的值替换。

Hence, IF (something) else (somethingelse) will be executed using the values of %variables% at the time the IF is encountered. 因此,将在遇到IF时使用%variables%的值执行IF (something) else (somethingelse) In your case, that means the outermost IF - in if defined string . 在您的情况下,这表示最外的IF if defined string

Two common ways to overcome this are 1) to use setlocal enabledelayedexpansion and use !var! 解决此问题的两种常用方法是:1)使用setlocal enabledelayedexpansion和使用!var! in place of %var% to access the changed value of var or 2) to call a subroutine to perform further processing using the changed values. 代替%var%访问的改变值var或2),以调用一个子程序使用改变的值来执行进一步的处理。

Next problem is using a label within a block. 下一个问题是在块中使用标签。 Not a good idea. 这不是一个好主意。 On some versions, a label will terminate the block. 在某些版本中,标签会终止该块。 Call a subroutine instead. 而是调用子例程。

 call :cloop

...
goto start
:cloop
 (whatever needs to be done)
goto :eof

(note that :cloop and :EOF have a required colon. on cloop it means "this is an internal subroutine - it's in the cuurrent batchfile." :EOF is a predefined label understood by CMD to mean end of file .) (请注意:cloop:EOF具有必需的冒号。在cloop上表示“这是一个内部子例程-位于当前批处理文件中。” :EOFCMD理解end of file预定义标签。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM