Jump to content

HOSTS file modification


CodeMe

Recommended Posts

Do you specifically need the user to say "yes" ? Because if not, then it'd be simple to directly add what you want, into the host file.

But if you do want user to say yes, this will do the task you want, copy/paste into notepad, and save as .BAT:

@echo off
TITLE Modifying your HOSTS file
COLOR 85
ECHO.


:LOOP
SET Choice=
SET /P Choice="Do you want to modify HOSTS file ? (Y/N)"

IF NOT '%Choice%'=='' SET Choice=%Choice:~0,1%

ECHO.
IF /I '%Choice%'=='Y' GOTO ACCEPTED
IF /I '%Choice%'=='N' GOTO REJECTED
ECHO Please type Y (for Yes) or N (for No) to proceed!
ECHO.
GOTO Loop


:REJECTED
ECHO Your HOSTS file was left unchanged >> %systemroot%\Temp\yourinstall.log
ECHO Finished.
GOTO END

:ACCEPTED
ECHO Carrying out requested modifications to your HOSTS file
echo 127.0.0.1 local host >> %WinDir%\system32\drivers\etc\hosts
ECHO Finished.

GOTO END

:END
ECHO.
EXIT

Its quite self-explanatory. While the rest of the command-line script will do your yes/no part, the below line is what does the actual modification.

echo 127.0.0.1   local host >> %WinDir%\system32\drivers\etc\hosts

That is, you can "echo" whatever line you want into the HOSTS file, using the double-forward symbol. The location of the hosts file above, is given for win2k/XP/2k3. For win95/98/ME, its located at a different place.

Hi there this script you did... could it be modded to Delete lines from the HOST FILE Normally it's just one of these lines. But i need it to look for all 3


# 127.0.0.1 www.blah blah blah.org
# 127.0.0.1 blah blah blah.org
# 127.0.0.1 update blah blah blah.org

And Delete all 3 And Nothing else or just one of the lines Normally the top Line..

It's a Game Launcher & if any other Launchers are used this new Launcher will Give an error " Cannot connect to remote server " Until the one or the 3 lines are deleted..

I thank you in advance to anyone that can help. Am new to writing Scripts & ,bat files or exe

P.S I use Windows 8 Pro .. but it never give me the option when i joined :)

Do i have to add


DELHOST # 127.0.0.1 www.blah blah blah.org
DELHOST # 127.0.0.1 blah blah blah.org
DELHOST # 127.0.0.1 update blah blah blah.org[

In Some place in the bat file i make.. please can some one help. i'm so new at bat & exe files

you can still see the puppy fat round my eyes. lol

We can't play the game with out manually editing the host a few times a day.. & we wish to just run a bat script to do it for us to save time..

Ok not sure were i'm going wrong but please can some one help me..

I modded the script above but it still don't delete the lines...

i can make the script add the lines but not delete them :(

here is how i modded it

@echo off
TITLE Modifying your HOSTS file
COLOR 85
ECHO.


:LOOP
SET Choice=
SET /P Choice="Do you want to modify HOSTS file ? (Y/N)"

IF NOT '%Choice%'=='' SET Choice=%Choice:~0,1%

ECHO.
IF /I '%Choice%'=='Y' GOTO ACCEPTED
IF /I '%Choice%'=='N' GOTO REJECTED
ECHO Please type Y (for Yes) or N (for No) to proceed!
ECHO.
GOTO Loop


:REJECTED
ECHO Your HOSTS file was left unchanged >> %systemroot%\Temp\yourinstall.log
ECHO Finished.
GOTO END

:ACCEPTED ( HERE BELOW IS WHAT I ADDED )
ECHO Carrying out requested modifications to your HOSTS file
delhost 127.0.0.1 www.etcetc.org >> %WinDir%\system32\drivers\etc\hosts << I ADDED THIS LINE
delhost 127.0.0.1 etc etc.org >> %WinDir%\system32\drivers\etc\hosts << I ADDED THIS LINE
delhost 127.0.0.1 update.etcetc.org >> %WinDir%\system32\drivers\etc\hosts << I ADDED THIS LINE
ECHO Finished.

GOTO END

:END
ECHO.
EXIT

thank you so much people in advance

Please i'm keen to learn what i can. i know it's a simple line that should be able to be goggled.. but i always come up with power shell & Linux scripts

or scripts to add lines to hosts

Also most people scream on about why there not using a DNS server.. i don't get it, why can't they just answer the question for these people instead of telling them how to run there systems lol

Anyway Thank you ....:)

