netsh command returning unrecognized error, unknown reason

3.8k views Asked by At
@echo off
@title Program Internet Disconnector
REM check for admin rights code was here but is too long for this post    
cls
setlocal EnableDelayedExpansion
cls
echo Press any key to Start
pause >nul
cls
set i=0
for %%x in (exe) do (
for /r %%a in (*.%%x) do (
set /a i+=1
set P!i!=%%~fdpa
set F!i!=%%~nxa
))
set y=0
:lock
set /a y+=1
if %y% gtr %i% (
goto Done)
set exec=!F%y%!
set path=!P%y%!
echo:

These values for the variables exec and path, return normally (i.e. executable.exe and C:\) just like they should:

echo %exec%
echo %path%
pause

Here though, the issue is that the netsh command returns the error 'netsh' is not recognized as an internal or external command, operable program or batch file.

netsh advfirewall firewall add rule name="%exec%" dir=in action=block program="%path%" enable=yes
netsh advfirewall firewall add rule name="%exec%" dir=out action=block program="%path%" enable=yes
pause
goto lock
:Done
echo:
echo Press any key to exit...
Pause >nul
exit

What is the problem here as I cannot tell why this code is not just blocking the program and moving on. I've even used this code once before in a similar way without the bug. Does anyone know what's wrong?

1

There are 1 answers

1
Magoo On

PATH is a special variable - it contains a list of ;-separated directories which are searched for an executable. When you change path, the executable netsh.exe can no longer be found.

Solution : don't attempt to use path as a user-variable (equally, any other variable listed when you issue a set command at the prompt.) - and others like random,time,date etc...