title: Get hostnames from ping and store results to text
body: <p>Okay so here is the situation. I have a very large text file containing a lot of different IP addresses. I want to create a batch file, or anything that will get the job done, in order to resolve all of the host names for these IP addresses.</p>
  
  <p><strong>Currently</strong>, I have the IP Addresses stored one per line in a file called addresses.txt.</p>
  
  <p>I also have a batch file with the following code:</p>
  
  <pre><code>@echo off
  for /f "delims=" %%a in (addresses.txt) do ping -a %%a &gt;&gt; PING_LOG.txt &amp;&amp; (echo %%a Passed) || (echo %%a Failed)
  pause
  </code></pre>
  
  <p>This seems to work okay. It fills my text file with all of the results from the ping; the hostname, the replies, the stats, packets, etc. </p>
  
  <p><strong>Here is the problem:</strong>
  I <em>only</em> want the address I pinged and the hostname to be written to my output file. Is there any way I can accomplish this? Through the batch commands? Through other means (such as taking the output file and running it through something else to grab the information)? I am still actively trying to see what I can figure out, but any help would be immensely appreciated.</p>
  
  <p>(The final goal is to place the host name and addresses in an excel spreadsheet side-by-side, and considering there are literally thousands of them doing it manually sounds terrible)</p>
  
  <p><strong>MY FINAL SOLUTION</strong></p>
  
  <pre><code>@echo off
  setlocal EnableDelayedExpansion
  for /f "tokens=*" %%A in (addresses.txt) do (
      set "_res=%%A"
      for /f "tokens=2" %%B in ('^(nslookup %%A ^) ^| findstr /c:"Name:"') do set "_res=!_res!, %%B"
      echo !_res! &gt;&gt; LOG.txt
      echo !_res!
  )
  endlocal
  </code></pre>
  
  <p>Thanks to Dmitry and Gravity for the help ^</p>
  
  <p>Next, I took the data and dropped it into a spreadsheet (just one column). I wrote a macro script that looked at each cell, split the data into a string array using the comma as a delimeter, and then placed each resulting string into one column side by side (ip | name). </p>