i have this: set myvar = ^J | FIND /N /I "texttolookfor" %WINDIR%\system32\drivers\etc\hosts now i have the lines it fined the text in

how can i delete these line from the file?

i know i can run over the lines with FOR but the delete part is still tricky for me

i read about findstr, it suppose to be better but can't find anything to delete with that as well

now i read about

FIND /V "texttolookfor" %WINDIR%\system32\drivers\etc\hosts > newfile

it saves the file to a new one without the found string... but adds


at the begining but when i try

FIND /V "texttolookfor" %WINDIR%\system32\drivers\etc\hosts > %WINDIR%\system32\drivers\etc\hosts

it does not work...

  • msdos6.22, but also %WINDIR%\System32? I'm confused? Are you editing a windows file on an MS Dos 6.22 system, or is the msdos6.22 tag a mistake? – Bryan Dec 10 '12 at 20:04
  • it's within windows... i will fix the tag now – Y.G.J Dec 10 '12 at 20:12
  • I feel like you're going about this the wrong way, but it's sort of confusing to figure out what you're actually trying to do. Instead of asking for help on this one specific piece of your problem, why don't you explain your whole problem. I'm betting that there's probably a much easier solution. – MDMarra Dec 10 '12 at 20:21

Your command:

FIND /V "texttolookfor" %WINDIR%\system32\drivers\etc\hosts > newfile

adds ------------------%WINDIR%\system32\drivers\etc\hosts to the top of the file as that's part of the default output of FIND. Try it without the redirection and see.

FIND /V "texttolookfor" %WINDIR%\system32\drivers\etc\hosts

# Copyright (c) 1993-2009 Microsoft Corp.
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.

TYPE can be used to cat the file, redirect its output through FIND, and it should not display the file/path:

TYPE %WINDIR%\system32\drivers\etc\hosts | FIND /V "texttolookfor"

This can, of course, be redirected to a new file as well:

TYPE %WINDIR%\system32\drivers\etc\hosts | FIND /V "texttolookfor" > newfile
  • Oblig. findstr example should be findstr /v /i /c:"texttolookfor" %WINDIR%\system32\drivers\etc\hosts > %TEMP%\hosts.tmp && copy /y %TEMP%\hosts.tmp %WINDIR%\system32\drivers\etc\hosts. Expect admin perms to be a headache on WinV/7/2008/R2, though. – jimbobmcgee Dec 10 '12 at 21:38

Try the Win32 port of Grep

Assuming you want to purge any lines containing xyz the following command will do what you need.

grep -v xyz inputfile.txt > outputfile.txt

If you want to match an entire line, this is possible too using a regular expression. As MDMarra has already said in the comments, please explain the bigger picture of what you are trying to do.

p.s. Piping the output back over the original file, probably isn't a good thing to do, especially when experimenting, and even more especially when it's a system file.

  • i know overwriting is not good... but in this case it's a must. any way i can make a backup at c:\ but the export file gets first line of the path and i don't get why – Y.G.J Dec 10 '12 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.