批处理命令冲突 [英] Batch Command Conflict

查看:81
本文介绍了批处理命令冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了此批处理脚本,该脚本将允许用户输入网站的URL以及以分钟为单位的时间,然后将URL添加到主机文件中,并在时间到期后将其删除. (有效地阻止了一定时间的网站)

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)

它会在首次运行时通过创建另一个批处理文件从主机文件中删除网站,然后在指定的时间使用AT命令启动新的批处理文件.它在阻止多个网站时起作用,唯一的问题是,如果同时设置了多个网站来取消阻止,则创建的每个新批处理文件的取消阻止过程似乎会发生冲突.如您所见,我试图通过将批处理文件的运行推迟到另一个文件完成之前来解决此问题.不幸的是,它在大多数情况下都不起作用.有时,如果时机幸运,那么两个批处理文件将在不互相干扰的情况下执行.这是代码,为我糟糕的编码技术感到抱歉,这似乎很难理解:

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

(该程序必须以管理员身份运行才能工作.因此,我将其编译为.exe并添加了管理员清单.尽管由于运行批处理或已编译时遇到相同的问题,但编译为.exe也不是问题. exe)

(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

还...我使用了 schtasks 而不是 AT ,以便以后可以轻松找到并删除该任务,因为它允许您应用唯一的名称而不是从分配的ID号开始.

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.

这篇关于批处理命令冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