Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7614803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:29:45+00:00 2026-05-31T02:29:45+00:00

I have made this batch script which will allow the user to enter a

  • 0

I have made this batch script which will allow the user to enter a website’s URL as well as a time in minutes, it then adds the URL to the hosts file and removes it once the time expires. (Effectively blocking a website for a certain amount of time)

It removes the website from the hosts file by creating another batch file when it is first run, and then uses the AT command to launch the new batch file at the specified time. It works when blocking multiple websites, the only problem is if more then one website is set to become unblocked at the same time, the unblocking process of each new batch file which was created seems to conflict. As you will see, I have attempted to fix this by delaying a batch file from running until the other has completed. Unfortunately it does not work most of the time. Sometimes if the timing is lucky then both batch files will execute without interfering with one another. Here’s the code, sorry for my terrible coding techniques, it may seem hard to understand:

@echo off
TITLE Site Blocker
SET name=%random%
SET /P url=Enter website (e.g. www.facebook.com)- 
SET /P mins=How many minutes do you want to block it for?: 
GOTO :SET
:BACK
AT %hh%:%mm% C:\Users\%username%\downloads\%name%.bat
echo. >> C:\WINDOWS\System32\drivers\etc\hosts
echo 127.0.0.1 %url% >> C:\WINDOWS\System32\drivers\etc\hosts
echo :TOP >> C:\Users\%username%\downloads\%name%.bat
echo IF EXIST C:\Users\Downloads\temp1.txt GOTO :WAIT >> C:\Users\%username%\downloads\%name%.bat
echo echo DONT DELETE ^>^> C:\Users\Downloads\temp1.txt >> C:\Users\%username%\downloads\%name%.bat
echo find /v "%url%" ^< C:\WINDOWS\System32\drivers\etc\hosts ^> C:\Users\%username%\desktop\temp.txt >> C:\Users\%username%\downloads\%name%.bat
echo del C:\WINDOWS\System32\drivers\etc\hosts /Q >> C:\Users\%username%\downloads\%name%.bat
echo ren C:\Users\%username%\desktop\temp.txt hosts >> C:\Users\%username%\downloads\%name%.bat
echo copy C:\Users\%username%\desktop\hosts C:\WINDOWS\System32\drivers\etc\ >> C:\Users\%username%\downloads\%name%.bat
echo del C:\Users\%username%\desktop\hosts /Q >> C:\Users\%username%\downloads\%name%.bat
echo msg * %url% unblocked >> C:\Users\%username%\downloads\%name%.bat
echo del C:\Users\Downloads\temp1.txt /Q >> C:\Users\%username%\downloads\%name%.bat
echo del C:\users\%username%\downloads\%name%.bat /Q >>  C:\Users\%username%\downloads\%name%.bat
echo exit >> C:\Users\%username%\downloads\%name%.bat
echo :WAIT >> C:\Users\%username%\downloads\%name%.bat
echo timeout 3 >> C:\Users\%username%\downloads\%name%.bat
echo GOTO :TOP >> C:\Users\%username%\downloads\%name%.bat
exit
:SET
set /a mm=%time:~3,2%
set /a hh=%time:~0,2%
if %mm% gtr 60 GOTO :CHECK
set /a mm=%mm%+%mins%
:DONE
if %mm% gtr 60 GOTO :CHECK
if %mm%==60 set /a hh=%hh%+1 & set /a mm=00
GOTO :BACK
exit
:CHECK
if %mm% gtr 60 set /a hh=%hh%+1
if %mm% gtr 60 set /a mm=%mm%-60 & GOTO :DONE
GOTO :DONE

