1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

CMD If find in file

Discussion in 'Software Development' started by dropdeadcriminal, Nov 10, 2011.

Thread Status:
Not open for further replies.
Advertisement
  1. dropdeadcriminal

    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
     
  2. dropdeadcriminal

    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
     
  3. Squashman

    Squashman 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
     
  4. Squashman

    Squashman 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
     
  5. dropdeadcriminal

    dropdeadcriminal Thread Starter

    Joined:
    Nov 10, 2011
    Messages:
    3
    Efficient!
    Do you have a trick to throw a new line in before adding the text as well?
     
  6. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    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
     
  7. Sponsor

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 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1026273

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice