开发者

Batch Command Conflict

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)


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.


I have doubts about an IF that you have in the script which seems to be used extensively:

if %mm% gtr 60...

If the idea is to check for minutes greater than 60, I guess this may be the cause of your issues. A minute value can be 0 to 59 never 60.


I don't think it'll fix things, but it might make it easier to read/manage if you didn't create the secondary file, just used the original with arguments to indicate a removal.

For instance (untested, ballparking):

@echo off
TITLE Site Blocker
if .%1.==.REMOVE. goto :remove
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
:remove
  set URL=%2
  :: do removal stuff
  goto :eof
:SET
AT %hh%:%mm% thisbatchfile.bat REMOVE %URL%

What are you using to compile it to an .EXE?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