Merry Christmas everyone also.. i wish you all the very best.. Gary (From Blackburn)

Edited by Yzöwl
Link to comment
Share on other sites


Maybe you are focusing to the specifics and not to the generic.

What you want to do, which is "How do I remove lines from a HOST file?" is a sub-set of the generic "How do I remove lines from a plain text file?".

The latter has TWO different answers:

  1. there is NO provision without using third party tools to delete lines from a plain text file
  2. what you can do instead is to re-write the text file omitting the lines that you do not want

Typically this can be done using a FOR loop and either the FIND or FINDSTR commands.


FOR /F "tokens=* delims=" %%A IN ('type "%WinDir%\system32\drivers\etc\hosts"') DO ECHO %%A
FOR /F "tokens=* delims=" %%A IN ('type "%WinDir%\system32\drivers\etc\hosts"^|FIND /V "www.etcetc.org"') DO ECHO %%A>>"%WinDir%\system32\drivers\etc\hosts1"
FOR /F "tokens=* delims=" %%A IN ('type "%WinDir%\system32\drivers\etc\hosts1"^|FIND /V "etc etc.org"') DO ECHO %%A>>"%WinDir%\system32\drivers\etc\hosts2"
FOR /F "tokens=* delims=" %%A IN ('type "%WinDir%\system32\drivers\etc\hosts2"^|FIND /V "update.etcetc.org"') DO ECHO %%A>>"%WinDir%\system32\drivers\etc\hosts3"
NOTEPAD "%WinDir%\system32\drivers\etc\hosts3"

Within limits, you can use more FIND /V on the same command in the FOR loop, if all you have to do is to remove three lines, you can do it in one command.

then, once you are satisfied with the result, you delete the original file and rename the temp file, like:

del "%WinDir%\system32\drivers\etc\hosts"
ren "%WinDir%\system32\drivers\etc\hosts3" hosts

Take some time on:

http://www.robvanderwoude.com/ntfor.php

http://www.robvanderwoude.com/ntfortokens.php

http://www.robvanderwoude.com/find.php

http://www.robvanderwoude.com/findstr.php

What I don't understand is if these lines (site addresses) are always three, are always the same three, etc. etc. or if you need to call the command with a parameter (the line/site address to be deleted).

jaclaz

Link to comment
Share on other sites

I'm curious where the OP got the command "delhost 127.0.0.1 ....."? I've never heard of the batch command "delhost".

The method that jaclaz detailed above should work fine and offer any needed flexibility.

Cheers and Regards

Link to comment
Share on other sites

I'm curious where the OP got the command "delhost 127.0.0.1 ....."? I've never heard of the batch command "delhost".

It seems to me like the result of the usual "infinite monkeys approach" that most people new to scripting tend to use initially :whistle: .

http://en.wikipedia.org/wiki/Infinite_monkey_theorem

obviously the probabilities to get a correct command line are much higher than re-writing all of Shakespeare's plays ;), but still they are infinitely low :ph34r: .

jaclaz

Link to comment
Share on other sites

Maybe you are focusing to the specifics and not to the generic.

What you want to do, which is "How do I remove lines from a HOST file?" is a sub-set of the generic "How do I remove lines from a plain text file?".

The latter has TWO different answers:

  1. there is NO provision without using third party tools to delete lines from a plain text file
  2. what you can do instead is to re-write the text file omitting the lines that you do not want

Typically this can be done using a FOR loop and either the FIND or FINDSTR commands.


FOR /F "tokens=* delims=" %%A IN ('type "%WinDir%\system32\drivers\etc\hosts"') DO ECHO %%A
FOR /F "tokens=* delims=" %%A IN ('type "%WinDir%\system32\drivers\etc\hosts"^|FIND /V "www.etcetc.org"') DO ECHO %%A>>"%WinDir%\system32\drivers\etc\hosts1"
FOR /F "tokens=* delims=" %%A IN ('type "%WinDir%\system32\drivers\etc\hosts1"^|FIND /V "etc etc.org"') DO ECHO %%A>>"%WinDir%\system32\drivers\etc\hosts2"
FOR /F "tokens=* delims=" %%A IN ('type "%WinDir%\system32\drivers\etc\hosts2"^|FIND /V "update.etcetc.org"') DO ECHO %%A>>"%WinDir%\system32\drivers\etc\hosts3"
NOTEPAD "%WinDir%\system32\drivers\etc\hosts3"

