Note that there are some explanatory texts on larger screens.

plurals
  1. POStore return from a command into a variable in a bat file
    primarykey
    data
    text
    <p>I have a program where I take a list of IPs, run nslookup on them, and then store them into two different files, one where the IP is included, one which just has the output of nslookup. To cut my calls to nslookup in half, I would like to call it once, store it in a variable, and then write to both files. But, a simple store doesn't work.</p> <pre><code>set val=nslookup %ip% </code></pre> <p>displays</p> <pre><code>nslookup 127.0.0.1 </code></pre> <p>and not the result of running it.</p> <p>I've found one way to do this this is</p> <pre><code>nslookup %ip% | find "Name"&gt; tempFile set /P result=&lt;tempFile </code></pre> <p>but I'm now writing to a file and then getting the value back. For that cost, I might as well call nslookup twice. It also requires me to pick a single line (which is fine for me this time, but for other cases it may not work).</p> <p>I've also tried using the for loop trick</p> <pre><code>for /f "delims=" %%a in ('nslookup %ip% ^| find "Name"') do set name=%%a </code></pre> <p>but this only returns the very last line or the one line I specify with find. Still better than the previous example as there are no temp files, but still imperfect.</p> <p>So my question is if there is some better way to store the return value from a command to a variable.</p> <p>Edit: Consider</p> <pre><code>nslookup 127.0.0.1 </code></pre> <p>which returns</p> <pre><code>Server: fake.com Address: 0.0.0.0 Name: localhost Address: 127.0.0.1 </code></pre> <p>Right now, all I want is the name, so returning a single line works for my current batch file. But what if I also needed to know the server, so I want</p> <pre><code>for /f "delims=" %%a in ('nslookup 127.0.0.1') do set returnVal=%%a echo %returnVal% </code></pre> <p>to display</p> <pre><code>Server: fake.com Address: 0.0.0.0 Name: localhost Address: 127.0.0.1 </code></pre> <p>where right now it only displays</p> <pre><code>Address: 127.0.0.1 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload