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

Batch file, The syntax of the command is incorrect

当我 运行 我的批处理文件时,我收到错误 "The syntax of the command is incorrect"。在我键入 yn 或第一个用户输入请求 (userResponse) 中的任何其他内容后,错误立即出现。我一直在把代码中的命令一一注释掉,但还是报错...

它好像在哪里?

谢谢!

代码:

@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

)

你错过了

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

应该是

if !userResponseOpenURL!==n (

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

编辑: 天哪,这感觉不对,但根据 ,你显然应该在 if 块内的标签上方使用无用的标签。这将使您的代码像这样:

@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 if 的更多信息,请查看此处: