开发者

Batch Program Problem

开发者 https://www.devze.com 2023-03-31 08:44 出处:网络
I am currently trying to program a batch script which allows the user to enter the name of a website, and then writes \"127.0.0.1 www.website.com\" at the bottom of the user\'s hosts file (essentially

I am currently trying to program a batch script which allows the user to enter the name of a website, and then writes "127.0.0.1 www.website.com" at the bottom of the user's hosts file (essentially blocking that website)

Everything is working except for one line. I need to write the following line into another batch file which will be created by my program:

echo find /v "%url%" < C:\WINDOWS\System32\drivers\etc\hosts > C:\Users\%username%\deskt开发者_如何学JAVAop\temp.txt >> unblock.bat

This line is part of the code which will be able to remove the website from the hosts file if the user wishes to. The problem appears to be the "<" and ">" signs. The program will not allow me to write these to the new batch file. I have tried to save them as variables and realized that the only way i could do it was by declaring them with inverted commas like this:

set char1="<"
set char2=">"

and then my command looks like this:

echo find /v "%url%" %char1% C:\WINDOWS\System32\drivers\etc\hosts %char2% C:\Users\%username%\desktop\temp.txt >> unblock.bat

The problem with this is that when writing to the new batch file, they both still have "" around them which makes the new batch file useless as the command does not execute properly.

Any ideas on how to fix this?

Here's the whole code for the batch file (incomplete):

@echo off
TITLE Site Blocker
SET /P url=Enter website (e.g. www.facebook.com)- 
echo. >> C:\WINDOWS\System32\drivers\etc\hosts
echo 127.0.0.1 %url% >> C:\WINDOWS\System32\drivers\etc\hosts
echo find /v "%url%" < C:\WINDOWS\System32\drivers\etc\hosts >  C:\Users\%username%\desktop\temp.txt >> unblock.bat
echo del C:\WINDOWS\System32\drivers\etc\hosts /Q >> unblock.bat
echo ren C:\Users\%username%\desktop\temp.txt hosts >> unblock.bat
echo copy C:\Users\%username%\desktop\hosts C:\WINDOWS\System32\drivers\etc\ >>   unblock.bat
echo del C:\Users\%username%\desktop\hosts /Q >> unblock.bat
echo msg * %url% unblocked >> unblock.bat
echo del unblock.bat >> unblock.bat
echo exit >> unblock.bat
exit


Escape the greater than and less than with a caret (^)

0

精彩评论

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