title: Batch File Check for String in Other File
body: <p>I have a batch file that writes a line with an ip and a name for the ip in the etc/hosts file. Is there a way to check if this line already exists? Or alternatively just see if a word exists in the file?</p>
  
  <p>edit:
  want something like if string exists move on but if not echo</p>
  
  <p><strong>initial code</strong></p>
  
  <pre><code>findstr "mystring" "C:\Windows\System32\drivers\etc\hosts" &gt;nul 2&gt;&amp;1
  if errorlevel 1 echo 111.222.333.444 mystring&gt;&gt;%systemroot%\SYSTEM32\DRIVERS\ETC\HOSTS
  </code></pre>
  
  <p><strong>Solved: with suggested enhancements</strong></p>
  
  <pre><code>    findstr /V "^#" "C:\Windows\System32\drivers\etc\hosts" | findstr /ILC:"mystring" &gt;nul 2&gt;&amp;1 ||^
  (echo 111.222.333.444 mystring&gt;&gt;%systemroot%\SYSTEM32\DRIVERS\ETC\HOSTS)
  </code></pre>
  
  <ul>
  <li>can ignore commented out lines</li>
  <li>can search for case-insensitive string</li>
  <li>will write a string if string not found</li>
  </ul>