(The program must be run as administrator to work. I therefore compile it to .exe and add an administrator manifest. Compiling to .exe is not the problem though as I have the same problem when running the batch or the compiled .exe)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:29:46+00:00Added an answer on May 31, 2026 at 2:29 am

    I had a little bit of fun with this… and since you didn’t mark any answer yet I thought you might like to see it. It seems like your major problem is you want to be able to block multiple websites and have them unblock at the same time correct?

    Using your method I modified it with a dynamic looping batch that would allow you to add as many websites to temporarily ban as you want. that way only one batch is accessing the hosts file at a time. It also cleans up everything it creates before getting rid of itself. Hope this helps:

    @echo off
    TITLE Site Blocker
    setlocal EnableDelayedExpansion
    SET name=%random%
    SET /P count=How many websites do you need blocked?:
    SET /P mins=How many minutes do you want to block them for?: 
    SET N=0
    :URL_LOOP
    SET /a N+=1
    IF !N! gtr !count! GOTO SET
    SET /P url%N%=Enter website (e.g. www.facebook.com)- 
    GOTO URL_LOOP
    :BACK
    schtasks /create /sc ONCE  /tn %name% /TR C:\Users\%username%\downloads\%name%.bat /st %hh%:%mm%
    SET N=0
    :HOST_LOOP
    SET /a N+=1
    IF !N! gtr !count! GOTO END_HOST_LOOP
    echo. 127.0.0.1 !url%N%! >> C:\Windows\System32\drivers\etc\hosts
    GOTO HOST_LOOP
    :END_HOST_LOOP
    echo @echo off >> C:\Users\%username%\downloads\%name%.bat
    echo setlocal >> C:\Users\%username%\downloads\%name%.bat
    echo :TOP >> C:\Users\%username%\downloads\%name%.bat
    echo copy C:\Windows\System32\drivers\etc\hosts C:\temp1.txt >> C:\Users\%username%\downloads\%name%.bat
    SET N=0
    SET M=1
    :FIND_LOOP
    SET /a M+=1
    SET /a N+=1
    IF !N! gtr !count! GOTO END_FIND_LOOP
    echo find /v "!url%N%!" ^< C:\temp%N%.txt ^> C:\temp%M%.txt >> C:\Users\%username%\downloads\%name%.bat
    echo del C:\temp%N%.txt >> C:\Users\%username%\downloads\%name%.bat
    GOTO FIND_LOOP
    :END_FIND_LOOP
    echo xcopy /y C:\temp%N%.txt C:\Windows\System32\drivers\etc\hosts >> C:\Users\%username%\downloads\%name%.bat
    echo del C:\temp%N%.txt >> C:\Users\%username%\downloads\%name%.bat
    SET N=0
    :MSG_LOOP
    SET /a N+=1
    IF !N! gtr !count! GOTO END_MSG_LOOP
    echo msg * !url%N%! unblocked >> C:\Users\%username%\downloads\%name%.bat
    GOTO MSG_LOOP
    :END_MSG_LOOP
    echo schtasks /delete /f /tn %name% >> C:\Users\%username%\downloads\%name%.bat
    echo del C:\users\%username%\downloads\%name%.bat /Q >>  C:\Users\%username%\downloads\%name%.bat
    echo exit >> C:\Users\%username%\downloads\%name%.bat
    echo endlocal >> C:\Users\%username%\downloads\%name%.bat
    exit
    :SET
    set /a mm=%time:~3,2%
    set /a hh=%time:~0,2%
    if %mm% gtr 60 GOTO :CHECK
    set /a mm=%mm%+%mins%
    :DONE
    if %mm% gtr 60 GOTO :CHECK
    if %mm%==60 set /a hh=%hh%+1 & set /a mm=00
    GOTO :BACK
    exit
    :CHECK
    if %mm% gtr 60 set /a hh=%hh%+1
    if %mm% gtr 60 set /a mm=%mm%-60 & GOTO :DONE
    GOTO :DONE
    endlocal
    

    Also… I used schtasks instead of AT so that I could easily find the task after and delete it, since it allows you to apply unique names instead of going from an assigned ID number.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a batch script which runs a Python application. This batch script
Hi have made this function which is made to replicate an error that I
OK, I was just fooling around in my spare time and have made this
I have a system in which I allow users (admins) to request batch emails
I have made this code(it is a part of my whole script) -- if(unset($_SESSION['USER_ID']))
I have made this timer script but I can't seem to make it so
I have made some progress with the DataList and UserControl this morning but I
Hi I made this site a while ago in my table days but have
This is a C++ class that I have made with n number of pointers.
This is my first winform app in .NET... I have made a couple of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.