.bat file to edit
Moderator: DosItHelp
.bat file to edit
Hello,
I come to you for help
Here is a file to block the .exe with the windows firewall that I found here
https://sites.google.com/site/mytools40 ... le-program…
I would like to modify this .bat so that it blocks only one .exe and also that I can choose which one I want to block
Being a novice in it I come to you to know where to touch
Thanking you
Have a good day
File
http://www.k-upload.fr/afficher-fichier ... l.bat.html
I come to you for help
Here is a file to block the .exe with the windows firewall that I found here
https://sites.google.com/site/mytools40 ... le-program…
I would like to modify this .bat so that it blocks only one .exe and also that I can choose which one I want to block
Being a novice in it I come to you to know where to touch
Thanking you
Have a good day
File
http://www.k-upload.fr/afficher-fichier ... l.bat.html
Re: .bat file to edit
Untested:
Steffen
Code: Select all
@echo off &setlocal
set "exefile=C:\whatever\path\xyz.exe"
for %%i in ("%exefile%") do (
NETSH advfirewall firewall add rule name="blockfirewall-%%~nxi" dir=in program="%%~i" action="block" enable="yes"
NETSH advfirewall firewall add rule name="blockfirewall-%%~nxi" dir=out program="%%~i" action="block" enable="yes"
)
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: .bat file to edit
Step 1 is to paste the code here, select it, and then click the </> button.
I'm lazy and don't feel like reading the entire program, but somebody was nice enough to put the words "Firewall rules for all *.exe files" in the script, so I know to look for the string "exe" elsewhere.
Step 2 is to realize that because all echo does is display things to the screen, you can ignore pretty much everything in the script except the two netsh commands. While you're there, notice the two for loops directly above them that contain the string "exe." Knowing that a for loop given the argument *.exe will loop over all .exe files in the current directory, we know that this is the main bit of the script.
Since you only want to block one application, you don't need a for loop, so just get rid of it and replace %%G with the application you want to block. You can store the application name in a variable beforehand if you don't feel like typing as much.
Code: Select all
@echo off
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 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")
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
Step 2 is to realize that because all echo does is display things to the screen, you can ignore pretty much everything in the script except the two netsh commands. While you're there, notice the two for loops directly above them that contain the string "exe." Knowing that a for loop given the argument *.exe will loop over all .exe files in the current directory, we know that this is the main bit of the script.
Code: Select all
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")
Code: Select all
set blocked_app=skype.exe
NETSH advfirewall firewall add rule name="%blocked_app%" dir=in program="%blocked_app%" action="block" enable="yes"
NETSH advfirewall firewall add rule name="%blocked_app%" dir=out program="%blocked_app%" action="block" enable="yes"
Re: .bat file to edit
Thank you so much for your answer to both of you
ShadowThief I did not understand how to proceed
If I want for example block RevoUninPro. exe in the folder
C:\Program Files\VS Revo Group\Revo Uninstaller Pro
I have to copy my bat file and paste it into this folder
And after I change what sentence in my bat file so that it crashes that RevoUninPro. exe?
Thanking you
ShadowThief I did not understand how to proceed
If I want for example block RevoUninPro. exe in the folder
C:\Program Files\VS Revo Group\Revo Uninstaller Pro
I have to copy my bat file and paste it into this folder
And after I change what sentence in my bat file so that it crashes that RevoUninPro. exe?
Thanking you
Re: .bat file to edit
This script seems to work
@echo off &setlocal
set "exefile=C:\Program Files (x86)\Bandicam\bdcam.exe"
for %%i in ("%exefile%") do (
NETSH advfirewall firewall add rule name="blockfirewall-%%~nxi" dir=in program="%%~i" action="block" enable="yes"
NETSH advfirewall firewall add rule name="blockfirewall-%%~nxi" dir=out program="%%~i" action="block" enable="yes"
)
but it is necessary all the time to put the path of the exe
can we ask him to look for the .exe when copying the .bat file in the installation folder
Thank you
@echo off &setlocal
set "exefile=C:\Program Files (x86)\Bandicam\bdcam.exe"
for %%i in ("%exefile%") do (
NETSH advfirewall firewall add rule name="blockfirewall-%%~nxi" dir=in program="%%~i" action="block" enable="yes"
NETSH advfirewall firewall add rule name="blockfirewall-%%~nxi" dir=out program="%%~i" action="block" enable="yes"
)
but it is necessary all the time to put the path of the exe
can we ask him to look for the .exe when copying the .bat file in the installation folder
Thank you
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: .bat file to edit
You're going to need the full path for the "program=%%~i" part of the script; otherwise the Firewall won't know where the application is located. (I suspect it will look in every directory listed in %PATH%, but I'm not positive.)
For my script (and aGerman's), all you need to do is set the value of the blocked_app variable to the full path of the application that you want to block.
For my script (and aGerman's), all you need to do is set the value of the blocked_app variable to the full path of the application that you want to block.
Re: .bat file to edit
@echo off &setlocal
set "exefile=C:\Program Files (x86)\Bandicam\bdcam.exe"
for %%i in ("%exefile%") do (
NETSH advfirewall firewall add rule name="blockfirewall-%%~nxi" dir=in program="%%~i" action="block" enable="yes"
NETSH advfirewall firewall add rule name="blockfirewall-%%~nxi" dir=out program="%%~i" action="block" enable="yes"
)
Thank you
it blocks the application that I want but it is necessary all the time put the access path as the example above
I have several applications to block in each different folder
I would like that when I stick the .bat in a folder he finds me my exe that I want to block
can you give me an example?
Excuse me if I have trouble understanding
Thank you very much for your help
set "exefile=C:\Program Files (x86)\Bandicam\bdcam.exe"
for %%i in ("%exefile%") do (
NETSH advfirewall firewall add rule name="blockfirewall-%%~nxi" dir=in program="%%~i" action="block" enable="yes"
NETSH advfirewall firewall add rule name="blockfirewall-%%~nxi" dir=out program="%%~i" action="block" enable="yes"
)
Thank you
it blocks the application that I want but it is necessary all the time put the access path as the example above
I have several applications to block in each different folder
I would like that when I stick the .bat in a folder he finds me my exe that I want to block
can you give me an example?
Excuse me if I have trouble understanding
Thank you very much for your help
Re: .bat file to edit
As long as there is only the .exe file in the folder that you want to block then use
Steffen
Code: Select all
@echo off &setlocal
for %%i in (*.exe) do (
NETSH advfirewall firewall add rule name="blockfirewall-%%~nxi" dir=in program="%%~fi" action="block" enable="yes"
NETSH advfirewall firewall add rule name="blockfirewall-%%~nxi" dir=out program="%%~fi" action="block" enable="yes"
)
Re: .bat file to edit
Thank you but I change your script
but if i put the bat file in the software installation folder
and if after I go to see on the side of the firewall the path of the exe is not good
I have C:\Windows\System32\bdcam.exe
and it should be
C:\Program Files (x86)\Bandicam\bdcam.exe
Thanking you
Code: Select all
@echo off &setlocal
for %%i in (bdcam.exe) do (
NETSH advfirewall firewall add rule name="BANDICAM-%%~nxi" dir=in program="%%~fi" action="block" enable="yes"
NETSH advfirewall firewall add rule name="BANDICAM-%%~nxi" dir=out program="%%~fi" action="block" enable="yes"
)
and if after I go to see on the side of the firewall the path of the exe is not good
I have C:\Windows\System32\bdcam.exe
and it should be
C:\Program Files (x86)\Bandicam\bdcam.exe
Thanking you
Re: .bat file to edit
I assume you run the script as administrator via right click. This way the working directory is automatically set to C:\Windows\System32. Begin your batch script withto change the working directory back to the script folder.
Steffen
Code: Select all
@echo off &setlocal
cd /d "%~dp0"
Steffen
Re: .bat file to edit
Thank you very much for your answer
it works very well for one of my software
by doing this script
but not on everything when I take the same file by change the script as attached
it cuts me the file in three parts in the firewall so it does not block my file Corel PaintShop Pro.exe
It's weird it creates three firewalls with part of the name Corel, PaintShop and Pro
it must come from the name I put "for %%i in (Corel PaintShop Pro.exe) do (" which is wrong but almost must be put as it is a broken word
Thanking you for your patience
it works very well for one of my software
by doing this script
Code: Select all
@echo off &setlocal
cd /d "%~dp0"
for %%i in (bdcam.exe) do (
NETSH advfirewall firewall add rule name="Bandicam" dir=in program="%%~fi" action="block" enable="yes"
NETSH advfirewall firewall add rule name="Bandicam" dir=out program="%%~fi" action="block" enable="yes"
)
but not on everything when I take the same file by change the script as attached
Code: Select all
@echo off &setlocal
cd /d "%~dp0"
for %%i in (Corel PaintShop Pro.exe) do (
NETSH advfirewall firewall add rule name="Corel PaintShop Pro 2018" dir=in program="%%~fi" action="block" enable="yes"
NETSH advfirewall firewall add rule name="Corel PaintShop Pro 2018" dir=out program="%%~fi" action="block" enable="yes"
)
It's weird it creates three firewalls with part of the name Corel, PaintShop and Pro
it must come from the name I put "for %%i in (Corel PaintShop Pro.exe) do (" which is wrong but almost must be put as it is a broken word
Thanking you for your patience
Re: .bat file to edit
As always - paths / file names that contain spaces have to be enclosed in Quotation marks (and it doesn't do any harm if you do the same with file names that don't contain spaces).
Steffen
Code: Select all
for %%i in ("Corel PaintShop Pro.exe") do (
Re: .bat file to edit
Thank you so much it works impeccably well
Thank you for all and good day
Thank you for all and good day