0

I've seen this question posted before, but none of the solutions address my issue. I'm simply trying to iterate through a text file that contains host names. When I try the same command (omitting the extra percent signs) from the command line, it will work once or twice then then give the error noted below. Running it as a batch file, the batch file exits without doing anything. This one has really stumped me.

Here's the code in my batch file:

@echo off
set OUTPUTFILE=Results/Results.txt
set lookup=HostNames.txt
FOR /F %%i in (%lookup%) do 
FOR /F "usebackq skip=3 delims=: tokens=2" %j in (`nslookup %i`) 
do @echo %%i %%j >> %OUTPUTFILE%

At a command line I get:

i`) was unexpected at this time.

When I run it at a command line I am taking out the additional percent signs needs when it runs in a batch file. I'm also using absolute paths at the command line, to ensure it this isn't an issue with the environment variables I've set.

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
Jace Nimz
  • 25
  • 1
  • 1
  • 4

4 Answers4

3
@echo off
set OUTPUTFILE=Results\Results.txt
set lookup=HostNames.txt
FOR /F %%i in (%lookup%) do ( 
 FOR /F "skip=3 delims=: tokens=2" %%j in ('echo(^|nslookup %%i') do @echo %%i %%j >> %OUTPUTFILE%
)

Problems: / is a switch in winbatch; \ is a directory-level-separator.

do ( must be on the same physical line as its for

%i should be %%i

%j should be %%j

I've removed the usebackq and changed the quotes from backticks to single-quotes as it's not necessary to use usebackq here.


(minor fixes added)

The "flashing cursor" is caused by nslookup requesting information from the keyboard. Adding echo( supplies a newline to terminate nslookup and ^| is an escaped-pipe which directs the output of that echo to the input of nslookup.

Since my setup differs radically from yours, I can do but primitive verification.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • you forgot to set double percentage here `nslookup %i` – npocmaka Mar 11 '16 at 22:27
  • Thanks npocmaka & Magoo. This is closer, I think but still doesn't seem to be working. @Magoo I tried your corrections, now I'm getting the following output when I run the batch file: ` (FOR /F "skip=3 delims=: tokens=2" %j in ('nslookup HOSTNAME_EDITED') do @echo HOSTNAME_EDITED %j 1>>Results\Results.txt ) The syntax of the command is incorrect. ` – Jace Nimz Mar 11 '16 at 22:47
  • When I set "nslookup %%i" I get a flashing cursor. – Jace Nimz Mar 11 '16 at 22:49
  • Thanks for the script, useful and highly configurable. Noice! – Charles Tempo Oct 27 '20 at 13:31
2

@Magoo had the best answer for my needs. The batch file works as intended once I fixed a major issue: Kids, DON'T name your batch file with a windows internal command name. I had called the batch file "nlookup.bat" which caused MAJOR brain damage; a lesson I'll not soon forget.

Thank you all, this is working well now!

Jace Nimz
  • 25
  • 1
  • 1
  • 4
1

I modified Magoo's answer to output to a CSV file. It will output two columns "#, Input, Output" where you will have two rows for each successful nslookup call, one with the hostname, and one with the IP. I also modified the delims to delimit on ":" and " " so that the output doesn't have a bunch of extra spaces. I wanted to contain each query on a single row with columns "Input, Hostname, IP" but I got frustrated so I just added a counter in the loop and outputted it as the first column for each separate query and figured this was good enough.

@echo OFF
setlocal enabledelayedexpansion enableextensions
set me=%~n0
set parent=%~dp0
set outputfile=%parent%nslookup_results.csv
set inputfile=%parent%nslookup_input.txt
@echo #,Input,Output >> %outputfile%
FOR /F %%G in (%inputfile%) do (
 set /a resultcount+=1
 FOR /F "skip=3 tokens=2 delims=: " %%J in ('echo(^|nslookup %%G') do @echo !resultcount!,%%G,%%J >> %outputfile%
)
M A
  • 11
  • 2
0

try like this:

@echo off
set "OUTPUTFILE=Results\Results.txt"
set "lookup=HostNames.txt"
FOR /F %%i in (%lookup%) do  (
    FOR /F "usebackq skip=3 delims=: tokens=2" %%j in (`nslookup %%i`) do (
        @echo %%i %%j >> %OUTPUTFILE%
    )
)
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • Thanks! I don't think that's the right syntax in ('nslookup %%i') When I set "nslookup %%i" I get a flashing cursor. – Jace Nimz Mar 11 '16 at 22:51