Page 1 of 1

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

Posted: 28 Mar 2023 04:50
by crimsonnight
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!

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

Posted: 03 Apr 2023 15:35
by penpen
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

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

Posted: 04 Apr 2023 07:11
by atfon
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