批处理文件,命令的语法不正确

时间:2015-11-22 19:53:23

标签: batch-file syntax-error

当我运行我的批处理文件时,我收到错误"命令的语法不正确"。在第一个用户输入请求(userResponse)上输入 y n 或其他任何内容后,就会出现错误。我一个接一个地在代码中评论命令,但我仍然得到错误......

它似乎在哪里?

谢谢!

代码:

@echo off
color A
title Trace Route
SETLOCAL ENABLEDELAYEDEXPANSION

:RestartPoint

set /p userResponse="Would you like a list of local devices to trace (y/n)? "

if !userResponse!==y (

net view

set /p IPAddress="Enter the IP address that you would like to trace: " 
echo.
set /p addressType="Enter the IP Address type (4 or 6): "

if !addressType!==4 tracert -4 !IPAddress!

if !addressType!==6 tracert -6 !IPAddress!

echo.

:openURLRestartPoint1

set /p userResponseOpenURL="Would you like to map the found IP (y/n)? "

if !userResponseOpenURL!==y (

start "" http://www.ip-adress.com/ip_tracer/
)

if !userResponseOpenURL!==n(

echo.
echo Exiting program...
echo.

PAUSE

) else (

echo.
echo Invalid request. Please try again.
echo.

GOTO openURLRestartPoint1

)

) 

if !userResponse!==n (

set /p IPAddress="Enter the IP address that you would like to trace: " 
echo.
set /p addressType="Enter the IP Address type (4 or 6): "

if !addressType!==4 tracert -4 !IPAddress!

if !addressType!==6 tracert -6 !IPAddress!

echo.

:openURLRestartPoint2

set /p userResponseOpenURL="Would you like to map the found IP (y/n)? "

if !userResponseOpenURL!==y (

start "" http://www.ip-adress.com/ip_tracer/
)

if !userResponseOpenURL!==n(

echo.
echo Exiting program...
echo.

PAUSE

) else (

echo.
echo Invalid request. Please try again.
echo.

GOTO openURLRestartPoint2

)

) else (

echo.
echo Invalid request. Please try again.
echo.

GOTO RestartPoint

)

1 个答案:

答案 0 :(得分:2)

你错过了

中的空格
if !userResponseOpenURL!==n(

应该是

if !userResponseOpenURL!==n (

if语句中的标签似乎也有些麻烦,当我想出如何做到这一点时会更新我的答案,但当你删除if语句中的标签并将更改改为时,你会看到上面建议它会起作用。最后,如果你在第一个set按y,它仍将转到最后一个,因为其他只适用于!userResponse!==n部分。

编辑: 亲爱的上帝,这感觉不对,但根据https://stackoverflow.com/a/4006006/5022761,你应该在标签内部使用无用的标签。这将使你的代码像这样:

@echo off
color A
title Trace Route
SETLOCAL ENABLEDELAYEDEXPANSION
:RestartPoint
set /p userResponse="Would you like a list of local devices to trace (y/n)? "
if !userResponse!==y (
  net view
  set /p IPAddress="Enter the IP address that you would like to trace: " 
  echo.
  set /p addressType="Enter the IP Address type (4 or 6): "
  if !addressType!==4 tracert -4 !IPAddress!
  if !addressType!==6 tracert -6 !IPAddress!
  echo.
:test
:openURLRestartPoint1
  set /p userResponseOpenURL="Would you like to map the found IP (y/n)? "
  if !userResponseOpenURL!==y (
    start "" http://www.ip-adress.com/ip_tracer/
  )
  if !userResponseOpenURL!==n (
    echo.
    echo Exiting program...
    echo.
    PAUSE
  ) else (
    echo.
    echo Invalid request. Please try again.
    echo.
    GOTO openURLRestartPoint1
  )
) 
if !userResponse!==n (
  set /p IPAddress="Enter the IP address that you would like to trace: " 
  echo.
  set /p addressType="Enter the IP Address type (4 or 6): "
  if !addressType!==4 tracert -4 !IPAddress!
  if !addressType!==6 tracert -6 !IPAddress!
  echo.
:test
:openURLRestartPoint2
  set /p userResponseOpenURL="Would you like to map the found IP (y/n)? "
  if !userResponseOpenURL!==y (
    start "" http://www.ip-adress.com/ip_tracer/
  )
  if !userResponseOpenURL!==n (
    echo.
    echo Exiting program...
    echo.
    PAUSE
  ) else (
    echo.
    echo Invalid request. Please try again.
    echo.
    GOTO openURLRestartPoint2
  )
) else (
  echo.
  echo Invalid request. Please try again.
  echo.
  GOTO RestartPoint
)

有关if / else / else的更多信息,请查看此处:https://stackoverflow.com/a/25385407/5022761