Within limits, you can use more FIND /V on the same command in the FOR loop, if all you have to do is to remove three lines, you can do it in one command.

then, once you are satisfied with the result, you delete the original file and rename the temp file, like:

del "%WinDir%\system32\drivers\etc\hosts"
ren "%WinDir%\system32\drivers\etc\hosts3" hosts

Take some time on:

http://www.robvander...e.com/ntfor.php

http://www.robvander...ntfortokens.php

http://www.robvanderwoude.com/find.php

http://www.robvander...com/findstr.php

What I don't understand is if these lines (site addresses) are always three, are always the same three, etc. etc. or if you need to call the command with a parameter (the line/site address to be deleted).

jaclaz

@jaclaz

ohmy.gif

Not that i don't appreciate your help & time jaclaz But you have just totally confused me to the max now.

I'm NEW to making bat files & you have gone on like i understand what i'm doing lolwhistling.gif

I am also Have dyslexia along with ADHD trying to concentrate on what your trying to tell me as made this project sound even harder for me now sad.gif

is there some please that can help spell it out for me & put it in layman's terms for me please rolleyes.gif

I'm not stupid or thick just always Have always been one of them people that as to have something shown to them.

i was the same at school i had to have separate teacher.. anyway this is a little personal & Hard for me to tell people on a forum.

so i hope you understand & someone can help me.

thank you again

Gary

Link to comment
Share on other sites

Let's make a simpler example.

Let's say that you have a directory C:\tests and in this directory you have a text file c:\tests\mytest.txt with these contents (copy and paste in a new notepad window and save as c:\tests\test.txt:

Line #1: This is first line
Line #2: this is second line
Line #3: this is third line

And let's say that you want to remove the second line, i.e. your expected result is:

Line #1: This is first line
Line #3: this is third line

There is NO command built in in the batch language NOR any tool shipped with any DOS/Windows release capable of removing a line from a text file.

Is this clear enough till now? :unsure:

There is a workaround that is to read the WHOLE contents of the file and re-writing a new file filtering out the unwanted line.

Open a command prompt window and in it type )or copy/paste):

TYPE C:\tests\mytest.txt

[ENTER]

You will see the three lines.

now try:

TYPE C:\tests\mytest.txt|FIND /V "#2"

[ENTER]

You will see your desired result, the line #2 is filtered out by the FIND /V command.

now try:

TYPE C:\tests\mytest.txt|FIND /V "#2">C:\tests\mynewtest.txt

[ENTER]

Open in Explorer the folder C:\tests\ and you will find in it a file named mynewtest.txt with the same output you before saw in the command window (i.e. the desired result).

What you have done was to "pipe" (or redirect) the output of the command TYPE to the input of FIND command and later redirected this latter's standard output to a file.

Is this clear enough? :unsure:

Besides your "dyslexia along with ADHD" troubles, you need ANYWAY to read and study the batch commands syntax and understand it if you want to write a batch file.

The given site:

http://www.robvanderwoude.com/

http://www.robvanderwoude.com/batchfiles.php

contains both "basic" syntax and "advanced" one explanations, examples and what not.

Start reading, at the light of this post, these:

http://www.robvanderwoude.com/type.php

http://www.robvanderwoude.com/find.php

http://www.robvanderwoude.com/redirection.php

jaclaz

Link to comment
Share on other sites

Let's make a simpler example.

Let's say that you have a directory C:\tests and in this directory you have a text file c:\tests\mytest.txt with these contents (copy and paste in a new notepad window and save as c:\tests\test.txt:

Line #1: This is first line
Line #2: this is second line
Line #3: this is third line

And let's say that you want to remove the second line, i.e. your expected result is:

Line #1: This is first line
Line #3: this is third line

There is NO command built in in the batch language NOR any tool shipped with any DOS/Windows release capable of removing a line from a text file.

