.bat file to edit

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
bubuleur
Posts: 7
Joined: 21 Jan 2018 08:51

.bat file to edit

#1 Post by bubuleur » 21 Jan 2018 08:59

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: .bat file to edit

#2 Post by aGerman » 21 Jan 2018 18:54

Untested:

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"
)
Steffen

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: .bat file to edit

#3 Post by ShadowThief » 21 Jan 2018 19:05

Step 1 is to paste the code here, select it, and then click the </> button.

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
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.

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")
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

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"

bubuleur
Posts: 7
Joined: 21 Jan 2018 08:51

Re: .bat file to edit

#4 Post by bubuleur » 22 Jan 2018 02:19

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

bubuleur
Posts: 7
Joined: 21 Jan 2018 08:51

Re: .bat file to edit

#5 Post by bubuleur » 22 Jan 2018 09:54

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

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: .bat file to edit

#6 Post by ShadowThief » 22 Jan 2018 17:19

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.

bubuleur
Posts: 7
Joined: 21 Jan 2018 08:51

Re: .bat file to edit

#7 Post by bubuleur » 24 Jan 2018 02:29

@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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: .bat file to edit

#8 Post by aGerman » 24 Jan 2018 11:47

As long as there is only the .exe file in the folder that you want to block then use

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"
)
Steffen

bubuleur
Posts: 7
Joined: 21 Jan 2018 08:51

Re: .bat file to edit

#9 Post by bubuleur » 25 Jan 2018 05:50

Thank you but I change your script

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"
)
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
Image
and it should be
C:\Program Files (x86)\Bandicam\bdcam.exe
Thanking you

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: .bat file to edit

#10 Post by aGerman » 25 Jan 2018 10:15

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 with

Code: Select all

@echo off &setlocal
cd /d "%~dp0"
to change the working directory back to the script folder.

Steffen

bubuleur
Posts: 7
Joined: 21 Jan 2018 08:51

Re: .bat file to edit

#11 Post by bubuleur » 26 Jan 2018 02:21

Thank you very much for your answer
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"
)
Image

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 cuts me the file in three parts in the firewall so it does not block my file Corel PaintShop Pro.exe

Image

Image

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: .bat file to edit

#12 Post by aGerman » 26 Jan 2018 03:27

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).

Code: Select all

for %%i in ("Corel PaintShop Pro.exe") do (
Steffen

bubuleur
Posts: 7
Joined: 21 Jan 2018 08:51

Re: .bat file to edit

#13 Post by bubuleur » 26 Jan 2018 04:02

Thank you so much it works impeccably well
Thank you for all and good day

Post Reply