Josh
Josh

Reputation: 366

Batch Program Problem

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%\desktop\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

Upvotes: 0

Views: 477

Answers (1)

Arun
Arun

Reputation: 2523

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

Upvotes: 3

Related Questions