BWBellairs
BWBellairs

Reputation: 23

Batch Script Error on Set /P

I'm trying to get a script to execute something when it detects if an active internet connection is present. This is what I've come up with but I get a syntax error when one one of the if statements isn't run.

for /f "delims=" %%a in ('netsh interface show interface ^| find /I "Enabled        Connected"') do @set netsh_output=%%a

SET /P internet_connected="false" 
IF "%netsh_output%"=="Enabled        Connected      Dedicated        WiFi" (
  SET /P internet_connected="true"
)

IF "%netsh_output%"=="Enabled        Disconnected   Dedicated        Ethernet" (
  SET /P internet_connected="true"
)

if "%internet_connected%"=="true"(
  ECHO connected

)

It works when I have Wifi but seeing as I don't have ethernet and the second doesn't run it raises a syntax error for no reason. Am I not seeing something I should be?

C:\Users\ohheyit'sme\Desktop>IF "Enabled        Connected      Dedicated        WiFi" == "Enabled        Connected      Dedicated        WiFi" (SET /P internet_connected="true" )
true

C:\Users\ohheyit'sme\Desktop>IF "Enabled        Connected      Dedicated        WiFi" == "Enabled        Disconnected   Dedicated        Ethernet" (SET /P internet_connected="true" )
The syntax of the command is incorrect.
C:\Users\ohheyit'sme\Desktop>if "false"=="true"(

Upvotes: 1

Views: 425

Answers (1)

user6250760
user6250760

Reputation:

Typographical Error

if "%internet_connected%"=="true"(

should be

if "%internet_connected%"=="true" (

IF "%netsh_output%"=="Enabled        Disconnected   Dedicated        Ethernet" (

should be

IF "%netsh_output%"=="Enabled        Connected   Dedicated        Ethernet" (

Mis-using set /p

And if you want to set a variable, not ask for input, do not use /p flag.

set Foo=Bar

This sets the variable Foo with the string of Bar.

set /p Foo=Bar

This sets the variable Foo to user's input.

Upvotes: 1

Related Questions