Disable/enable lan-device by batchfile: If comparisons not working

1

I have created this .bat-file which is invoked by clicking on the proper shortcut, which is configured properly. But the last two comparisons seem not to evaluate properly.

Here we go.

@ECHO off
SETLOCAL EnableDelayedExpansion 

SET landevcename='Imagine any name for a device'

SET landevcestatus=0
SET landevcestatusDisab=0
SET landevcestatusConn=2
SET landevcestatusT="NONE"
SET landevceidx=0

SET counter=1

ECHO Lan device to be checked is: %landevcename%

FOR /F "tokens=1" %%I IN ('wmic PATH win32_networkadapter where "Name=%landevcename%" get index')  DO ( 
        IF !counter!==2 SET /a landevceidx=%%I             

        SET /a counter+=1
     )
ECHO Index of local area connection is: %landevceidx%

SET /a counter=1
FOR /F "tokens=1" %%J IN ('wmic PATH win32_networkadapter where "Name=%landevcename%" get netconnectionstatus')  DO (       
                            IF !counter!==2 SET /a landevcestatus=%%J              

                            SET /a counter+=1
                           )

ECHO Status of local area connection is: %landevcestatus%                          

REM IF %landevcestatus%==%landevcestatusDisab% SET /a landevcestatusT=Disabled
IF %landevcestatus%==0 SET /a %landevcestatusT% "ItIsNotEnabled"

REM IF %landevcestatus%==%landevcestatusConn% SET /a landevcestatusT=Connected         
**IF %landevcestatus%==2 SET /a %landevcestatusT% "ItIsEnabled"**

ECHO Text - Status of local area connection is: %landevcestatusT%

ECHO Status of local area connection is: %landevcestatus%                          

CMD
@ECHO off

Those both:

IF %landevcestatus%==0 SET /a %landevcestatusT% "ItIsNotEnabled"

IF %landevcestatus%==2 SET /a %landevcestatusT% "ItIsEnabled"

Where could be the typo ?

icbytes

Posted 2016-03-02T14:06:38.940

Reputation: 121

Can you tell me the errors you are getting? – JohnnyVegas – 2016-03-02T14:23:00.470

ECHO Text - Status of local area connection is: %landevcestatusT% is NONE. – icbytes – 2016-03-02T14:24:03.627

Could you try setting an IF/ELSE rather than two separate FOR statements? It's seems a really complex routine for such a simple output. The problems are in the IF statements as it's only getting the variable you set at the start - ignoring everything else. – JohnnyVegas – 2016-03-02T14:41:48.203

You mean instead of two seperate if's ? not for's ? – icbytes – 2016-03-02T14:42:40.930

Answers

2

Where could be the typo?

  • You have many errors in your batch file.

  • You are clearly confused about how to use set and set /a. Please read set.

  • It would also be worth you reading Debugging your batch files.

  • A general observation - fix your indentation (it makes debugging easier).


Corrections

SET landevcename='Imagine any name for a device'

Should be:

SET landevcename="Imagine any name for a device"

If you use ' then you need to escape the ' in your for command.


IF !counter!==2 SET /a landevceidx=%%I

Should be:

IF !counter!==2 SET landevceidx=%%I

/a is used for numeric expressions not string assignments.


IF !counter!==2 SET /a landevcestatus=%%J 

Should be:

IF !counter!==2 SET landevcestatus=%%J

SET /a counter+=1

Should be:

SET /a "counter+=1"

IF %landevcestatus%==0 SET /a %landevcestatusT% "ItIsNotEnabled"

Should be:

IF %landevcestatus%==0 SET landevcestatusT="ItIsNotEnabled"

IF %landevcestatus%==2 SET /a %landevcestatusT% "ItIsEnabled"

Should be:

IF %landevcestatus%==2 SET landevcestatusT="ItIsEnabled"

FOR /F "tokens=1" %%I IN ('wmic PATH win32_networkadapter where "Name=%landevcename%" get index')  DO ( 

Should be:

FOR /F "tokens=1" %%I IN ('wmic PATH win32_networkadapter where Name^=%landevcename% get index')  DO ( 

You need to escape the = using ^, and remove the "s (they are part of the variable %landevcename%).


Simplified batch file

You can do what you want with a single for /f command and fewer variables. You don't need an index or a counter.

Use the following batch file and set landevicename as appropriate.

LanStatus.cmd:

@echo off
setlocal 

set landevcename="Remote NDIS based Internet Sharing Device"

echo Lan device to be checked is: %landevcename%

rem skip first line
rem use findstr to strip blank lines from wmic output
for /f "usebackq skip=1 tokens=1,2" %%i IN (`wmic PATH win32_networkadapter where Name^=%landevcename% get netconnectionstatus^, index ^| findstr /r /v "^$"`)  DO (  set landevceidx=%%i
  set landevcestatus=%%j       
  )

if %landevcestatus%==2 (
  set landevcestatusT="ItIsEnabled"
  ) else (
  set landevcestatusT="ItIsNotEnabled"
  )

echo Text - Status of local area connection is: %landevcestatusT%

echo Status of local area connection is: %landevcestatus%                        

echo Index of local area connection is: %landevceidx%

endlocal

Example output:

F:\test>lanstatus
Lan device to be checked is: "Remote NDIS based Internet Sharing Device"
Text - Status of local area connection is: "ItIsEnabled"
Status of local area connection is: 2
Index of local area connection is: 17

Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • findstr - Search for strings in files.
  • for /f - Loop command against the results of another command.
  • if - Conditionally perform a command.
  • set - Display, set, or remove CMD environment variables. Changes made with SET will remain only for the duration of the current CMD session.
  • wmic - Windows Management Instrumentation Command.

DavidPostill

Posted 2016-03-02T14:06:38.940

Reputation: 118 938

SET landevcename=Imagine any name for a device"

looks faulty. may be, forgot first quotes ? – icbytes – 2016-03-02T14:54:42.850

Answer fixed ... – DavidPostill – 2016-03-02T14:55:37.610

Did You also see, that sometimes /a is needed, afaik, because of the SetEnbleDelayesExpansion. I have it even from a SO site, partially ... – icbytes – 2016-03-02T14:59:47.053

/a has nothing to do with delayed expansion. It is for arithmetic expressions. Please read the link I gave you. – DavidPostill – 2016-03-02T15:01:40.597

Thanks once again, I have also been on that site some time, still kept the /a because it seemed to work mostly. Anyway, I will adopt the script as You recommended, and return with the results soon. – icbytes – 2016-03-02T15:03:27.180

But I have this tried before : SET landevcename='"magine any name for a device"

This will fail in WMIC. – icbytes – 2016-03-02T15:09:52.553

@icbytes There were more mistakes in your batch file. I'm adding a simplified batch file that works. Please wait a few minutes for me to update the answer. – DavidPostill – 2016-03-02T15:12:22.660

Perfect, tiny, findtr properly used, I appreciate this. – icbytes – 2016-03-02T15:34:56.340

One more thing. My usual aim, why I want to get the index is to either enable or disable it, like toggle. – icbytes – 2016-03-02T16:38:14.823

Answer updated to save and echo index – DavidPostill – 2016-03-02T17:05:22.207