0

When I run my batch file, I get the error "The syntax of the command is incorrect". The error appears right after I type y or n or anything else at the first user input request (userResponse). I've been commenting out the commands in the code one by one, but I still get the error...

Where does it seem to be?

Thanks!

Code:

@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

)
Philip Becker
  • 21
  • 1
  • 8
  • 3
    You're missing a space before the `(` in `if !userResponseOpenURL!==n(`. Also, labels and gotos don't play nice inside of code blocks; you might want to move the labels to right above their corresponding `if !userResponse!==` statements. – SomethingDark Nov 22 '15 at 21:08

1 Answers1

2

you missed the spaces in

if !userResponseOpenURL!==n(

that should be

if !userResponseOpenURL!==n (

There also seems to be some trouble with labels in if statements, will update my answer when I figured out how to do this, but you'll see that when you remove the labels inside if statements and make the change as suggested above it will work. Finally if you press y at the first set it will still go to the last else because that else only works for the !userResponse!==n part.

EDIT: Dear god this feels wrong, but you should apparently use a useless label above your labels inside if blocks, according to https://stackoverflow.com/a/4006006/5022761. This would make your code like this:

@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
)

For more about the if/else/else if look here: https://stackoverflow.com/a/25385407/5022761

Community
  • 1
  • 1
Dennis van Gils
  • 3,487
  • 2
  • 14
  • 35
  • It would be _way_ better if `:openURLRestartPoint1` was moved above `if !userResponse!==y (` and `:openURLRestartPoint2` was moved above `if !userResponse!==n (` because the value of `!userResponse!` isn't going to change, and this way the labels won't be inside of code blocks. – SomethingDark Nov 22 '15 at 21:43
  • 1
    You're right, but I thought the reason they were placed there was so that the user wouldn't get asked for ip-address and ip-type twice, which this does. Although I agree with you if that isn't important, this code feels like a very bad hack – Dennis van Gils Nov 22 '15 at 21:45
  • Thank you all for your responses! The program is now fixed! :) – Philip Becker Nov 23 '15 at 01:48