Is this clear enough till now? :unsure:

There is a workaround that is to read the WHOLE contents of the file and re-writing a new file filtering out the unwanted line.

Open a command prompt window and in it type )or copy/paste):

TYPE C:\tests\mytest.txt

[ENTER]

You will see the three lines.

now try:

TYPE C:\tests\mytest.txt|FIND /V "#2"

[ENTER]

You will see your desired result, the line #2 is filtered out by the FIND /V command.

now try:

TYPE C:\tests\mytest.txt|FIND /V "#2">C:\tests\mynewtest.txt

[ENTER]

Open in Explorer the folder C:\tests\ and you will find in it a file named mynewtest.txt with the same output you before saw in the command window (i.e. the desired result).

What you have done was to "pipe" (or redirect) the output of the command TYPE to the input of FIND command and later redirected this latter's standard output to a file.

Is this clear enough? :unsure:

Besides your "dyslexia along with ADHD" troubles, you need ANYWAY to read and study the batch commands syntax and understand it if you want to write a batch file.

The given site:

http://www.robvanderwoude.com/

http://www.robvander.../batchfiles.php

contains both "basic" syntax and "advanced" one explanations, examples and what not.

Start reading, at the light of this post, these:

http://www.robvanderwoude.com/type.php

http://www.robvanderwoude.com/find.php

http://www.robvander...redirection.php

jaclaz

What I don't understand is if these lines (site addresses) are always three, are always the same three, etc. etc. or if you need to call the command with a parameter (the line/site address to be deleted).

