简体   繁体   English

Windows构建和操作“Goto此时出乎意料”

[英]Windows build and action “Goto was unexpected at this time”

Hoping for some assistance explaining and verifing this script I am getting an error "goto was unexpected at this time" 希望得到一些帮助解释和验证这个脚本我得到一个错误“goto此时出乎意料”

I attempting to created a script that gets the windows version number... depending on the version number if its higher or equals then x it has to goto action 1 (FTRDWIN8) and when the version number is less then x it has to goto action 2 我试图创建一个获取Windows版本号的脚本...取决于版本号,如果它更高或等于然后x它必须转到动作1(FTRDWIN8),当版本号小于x时它必须转到动作2

@echo off

::parsing the output of "ver" to getting windows version number 
::excuting. 

set strbuild=6.2

for /f "tokens=4-5 delims=. " %%i in ('ver') do set VERSION=%%i.%%j 
if "%version%" geq "%strbuild%" goto FTRDWIN8
if "%version%" lss "%strbuild%" goto FTRDWINXPVI7


:FTRDWIN8
"C:\Windows\write.exe"
exit

:FTRDWINXPVI7
"C:\Windows\notepad.exe"
exit

If only compares strings or integers; If只比较字符串或整数; any non numeric char forces string comparison on a char by char base and then 10.0 is less than 8.1 任何非数字字符串强制字符串比较char by char base然后10.0小于8.1

This slightly changed batch 这个稍微改变了一批

@echo on
set strbuild=6.2

for /f "tokens=4-5 delims=. " %%i in ('ver') do set VERSION=%%i.%%j 
if "%version%" geq "%strbuild%" goto FTRDWIN8
if "%version%" lss "%strbuild%" goto FTRDWINXPVI7

Goto :eof
:FTRDWIN8
"C:\Windows\write.exe"
Exit

:FTRDWINXPVI7
"C:\Windows\notepad.exe"
Exit

has this transcript with echo on: 有这个成绩单与回声:

set strbuild=6.2
for /F "tokens=4-5 delims=. " %i in ('ver') do set VERSION=%i.%j
set VERSION=10.0
if "10.0 " GEQ "6.2" goto FTRDWIN8
if "10.0 " LSS "6.2" goto FTRDWINXPVI7
"C:\Windows\notepad.exe"
Goto :Eof

I copied your code and executed, and I cannot reproduce your error, though it gives the wrong result. 我复制了你的代码并执行了,我无法重现你的错误,尽管它给出了错误的结果。 I have windows 10 (version 10.0.14393), and your IF statement does a string comparison, so "10.0" is less than "6.2" 我有Windows 10(版本10.0.14393),你的IF语句进行字符串比较,所以“10.0”小于“6.2”

I cannot see how your code could ever give an error message of "goto was unexpected at this time" . 我看不出你的代码如何给出“goto此时意外”的错误信息。 If you modify your code to enable delayed expansion and use !var! 如果您修改代码以启用延迟扩展并使用!var! instead of %var% , then your code could not possibly give that error. 而不是%var% ,那么你的代码不可能给出错误。

You should remove the ECHO OFF so you can see where the error message is coming from. 您应该删除ECHO OFF以便查看错误消息的来源。 I suspect it is in some other unrelated code, but can't be sure. 我怀疑它是在其他一些不相关的代码中,但不能确定。

As I stated earlier, (and also pointed out by LotPings) your logic is wrong because your IF statement with quotes and decimal point does a string comparison, not a numeric comparison. 正如我之前所说,(并且也由LotPings指出)你的逻辑是错误的,因为带引号和小数点的IF语句进行字符串比较,而不是数字比较。

Here is corrected logic that does the comparison in multiple steps - each version node must be tested independently. 这是纠正的逻辑,它在多个步骤中进行比较 - 每个版本节点必须独立测试。 I don't see any reason to set any environment variable. 我没有看到任何设置任何环境变量的理由。 Also, I like to use EXIT /B instead of EXIT . 另外,我喜欢使用EXIT /B而不是EXIT I suspect that the code may be locale dependent - I'm not sure if the VER command changes its output format depending on the locale. 我怀疑代码可能依赖于语言环境 - 我不确定VER命令是否根据语言环境更改其输出格式。

@echo off
for /f "tokens=4-5 delims=. " %%A in ('ver') do (
  if %%A gtr 6 goto FTRDWIN8
  if %%A equ 6 if %%B geq 2 goto FTRDWIN8
)

:FTRDWINXPVI7
"C:\Windows\notepad.exe"
exit /b

:FTRDWIN8
"C:\Windows\write.exe"
exit /b

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

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