Jump to content

Delete lines from the hosts file via .bat


NaJeEb303

Recommended Posts

HelL0 guyz... neEd sum HeLp in batch..

 

I hav a batch script that edits my hosts file, i use it to block facebook on my pc temporarily, becuz i want no one to use facebook on my pc except me..  happy.png , so when i'm done on my pc, i just run the batch script and facebook get blocked on my pc..

and also i use it for some other purpose..

 

Here is my batch script:

Quote

 

@ECHO OFF

 
SET NEWLINE=^& echo.
 
FIND /C /I "www.facebook.com" %WINDIR%\system32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 0 ECHO %NEWLINE%^127.0.0.1 www.facebook.com>>%WINDIR%\system32\drivers\etc\hosts
 
FIND /C /I "http://www.facebook.com/" %WINDIR%\system32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 0 ECHO ^127.0.0.1http://www.facebook.com/>>%WINDIR%\system32\drivers\etc\hosts
 
FIND /C /I "https://www.facebook.com/" %WINDIR%\system32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 0 ECHO ^127.0.0.1https://www.facebook.com/>>%WINDIR%\system32\drivers\etc\hosts
 
ipconfig /flushdns
 
CLS
 
EXIT
 
So its just easy to edit my hosts file via this batch script, but its very annoying to revert the change, becuz to get facebook back, i hav to delete these lines from the hosts file, and i do it manually..
So my question is;
Is there anyway to delet specific lines from the hosts file with a single batch file like this one that add lines..?
I think the above script needs a bit change but i dont know how.., all i know is that the above script first scans the hosts file for the specific line, if found then it ignores and proceed to next cammand, if not found then it adds the line.., there is the FIND command,, so i think after FIND command we can add a command something like DEL, so that it wil delete the line instead of adding it..
but i cant do it, as i d0nt hav much experience in CMD..
and plz do note that i hav anothr batch script that can delete the specific lines but it requires another .txt file which contains the lines.. and i want a single standalone .bat file like the one above that adds the line..
Any help will be appreciated..
And sorry if found my english bad....
Thanx   smile.png

Regards...

Link to comment
Share on other sites

I'm not an expert to scripting, just give you a hint.

 

Make a copy as .bak of the file, edit the file, do your thing than when you are done restore the file from the .bak.

thanx 4 suggesion.., already thought about that,, but i want an automate process...

Link to comment
Share on other sites

I would create two hosts files (one with the facebook block and the other without)

Put those two files anywhere you want and create a batch copy script where you would overwrite hosts file with your own each time you need to do that.

Link to comment
Share on other sites

I was going to try and make a script for you, but NIM's suggestion is actually the easiest way to do what you want.  That is because unlike batch's ability to easily add a line to a text file if needed, it can't easily delete a line.  What you end up having to do is copy the hosts file, leaving out the line(s) you want to "delete", delete the original hosts file, and rename the copied file to the original hosts file's name.  So just having two copies of the hosts file and copying, or renaming the one you want to use to the hosts file, whichever way you want to do it, ends up being an easier way to do it.

 

Cheers and Regards

Link to comment
Share on other sites

I would create two hosts files (one with the facebook block and the other without)

Put those two files anywhere you want and create a batch copy script where you would overwrite hosts file with your own each time you need to do that.

 

I was going to try and make a script for you, but NIM's suggestion is actually the easiest way to do what you want.  That is because unlike batch's ability to easily add a line to a text file if needed, it can't easily delete a line.  What you end up having to do is copy the hosts file, leaving out the line(s) you want to "delete", delete the original hosts file, and rename the copied file to the original hosts file's name.  So just having two copies of the hosts file and copying, or renaming the one you want to use to the hosts file, whichever way you want to do it, ends up being an easier way to do it.

 

Cheers and Regards

still not what i want.., it will also restore my hosts file to its default,, so all the other sites that i have blocked will also become accessable.., e.g i hav blocked total uninstall for calling home, so my key doesn't get blacklisted.., and also i want the script to be universal...! btw thanx

@bphlpt .. still if u can.., try once for me.., i shall be thankful...

Edited by NaJeEb303
Link to comment
Share on other sites

still not what i want.., it will also restore my hosts file to its default,, so all the other sites that i have blocked will also become accessable.., e.g i hav blocked total uninstall for calling home, so my key doesn't get blacklisted.., and also i want the script to be universal...!

 

I'm confused. Why would it "restore your hosts file to it's default"?  If you make two copies of your hosts file with all the changes you want to make in it, ie including any other block you need, the only difference in the two copies you make are that one has the facebook block and the other does not, how can overwriting your hosts file with one of those copies possibly "restore your hosts file to it's default"?  It's impossible.  If this procedure will not work for you, then any script I could make would fail as well.

 