this is going to sound funny. but i have made a game launcher ( But i can't make a simple cmd to delete 3 lines from a host file (hahahah )

for a game & others have made there own launcher also. BUT they have made it so it adds the lines so the End USER can't use anyone's else's but there's

so when people try mine they get an error " CANNOT CONNECT TO REMOTE SERVER "

So i have to tell them to Edit these 3 lines out of there HOST file.

NOW the problem is NOT everybody know where or how what & Were a HOST file is, never mind how to edit one lol

so i'm trying to make it simple for my user's. when they download my tool, there is a bat file or script

that if they get an error message they run this script & the work is done for them.

saving them the hassle & messing up there own computers..

i got it to work, but it adds 3 hosts files to the directory 1,2,3

i don't think this is the best way for simple folk to use that do not know how to edit hosts

just trying what i can to help my END USER.

P.S

i thought something so simple would of been easy.

yet i can use ArtMoney to edit my game & find addresses etc

@@@@@@@@@@@@@@@@@

Again i thank you for the links to read, this will be hard, i have always learnt via video or being shown the end result..

i know i have to over come these problems & trust me i do try.

Thank you

Link to comment
Share on other sites

Again i thank you for the links to read, this will be hard, i have always learnt via video or being shown the end result..

In my experience I have seen people watching videos and people looking at end results, but noone ever learning or understanding anything from them (or from them alone, i.e. without additionally reading, studying, experimenting)

You can replicate the examples shown in a video alright, and a video can well be a support for learning :thumbup , but understanding principles or learning a computer language syntax from a video? :unsure:

jaclaz

Link to comment
Share on other sites

Again i thank you for the links to read, this will be hard, i have always learnt via video or being shown the end result..

In my experience I have seen people watching videos and people looking at end results, but noone ever learning or understanding anything from them (or from them alone, i.e. without additionally reading, studying, experimenting)

You can replicate the examples shown in a video alright, and a video can well be a support for learning :thumbup , but understanding principles or learning a computer language syntax from a video? :unsure:

jaclaz

@jaclaz

Dude that to me is a little insensitive & Insulting. How dare you presume what i do after watching a video.

I can & Do Build computer's & repair them for a living & have my own store. & very successful store to.

I Can MOD a BIOS for my ASUS Mobo & tweak it to work better & i can repair the Mobos them self's.

All this was done by watching video's & Experimenting. this is the best way for ME to learn.

Not every one is wired the same way as every one else jaclaz , we all learn & think in our own ways.

I Can use artmoney to locate addresses in programs & TrueBug.PHP.Obfuscator as well as OLLYDBG all done by video & self learnt.

and asking questions. BUT sometimes people think we all think on the same level on these forums & we don't Sir

we get to were we are by asking questions & sometime weather you like it or not. you will have to paraphrase your reply's so for them that don't know

are able to learn & pick it up easy..

Again i thank you for you help, but it's no use leaving 4 host files in the directory this will only confuse people even more lol

i'm not having a go at you, am simply standing up for my self... Simple :)

Link to comment
Share on other sites

Again i thank you for the links to read, this will be hard, i have always learnt via video or being shown the end result..

In my experience I have seen people watching videos and people looking at end results, but noone ever learning or understanding anything from them (or from them alone, i.e. without additionally reading, studying, experimenting)

You can replicate the examples shown in a video alright, and a video can well be a support for learning :thumbup , but understanding principles or learning a computer language syntax from a video? :unsure:

jaclaz

@jaclaz

Dude that to me is a little insensitive & Insulting. How dare you presume what i do after watching a video.

I can & Do Build computer's & repair them for a living & have my own store. & very successful store to.

I Can MOD a BIOS for my ASUS Mobo & tweak it to work better & i can repair the Mobos them self's.

All this was done by watching video's & Experimenting. this is the best way for ME to learn.

Not every one is wired the same way as every one else jaclaz , we all learn & think in our own ways. [...]

dubbio.gif

Link to comment
Share on other sites

but it's no use leaving 4 host files in the directory this will only confuse people even more lol

then, once you are satisfied with the result, you delete the original file and rename the temp file, like:

del "%WinDir%\system32\drivers\etc\hosts"
ren "%WinDir%\system32\drivers\etc\hosts3" hosts

jaclaz

Link to comment
Share on other sites

@CodeMe

If you want a specific solution to 'learn from' then provide a specific question!

(I can only provide a solution if you can provide me an exact character by character copy of the entries you need to modify/delete)

The three lines you've provided us with are already commented out meaning that they are not read when the file is parsed and therefore do not need deleting!

Are you suggesting that another game developer has written HOSTS entries which effectively block your domain!

Link to comment
Share on other sites

@CodeMe

I think that jaclaz has been very patient with you and given you all the tools you need, especially in this post:

Maybe you are focusing to the specifics and not to the generic.

What you want to do, which is "How do I remove lines from a HOST file?" is a sub-set of the generic "How do I remove lines from a plain text file?".

The latter has TWO different answers:

  1. there is NO provision without using third party tools to delete lines from a plain text file
  2. what you can do instead is to re-write the text file omitting the lines that you do not want

Typically this can be done using a FOR loop and either the FIND or FINDSTR commands.


FOR /F "tokens=* delims=" %%A IN ('type "%WinDir%\system32\drivers\etc\hosts"') DO ECHO %%A
FOR /F "tokens=* delims=" %%A IN ('type "%WinDir%\system32\drivers\etc\hosts"^|FIND /V "www.etcetc.org"') DO ECHO %%A>>"%WinDir%\system32\drivers\etc\hosts1"
FOR /F "tokens=* delims=" %%A IN ('type "%WinDir%\system32\drivers\etc\hosts1"^|FIND /V "etc etc.org"') DO ECHO %%A>>"%WinDir%\system32\drivers\etc\hosts2"
FOR /F "tokens=* delims=" %%A IN ('type "%WinDir%\system32\drivers\etc\hosts2"^|FIND /V "update.etcetc.org"') DO ECHO %%A>>"%WinDir%\system32\drivers\etc\hosts3"
NOTEPAD "%WinDir%\system32\drivers\etc\hosts3"

Within limits, you can use more FIND /V on the same command in the FOR loop, if all you have to do is to remove three lines, you can do it in one command.

then, once you are satisfied with the result, you delete the original file and rename the temp file, like:

del "%WinDir%\system32\drivers\etc\hosts"
ren "%WinDir%\system32\drivers\etc\hosts3" hosts

Take some time on:

http://www.robvanderwoude.com/ntfor.php'>http://www.robvanderwoude.com/ntfor.php

http://www.robvanderwoude.com/ntfortokens.php'>http://www.robvanderwoude.com/ntfortokens.php

http://www.robvanderwoude.com/find.php'>http://www.robvanderwoude.com/find.php

http://www.robvanderwoude.com/findstr.php'>http://www.robvanderwoude.com/findstr.php

What I don't understand is if these lines (site addresses) are always three, are always the same three, etc. etc. or if you need to call the command with a parameter (the line/site address to be deleted).

jaclaz

He then went above and beyond when, at your request, he tried to put it in "layman's terms" and gave you even simpler examples to practice with:

Let's make a simpler example.

Let's say that you have a directory C:\tests and in this directory you have a text file c:\tests\mytest.txt with these contents (copy and paste in a new notepad window and save as c:\tests\test.txt:

Line #1: This is first line
Line #2: this is second line
Line #3: this is third line

And let's say that you want to remove the second line, i.e. your expected result is:

Line #1: This is first line
Line #3: this is third line

There is NO command built in in the batch language NOR any tool shipped with any DOS/Windows release capable of removing a line from a text file.

Is this clear enough till now? :unsure:

There is a workaround that is to read the WHOLE contents of the file and re-writing a new file filtering out the unwanted line.

Open a command prompt window and in it type )or copy/paste):

