Last active
July 14, 2026 03:36
-
-
Save ph33nx/48a0147c6fdf9fefb38734fe0d3b3ab5 to your computer and use it in GitHub Desktop.
Batch script to block internet access for all .exe files in a folder recursively using Windows Firewall (inbound and outbound rules). Includes dynamic folder path input, usage instructions, and automation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| :: Batch Script: block_folder.bat | |
| :: Author: https://github.com/ph33nx | |
| :: Description: Blocks all .exe files in the specified folder (and subfolders) from accessing the internet (both inbound and outbound) using Windows Firewall. | |
| :: Usage: | |
| :: block_folder.bat [FolderPath] | |
| :: - Pass the folder path containing the .exe files to block. | |
| :: block_folder.bat -h | |
| :: - Displays this help message. | |
| :: Check for admin rights and relaunch as admin if not already | |
| @echo off | |
| NET SESSION >nul 2>&1 | |
| IF %ERRORLEVEL% NEQ 0 ( | |
| echo [INFO] Requesting administrator privileges... | |
| powershell -Command "Start-Process '%~f0' -ArgumentList '%*' -Verb RunAs" | |
| exit /b | |
| ) | |
| setlocal enabledelayedexpansion | |
| REM Check if a parameter is passed | |
| if "%~1"=="" goto help | |
| if "%~1"=="-h" goto help | |
| REM Get the folder path from the argument | |
| set "folderPath=%~1" | |
| REM Verify if the folder exists | |
| if not exist "%folderPath%" ( | |
| echo [ERROR] The specified folder does not exist: %folderPath% | |
| exit /b 1 | |
| ) | |
| REM Extract base folder name from the provided path | |
| for %%A in ("%folderPath%") do set "baseFolderName=%%~nA" | |
| REM Display a confirmation message | |
| echo [INFO] Blocking all .exe files in folder: %folderPath% | |
| REM Loop through all .exe files in the folder and create firewall rules | |
| for /r "%folderPath%" %%F in (*.exe) do ( | |
| set "filePath=%%F" | |
| set "fileName=%%~nxF" | |
| REM Enable delayed expansion inside the loop to use updated variables | |
| call :AddFirewallRules "!filePath!" "!baseFolderName!" "!fileName!" | |
| ) | |
| REM Final message | |
| echo [INFO] All .exe files in %folderPath% have been blocked (inbound and outbound). | |
| echo [INFO] You can view the rules in Windows Defender Firewall with Advanced Security by opening it from the Start menu. | |
| exit /b 0 | |
| :AddFirewallRules | |
| REM Arguments: %1 = filePath, %2 = baseFolderName, %3 = fileName | |
| set "filePath=%~1" | |
| set "baseFolderName=%~2" | |
| set "fileName=%~3" | |
| REM Generate rule names with base folder name and file name | |
| set "ruleName=Block %baseFolderName% %fileName% (automated)" | |
| echo [INFO] Adding outbound block rule for: %filePath% | |
| netsh advfirewall firewall add rule name="%ruleName% OUT" dir=out program="%filePath%" action=block enable=yes >nul | |
| echo [INFO] Adding inbound block rule for: %filePath% | |
| netsh advfirewall firewall add rule name="%ruleName% IN" dir=in program="%filePath%" action=block enable=yes >nul | |
| goto :eof | |
| :help | |
| echo Usage: block_folder.bat [FolderPath] | |
| echo. | |
| echo This script blocks all .exe files in the specified folder and its subfolders from accessing the internet (inbound and outbound) using Windows Firewall. | |
| echo. | |
| echo Parameters: | |
| echo FolderPath - Path to the folder containing .exe files to block. | |
| echo -h - Display this help message. | |
| echo. | |
| echo Example: | |
| echo block_folder.bat "C:\Program Files\Adobe" | |
| exit /b 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ty for codes, and this one for simply Run and Paste ur Folder Location:
@echo off
NET SESSION >nul 2>&1
IF %ERRORLEVEL% NEQ 0 (
powershell -Command "Start-Process '%~f0' -Verb RunAs"
exit /b
)
setlocal enabledelayedexpansion
REM Ask input
echo Block Internet Connection
echo.
set /p "folderPath=Folder Location: "
REM Remove the quotes
set "folderPath=%folderPath:"=%"
REM Check input empty
if "%folderPath%"=="" (
echo [ERROR] Path folder cant be empty!
pause
exit /b 1
)
REM Check Folder
if not exist "%folderPath%" (
echo [ERROR] Folder not found: %folderPath%
echo [INFO] Make sure ur folder correctly.
pause
exit /b 1
)
REM Ekstract folder name
for %%A in ("%folderPath%") do set "baseFolderName=%%~nA"
REM Confirm
echo.
echo [INFO] Folder: %folderPath%
echo [INFO] Name: %baseFolderName%
echo.
REM Loop all file .exe in folder and subfolder
set "count=0"
for /r "%folderPath%" %%F in (*.exe) do (
set /a count+=1
set "filePath=%%F"
set "fileName=%%~nxF"
call :AddFirewallRules "!filePath!" "!baseFolderName!" "!fileName!"
)
REM Last
echo.
echo ============================================
echo PROSESS SUCCES
echo ============================================
echo [INFO] %count% file .exe Blocked
echo [INFO] Check Firewall
echo.
pause
exit /b 0
:AddFirewallRules
REM Parameter: %1 = filePath, %2 = baseFolderName, %3 = fileName
set "filePath=%~1"
set "baseFolderName=%~2"
set "fileName=%~3"
REM Create new rule
set "ruleName=Block %baseFolderName% %fileName% (automated)"
echo [BLOCK] Outbound: %fileName%
netsh advfirewall firewall add rule name="%ruleName% OUT" dir=out program="%filePath%" action=block enable=yes >nul 2>&1
echo [BLOCK] Inbound: %fileName%
netsh advfirewall firewall add rule name="%ruleName% IN" dir=in program="%filePath%" action=block enable=yes >nul 2>&1
goto :eof