C
C
Cn0Hc0P2020-09-20 12:54:55
cmd/bat
Cn0Hc0P, 2020-09-20 12:54:55

How in a bat file with searching, deleting and adding a line?

Need code for bat
Search for string 127.0.0.1 youtube.com in the hosts file
If the string is found, then delete
If there is no string, then add
Ie . like a switch to turn it off youtube
Thank you all!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2020-09-20
@res2001

Prepare 2 hosts files, one contains a string, the other does not, and copy them instead of the original one.
The batch file will eventually turn out something like this:

findstr /C:"127.0.0.1 youtube.com" hosts 1>nul 2>&1 && (copy /y hosts.no hosts & exit /b)
copy /y hosts.yes hosts

hosts.yes and hosts.no are filenames where hosts.yes contains a string and hosts.no does not.
Didn't check the code.
You may need to specify the full paths to all files.
To change hosts, you need to run a batch file with admin rights.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question