TYPE C:\tests\mytest.txt

[ENTER]

You will see the three lines.

now try:

TYPE C:\tests\mytest.txt|FIND /V "#2"

[ENTER]

You will see your desired result, the line #2 is filtered out by the FIND /V command.

now try:

TYPE C:\tests\mytest.txt|FIND /V "#2">C:\tests\mynewtest.txt

[ENTER]

Open in Explorer the folder C:\tests\ and you will find in it a file named mynewtest.txt with the same output you before saw in the command window (i.e. the desired result).

What you have done was to "pipe" (or redirect) the output of the command TYPE to the input of FIND command and later redirected this latter's standard output to a file.

Is this clear enough? :unsure:

Besides your "dyslexia along with ADHD" troubles, you need ANYWAY to read and study the batch commands syntax and understand it if you want to write a batch file.

The given site:

http://www.robvanderwoude.com/

http://www.robvanderwoude.com/batchfiles.php

contains both "basic" syntax and "advanced" one explanations, examples and what not.

Start reading, at the light of this post, these:

http://www.robvanderwoude.com/type.php

http://www.robvanderwoude.com/find.php'>http://www.robvanderwoude.com/find.php

http://www.robvanderwoude.com/redirection.php

jaclaz

The only evidence you gave that you attempted to experiment with any of the above was when you complained:

i got it to work, but it adds 3 hosts files to the directory 1,2,3

The "extra" copies of the hosts file are created primarily for your benefit to help you understand what is happening in the various parts of the process, they are not meant to be kept in your final code. After you have gone through the steps as outlined and verified that the "hosts3" file has successfully accomplished your goal of deleting the 3 offending lines, then you can compress and clean up the code into a form suitable for distribution to your users. Assuming the above code is successful, the resulting code could look something like this:

@echo off
FOR /F "tokens=* delims=" %%A IN ('type "%WinDir%\system32\drivers\etc\hosts"^|FIND /V "www.etcetc.org"') DO ECHO %%A>>"%WinDir%\system32\drivers\etc\hosts1"
FOR /F "tokens=* delims=" %%A IN ('type "%WinDir%\system32\drivers\etc\hosts1"^|FIND /V "etc etc.org"') DO ECHO %%A>>"%WinDir%\system32\drivers\etc\hosts2"
FOR /F "tokens=* delims=" %%A IN ('type "%WinDir%\system32\drivers\etc\hosts2"^|FIND /V "update.etcetc.org"') DO ECHO %%A>>"%WinDir%\system32\drivers\etc\hosts3"
del "%WinDir%\system32\drivers\etc\hosts"
del "%WinDir%\system32\drivers\etc\hosts1"
del "%WinDir%\system32\drivers\etc\hosts2"
ren "%WinDir%\system32\drivers\etc\hosts3" hosts
EXIT

So you are left with the one single hosts file. I assume that this is what you were looking for?

Cheers and Regards

Link to comment
Share on other sites

@CodeMe

A simpler alternative workaround is just to provide instruction to your user to manual check the hosts (file) setting.

Ask user to copy and paste the below code in Start-->Run box and press enter to open hosts (file) in notepad.

notepad %systemroot%\system32\drivers\etc\hosts

Ask user to manual disable the 3 sites (if exists) by inserting # at the begining of the line. (since # is use for comment)

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...