CMD If find in file

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

dropdeadcriminal

Thread Starter
Joined
Nov 10, 2011
Messages
3
Hello,
My CMD line code is very rusty. I need some help assembling some pieces.
I want to search the hosts file, see if there is a line of code there, and if not add it.
I know i need to:
find /c "192.168.x.x" C:\WINDOWS\system32\drivers\etc\hosts
If the result of that is a 0 i need to:
if [%h%]==[0] echo 192.168.x.x intranet.com>>C:\WINDOWS\system32\drivers\etc\hosts
The part I cant recall is how to set the part of result of find /c "192.168.x.x" C:\WINDOWS\system32\drivers\etc\hosts with the count to %h%

Can anyone help.

Thanks
 

dropdeadcriminal

Thread Starter
Joined
Nov 10, 2011
Messages
3
never mind. I go it:

set nl=^& echo.
set chkhst=find /c "192.168.x.x" C:\WINDOWS\system32\drivers\etc\hosts
FOR /f "tokens=3 delims= " %G IN ('%chkhst% ^|find ":"') DO set ipf=%G
if [%ipf%]==[0] echo %nl%>>C:\WINDOWS\system32\drivers\etc\hosts
if [%ipf%]==[0] echo 192.168.x.x intranet.com>>C:\WINDOWS\system32\drivers\etc\hosts
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
Are you say you wanted to check if the errorlevel equals Zero which means it found what you wanted in the file.

Code:
IF %errorlevel%=="0" echo I found it
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
You can actually do what you are trying to do with a single line of code.

Code:
find "192.168.x.x" C:\WINDOWS\system32\drivers\etc\hosts ||echo 192.168.x.x intranet.com>>C:\WINDOWS\system32\drivers\etc\hosts
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
Efficient!
Do you have a trick to throw a new line in before adding the text as well?
With one line
Code:
find "192.168.x.x" C:\WINDOWS\system32\drivers\etc\hosts ||echo.>>C:\WINDOWS\system32\drivers\etc\hosts &&echo 192.168.x.x intranet.com>>C:\WINDOWS\system32\drivers\etc\hosts
Using an IF statement to check the error level
Code:
find "192.168.x.x" C:\WINDOWS\system32\drivers\etc\hosts
IF NOT %errorlevel%=="0" (
     echo.>>C:\WINDOWS\system32\drivers\etc\hosts
     echo 192.168.x.x intranet.com>>C:\WINDOWS\system32\drivers\etc\hosts
)
Read this so you understand what the operators are doing.
http://judago.webs.com/batchoperators.htm
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top