How to check if a wired connection is present in a batch file?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
crimsonnight
Posts: 1
Joined: 28 Mar 2023 04:48

How to check if a wired connection is present in a batch file?

#1 Post by crimsonnight » 28 Mar 2023 04:50

I'm running the following batch file when logging in:

Code: Select all

::if not DEFINED IS_MINIMIZED set IS_MINIMIZED=1 && start "" /min "%~dpnx0" %* && exit

:: See if NAS is connected locally
Dir \\192.168.0.26\Docker\CheckFile.txt
If %ErrorLevel% EQU 0 GoTo FileSpecifiedIsHere
GoTo ItIsntHere
Pause This line is never reached

:FileSpecifiedIsHere
::Do whatever I want to if the one specified is detected
Start "Starting the application" /MIN "C:\Program Files (x86)\Razer\Synapse3\WPFUI\Framework\Razer Synapse 3 Host\Razer Synapse 3.exe
GoTo EndThisBatch

:ItIsntHere
::Do whatever I want to if the one specified is not detected
Start "Starting the application" /MIN "C:\Program Files (x86)\ASUSTOR\EZ Connect\EasyConnect.exe"
GoTo EndThisBatch

:EndThisBatch
This worked as intended on my previous laptop, but it always seems to jump to 'ItIsntHere' when ran at login on this laptop. If I run the batch file manually it's working as intended, it just isn't working properly at login. I've tried added delays and that hasn't made a difference.
I think the next step would be instead of checking for my NAS, checking if an ethernet connection is present or not - how would I set up a check for this?

Cheers!

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How to check if a wired connection is present in a batch file?

#2 Post by penpen » 03 Apr 2023 15:35

The following might help you:

Code: Select all

powershell -NoProfile -Command get-NetIPConfiguration
Though i would add debug messages (using echo) and pauses, to see the program flow.
In case you don't have a visible console on login, you might redirect the echoes and avoid the pause commands instead.


penpen

atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Re: How to check if a wired connection is present in a batch file?

#3 Post by atfon » 04 Apr 2023 07:11

You can check the NdisMedium property of MSFT_NetAdapter if you have WMIC installed. Something like this will get you information about the type of connection you have:

Code: Select all

%__APPDIR__%wbem\WMIC.exe /NameSpace:\\root\StandardCimv2 path MSFT_NetAdapter where "InterfaceOperationalStatus='1' and Virtual='FALSE'" get NdisMedium
See this site for more information: https://wutils.com/wmi/root/standardcim ... properties

Post Reply