how to block (from Flash drive) entire .exe files of a specified folder using .bat file in Windows firewall?

CyborgTJB 1 Reputation point
2022-06-07T08:41:32.317+00:00

How to Block entire .exe files of a specified folder in (e.g. "C:\ProgramData\" or "C:\Program Files\") Windows Firewall using .bat?

I want to run it from pen drive for specified folder in "C:\ProgramData\" or in "C:\Program Files\"

Seems like this is exist but it need to be paste at the folder first.


@ setlocal enableextensions

@ cd /d "%~dp0"

for /R %%f in (*.exe) do (

netsh advfirewall firewall add rule name="Blocked: %%f" dir=out program="%%f" action=block

)

pause


What in my mind is to create a multiple .bat file and paste the code, paste the path for different path and run it.
I also want it to show me (create txt file, cmd, or anywhere) what did it do (just in case...).

---this may help, I guess-------------

@ echo off
@setlocal enableextensions
@cd /d "%~dp0"
color F0
title Exe blocker
echo.
echo. ==============================
echo. == Welcome to .exe blocker ==
echo. ==============================
echo.
Echo Current location: %cd%
echo.
CHOICE /C YN /M "Do you want to search in the current location?"
IF Errorlevel 2 goto END
IF Errorlevel 1 goto Yes
::--------------------CHECK and searching files-----------------
:Yes
set location=%cd%
cls
echo.
Echo. Searching for .exe files in %location%
echo.
set /a count=0
echo.___________________________________________
echo.Found:
echo.
FOR %%i in (*.exe) do (echo. %%i & set /a count+=1)
echo.___________________________________________
echo.
title Exe blocker %count% Files found
echo. Number of files found with .exe extention: %count%
echo.
echo.
set add2=

CHOICE /C YN /M "Do you want to add aditional information?"
IF Errorlevel 2 goto block
IF Errorlevel 1 goto ADD
::-----------------ADD additional info---------------
:ADD
Set /p add=Please type the additonal information for the name:
Set add2=%add%
::-----------------Add files to firewall--------------
:block
cls
title Exe blocker - Blocking Rules
set /a countt=0
echo.
echo.___________________________________________
echo. Inbound Rules
echo.___________________________________________
FOR /r %%B in (.exe) do (set /a countt+=1 & echo.%countt%. %%~nxB blocking... & netsh advfirewall firewall add rule name="%add2%%%~nxB" dir=in action=block program="%%~dpfnxB")
echo.___________________________________________
echo. OutBound rules
set /a coun=0
echo.___________________________________________
FOR /r %%B in (
.exe) do (set /a coun+=1 & echo.%coun%. %%~nxB blocking... & netsh advfirewall firewall add rule name="%add2%%%~nxB" dir=out action=block program="%%~dpfnxB")
echo.___________________________________________
Echo. Added files to Firewall
echo.
title Exe blocker - Rules Blocked
Pause
::------------------------END---------------------
:END
cls
ECHO.
Echo. Thanks for using .exe blocker
Echo.

CHOICE /C YN /T 10 /D n /M "Do you want to open firewall? (10 seconds)"
IF Errorlevel 2 goto EXIT
IF Errorlevel 1 goto OPEN
::------------------Open Firewall---------------
:OPEN
start "C:\Windows\System32" rundll32.exe shell32.dll,Control_RunDLL firewall.cpl
Goto EXIT

:EXIT

Windows for business | Windows Client for IT Pros | Networking | Network connectivity and file sharing
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Limitless Technology 39,926 Reputation points
    2022-06-10T07:08:38.387+00:00

    Hi there,

    The below BAT will create a firewall rule to block the folder in the drive, it adds all files in the folders so it even blocks .exe & .dll files.

    @Echo off
    REM
    REM
    cls
    net session >nul 2>&1
    if %errorLevel% == 0 (
    echo.
    ) else (
    GOTO :NOPERM
    )
    SETLOCAL EnableDelayedExpansion
    for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
    set "DEL=%%a"
    )
    @cd /d "%~dp0"
    for %%* in (.) do set RULENAME=%%~nx*
    ECHO|set /p ="- Add "
    call :ColorText 0a "Block In & Out "
    ECHO Firewall rules for all *.exe ^& .dll files
    ECHO.
    ECHO|set /p = "- located at '"
    call :ColorText 0b "%CD%'"
    ECHO (inc subfolders)
    ECHO.
    ECHO|set /p = "- creating "
    call :ColorText 1b "%RULENAME%"
    ECHO as the Firewall rule name ?
    ECHO.
    ECHO.
    ECHO.
    ECHO Press any key to continue or CTRL+C to terminate now ...
    pause >nul
    cls
    Echo.
    FOR /r %%G in ("
    .exe") Do (@Echo %%G
    NETSH advfirewall firewall add rule name="%RULENAME%-%%~nxG" dir=in program="%%G" action="block" enable="yes")
    FOR /r %%G in (".exe") Do (@Echo %%G
    NETSH advfirewall firewall add rule name="%RULENAME%-%%~nxG" dir=out program="%%G" action="block" enable="yes")
    FOR /r %%G in ("
    .dll") Do (@Echo %%G
    NETSH advfirewall firewall add rule name="%RULENAME%-%%~nxG" dir=in program="%%G" action="block" enable="yes")
    FOR /r %%G in ("*.dll") Do (@Echo %%G
    NETSH advfirewall firewall add rule name="%RULENAME%-%%~nxG" dir=out program="%%G" action="block" enable="yes")
    Echo.
    call :ColorText 0a "done"
    ECHO|set /p =" ... Goodbye"
    ECHO.
    ECHO.
    ECHO Press a key to exit ...
    pause >nul
    goto :eof

    :ColorText
    echo off
    <nul set /p ".=%DEL%" > "%~2"
    findstr /v /a:%1 /R "^$" "%~2" nul
    del "%~2" > nul 2>&1
    goto :eof

    :Finish
    Echo.
    Echo.
    Echo Batch ended...
    Goto :END

    :NOPERM
    ECHO.
    ECHO - You must run this file in Administrator mode
    ECHO.
    ECHO.
    ECHO.
    ECHO|SET /p ="- Press any key to exit ..."
    Pause >NUL
    ECHO goodbye
    ECHO.
    ECHO.
    :END

    Hope this resolves your Query !!


    --If the reply is helpful, please Upvote and Accept it as an answer–

    0 comments No comments

  2. CyborgTJB 1 Reputation point
    2022-06-10T14:02:00.277+00:00

    Thanks a lot "LimitlessTechnology-2700"

    I probably asking a stupid question but...

    I mean I run it (as admin) with the a path "C:\Program Files\GOM\GOMPlayerPlus"
    Nothing happen....

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.