FireWall Bach Script Help?

Discussion in 'Scripting' started by HORIZONTAL THINKER, Aug 12, 2016.

  1. HORIZONTAL THINKER

    HORIZONTAL THINKER MDL Member

    Jun 13, 2012
    155
    22
    10
    #1 HORIZONTAL THINKER, Aug 12, 2016
    Last edited by a moderator: Apr 20, 2017
    Hi guys.

    Let me start by saying, the script below has been compiled adding bits and pieces of batch scripts from different sources and lots of trial and error. I do not claim this to be my own work. For starters i can't code/script At All!

    Now that that's out of the way, here's my question i hope one of you guys can help me with.

    I've used this script for a number of years without ever having any issue, until recently.

    Of late, i've begun to install the Windows O/S on the C:/ partition and program Files on the E:/ partition.

    When installing new software, if i physically change the softwares install path to e.g. E:/Program Files (x86)/Example, the batch script still works flawlessly once i change 3 lines within the script i.e. change the 3 lines containing:

    C:/Program Files (x86)/Example

    To

    E:/Program Files (x86)/Example

    Now her's where i've run into an issue. I've went a step further and modified the regedit key:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion

    ProgramFilesDir From C to E to make it the default installation directory for new software.

    Now when i run the batch script it deposits itself in C:/Windows/system32

    Any help with fixing / ammending the batch script would be really appreciated. Script below spoiler:

    Code:
    @ echo off
    @copy %0 "C:\Program Files (x86)\Example\Example"
    @setlocal enableextensions
    @cd "C:\Program Files (x86)\Example\Example"
    color 09
    title HORIZONTAL THINKER FireWall Tool
    echo.
    
    Echo Current location: %cd%
    echo.
    echo.
    echo.
    CHOICE /C YN /M "Do you want to search for executables 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 HORIZONTAL THINKER FireWall Tool %count% Files found
    echo.          Number of files found with the .exe extention: %count%
    echo.
    echo.
    set add2=
    
    CHOICE /C YN /M        "Do you want to add additional 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 HORIZONTAL THINKER FireWall Tool - 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 Windows FireWall
    echo.
    title HORIZONTAL THINKER FireWall Tool locked
    Pause
    
    ::------------------------END---------------------
    
    :END
    cls
    ECHO.
    Echo. Thanks for using HORIZONTAL THINKER FireWall Tool
    Echo.
    
    CHOICE /C YN /T 10 /D n /M "Do You Want To Open Windows 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
    
    start /b "" cmd /c del "C:\Program Files (x86)\Example\Example\FireWallLazyBoy.bat"&exit /b
    

    Many thanks in advance.
     
  2. KNARZ

    KNARZ MDL Addicted

    Oct 9, 2012
    872
    457
    30
    uhhh....
    this script is a mess, sorry.

    1) replace all with C:\Program Files (x86)
    %ProgramFiles(x86)%

    or set it as own Variable:

    set "PF86=C:\Program Files (x86)" (or E:)
    and replace all other "C:\Program Files (x86)" with %PF86%

    than you only have one line to take care off.

    Why the count of exe files? for what?
    the Add and block section would run both if ADD gets choosen. (missing goto :EOF)

    add and add2 can't work. you never set add.

    %%~dpfnx = %~fB

    if you use do ()
    use it with multiplie lines

    do (
    echo 1
    netsh ...
    echo.
    )

    i can't fix this
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. ofernandofilo

    ofernandofilo MDL Member

    Sep 26, 2015
    211
    128
    10
    #3 ofernandofilo, Aug 12, 2016
    Last edited: Aug 12, 2016
    First, you can not navigate to a disk folder in another drive without the pushd and popd commands.

    see: http://ss64.com/nt/pushd.html
    see: http://ss64.com/nt/popd.html

    As I never seen your script before, and I'm a little out of time and very tired now - I had a long day at work -, I can not help you more. Maybe tomorrow I can give you other suggestions. Possibly, for now, just apply some pushds and popds and everything should work!

    cheers
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. KNARZ

    KNARZ MDL Addicted

    Oct 9, 2012
    872
    457
    30
    #4 KNARZ, Aug 13, 2016
    Last edited by a moderator: Apr 20, 2017
    you can:
    Code:
    cd /d
    anyway I can't fix it because I would have to rewrite it.

    you can be happy if ofernandofilo fixes/rewrites it.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. HORIZONTAL THINKER

    HORIZONTAL THINKER MDL Member

    Jun 13, 2012
    155
    22
    10
    #5 HORIZONTAL THINKER, Aug 13, 2016
    Last edited: Aug 13, 2016
    (OP)
    So,

    So i can actually learn something. Could you guys explain the corrections and what they do in relation to the function of the script.

    Sorry guys. But i'm a question machine to try and understand :)

    Re-Write the script but explain to me the additions and their functions please
     
  6. KNARZ

    KNARZ MDL Addicted

    Oct 9, 2012
    872
    457
    30
    rewrite the code e.g. with our first recommended improvments.
    remove every echo line. - just use 1-2 REM or :: lines to shortly discribe what you want to do in this section.
    write every multi command ( & / &&) in a single line
    read the help of "set" and substitute everything that you use more than 3 times (except echo).
    check all your variables. - e.g. coun, count, countt, add, add2 and so on.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. ofernandofilo

    ofernandofilo MDL Member

    Sep 26, 2015
    211
    128
    10
    #7 ofernandofilo, Aug 14, 2016
    Last edited by a moderator: Apr 20, 2017
    Man, I will consider that your script works in normal cases, such as when the Program Files folder is on drive C.

    And I will make use of the guidance provided by our KNARZ forum mate.

    Thank you, KNARZ, for guidance, I had never really heard of the parameter /D on the CD command. In fact, I do not remember used CD /? in life.

    * I have not tested your script before. And I have not tested your script later. The changes were small, and if your script was working before, I believe it will continue to function after the changes. As can be noted, the changes were minimal.

    Code:
    @echo off & color 09 & title HORIZONTAL THINKER FireWall Tool
    :: REM I just turned small commands in a single command line using "&".
    setlocal enableextensions
    copy %0 "%ProgramFiles(x86)%\Example\Example"
    cd /D "%ProgramFiles(x86)%\Example\Example"
    echo.
    Echo Current location: %cd%
    echo.
    echo.
    echo.
    
    CHOICE /C YN /M "Do you want to search for executables 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 HORIZONTAL THINKER FireWall Tool %count% Files found
    echo.          Number of files found with the .exe extention: %count%
    echo.
    echo.
    :: REM set add2=
    :: REM I 'commented' the above line because I believe it has no use.
    :: REM ":: and REM" are two ways to make comments in the code.
    :: When I use this "::", in really I creat a invalid label,
    :: It acts like a comment, and in the old times, It will run faster.
    REM When I use the term "REM", I'm using the official way. No big deal.
    REM There are cases that the use of "REM" is required, but I see no sense in explaining here.
    :: REM I usually use both on the same line. There is no advantage. I just got used to doing it.
    
    CHOICE /C YN /M        "Do you want to add additional 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 HORIZONTAL THINKER FireWall Tool - 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 Windows FireWall
    echo.
    title HORIZONTAL THINKER FireWall Tool locked
    Pause
    
    ::------------------------END---------------------
    
    :END
    cls
    ECHO.
    Echo. Thanks for using HORIZONTAL THINKER FireWall Tool
    Echo.
    
    CHOICE /C YN /T 10 /D n /M "Do You Want To Open Windows 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
    :: REM Goto EXIT
    :: REM I believe you don't need the above line.
    
    :EXIT
    ENDLOCAL
    start /b "" cmd /c del "%ProgramFiles(x86)%\Example\Example\FireWallLazyBoy.bat" & exit /b
    
    cheers
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. HORIZONTAL THINKER

    HORIZONTAL THINKER MDL Member

    Jun 13, 2012
    155
    22
    10
    Many thanks ofernandofilo and everyone who contributed to my query.
    Like i mentioned in my OP, i don't/can't code so this is gold dust for me on a learning curve.
    @ofernandofilo. I'll run this on a VM tomorrow after i get home from work and post my results.

    Again, many thanks guys.
     
  9. Compo

    Compo MDL Member

    Mar 29, 2011
    137
    103
    10
    #9 Compo, Aug 18, 2016
    Last edited by a moderator: Apr 20, 2017
    My suggestion is to allow the end user to choose the path, (this would keep your script modular).
    Code:
    @Echo Off
    If "%1"=="restart" GoTo :Script
    Setlocal
    
    Rem ---------------Choose Script Location---------------
    Echo(
    Echo( Example: "%ProgramFiles(x86)%\Example\Example"
    Echo(
    Set/P "ScrAt=Please type the intended path for your script: "
    Echo(
    PushD "%ScrAt%" && (If Not Exist "%~nx0" Copy %~0 1>Nul) || Exit/B
    Cmd /E:On /V:Off /C "%ScrAt%\%~nx0" restart
    Exit/B
    
    Rem ---------------------Main Script--------------------
    :Script
    Color 09
    SetLocal
    Set Title=HORIZONTAL THINKER FireWall Tool
    Title=%Title%
    Echo(
    Echo( Do you want to search for executables in the current location
    Echo(
    Choice /C YN /M "%CD%? "
    If %ErrorLevel%==2 GoTo :End
    
    Rem --------------CHECK And Searching Files-------------
    Cls
    Echo(
    Echo( Searching for .exe files in %CD%
    Echo(
    (Set Count=0)
    Echo(___________________________________________
    Echo(
    For %%a In (*.exe) Do (Echo( %%a
    Set/A Count+=1
    )
    Echo(___________________________________________
    Echo(
    Title %Title% %Count% Files found
    Echo(Number of .exe files found: %Count%
    Echo(
    
    Choice /C YN /M "Do you want to add additional information?"
    If %Errorlevel%==2 GoTo :Block
    
    Rem ----------------ADD Additional Info?----------------
    Echo(
    Set/P "add=Please type the additonal information for the name: "
    
    Rem ----------------Add Files To Firewall---------------
    :Block
    Cls
    Title %Title% - Blocking Rules
    (Set FAR=NetSh AdvFirewall Firewall Add Rule)
    Echo(
    Echo(___________________________________________
    Echo( Inbound and Outbound Rules
    Echo(___________________________________________
    For %%B In (*.exe) Do (Echo( blocking %%~nxB...
    %FAR% Name="%add%%%~nxB" Dir=In Action=Block Program="%%~fB"
    %FAR% Name="%add%%%~nxB" Dir=Out Action=Block Program="%%~fB"
    )
    Echo(
    Echo( Completed
    Echo(___________________________________________
    Title %Title% locked
    Timeout 7
    
    Rem -------------------------END------------------------
    :End
    Cls
    Echo(
    Echo( Thanks for using %Title%
    Echo(
    Choice /C YN /T 10 /D n /M "Do You Want To Open Windows FireWall? (10 seconds)"
    If %ErrorLevel%==2 GoTo :Exit
    
    Rem --------------------Open Firewall-------------------
    Start wf.msc
    
    :Exit
    (GoTo) 2>Nul & Del %0