PLEASE NOTE:  No discussion or direct mention of warez of any kind, including methods to bypass valid protection methods that some software uses regarding software keys will be tolerated anywhere on this forum.  Your comments regarding trying to keep a key from being blacklisted implies that it is not a validly obtained key.  Do not mention it, or anything like it again.  This is your only warning.

 

Cheers and Regards

Link to comment
Share on other sites

  • 7 months later...

just run the file as is. if the three lines are in the host file they will be removed, and if they are not, they will be added. how to add the more peculiar total uninstall part of your request i'm sure you can figure out yourself.
 

@echo offset "string=www.facebook.com"set "host=C:\Windows\System32\drivers\etc\hosts"for /f "delims=" %%h in ('type "%host%" ^| find /c /i "%string%"') do (	set "result=%%h" >nul	)if "%result%"=="0" (	>>%host% echo 127.0.0.1 www.facebook.com	>>%host% echo 127.0.0.1 http://www.facebook.com/	>>%host% echo 127.0.0.1 https://www.facebook.com/	set "msg=%string% successfully added to host file	)if "%result%" geq "1" (	for /f "tokens=*" %%a in ('findstr /in ".*" "%host%"') do (		echo %%a>>"%tmp%\l_1.x"		)	for /f "eol=# tokens=1,2 delims=:" %%a in ('findstr /i ".*%string%.*" "%tmp%\l_1.x"') do (		set stringlines=%%a		)	setlocal enabledelayedexpansion	set /a "beforelines=stringlines-3"	set /a "afterlines=stringlines+1"	for /f "eol=# tokens=2* delims=:" %%a in ('findstr /in ".*" "%tmp%\l_1.x"') do (		if %%a leq !beforelines! echo %%b>>"%tmp%\l_2.x"		if %%a geq !afterlines! echo %%b>>"%tmp%\l_2.x"		)	endlocal	move /y "%tmp%\l_2.x" %host% >nul	del /q /f %tmp%\*.x	set "msg=%string% successfully removed from host file	)call ipconfig /flushdns >nulecho.echo %msg%timeout 2 /nobreak >nul | exit

limitations:

the script does not handle empty newlines (carriage returns). they would be typed as the ECHO status in the new host file. all lines you want it to remove must be in sequence or all lines between the first and last occurrence of the SET "string" value will be removed.

i'm sure there is others limitations, just be careful if you edit the script. test it on some random text file or so..

Link to comment
Share on other sites

  • 6 months later...

just run the file as is. if the three lines are in the host file they will be removed, and if they are not, they will be added. how to add the more peculiar total uninstall part of your request i'm sure you can figure out yourself.

 

@echo offset "string=www.facebook.com"set "host=C:\Windows\System32\drivers\etc\hosts"for /f "delims=" %%h in ('type "%host%" ^| find /c /i "%string%"') do (	set "result=%%h" >nul	)if "%result%"=="0" (	>>%host% echo 127.0.0.1 www.facebook.com	>>%host% echo 127.0.0.1 http://www.facebook.com/	>>%host% echo 127.0.0.1 https://www.facebook.com/	set "msg=%string% successfully added to host file	)if "%result%" geq "1" (	for /f "tokens=*" %%a in ('findstr /in ".*" "%host%"') do (		echo %%a>>"%tmp%\l_1.x"		)	for /f "eol=# tokens=1,2 delims=:" %%a in ('findstr /i ".*%string%.*" "%tmp%\l_1.x"') do (		set stringlines=%%a		)	setlocal enabledelayedexpansion	set /a "beforelines=stringlines-3"	set /a "afterlines=stringlines+1"	for /f "eol=# tokens=2* delims=:" %%a in ('findstr /in ".*" "%tmp%\l_1.x"') do (		if %%a leq !beforelines! echo %%b>>"%tmp%\l_2.x"		if %%a geq !afterlines! echo %%b>>"%tmp%\l_2.x"		)	endlocal	move /y "%tmp%\l_2.x" %host% >nul	del /q /f %tmp%\*.x	set "msg=%string% successfully removed from host file	)call ipconfig /flushdns >nulecho.echo %msg%timeout 2 /nobreak >nul | exit

limitations:

the script does not handle empty newlines (carriage returns). they would be typed as the ECHO status in the new host file. all lines you want it to remove must be in sequence or all lines between the first and last occurrence of the SET "string" value will be removed.

i'm sure there is others limitations, just be careful if you edit the script. test it on some random text file or so..

 

thank u so much for the new script profess0r..

i didn't understand the limitation "or all lines between the first and last occurrence of the SET "string" value will be removed." , can  explain further plz., with example, cuz i m really a noob  :D ... 

ThanX

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...