# Batching: Get a hostname and IP from a list of file names



## autoamerican (Jul 4, 2012)

I'm working on a script right now and am stuck. What I've got is a script that reads a list of filenames in a directory and converts those filenames to hostnames (each file is named HOSTNAME.log) and dumps these to a log, then runs another command on that log to get the IP addresses for the host names and puts this in a second log, and then finally goes through that second log to strip out addresses in a certain subnet and give me the final output in a third log.

So far, this part all works - I get a clean list of IPs. What I would LIKE it to do is give me a final list of not just the IP addresses, but hostnames as well (preferably something formatted like *255.255.255.1 HOSTNAME.domain.net*), and I'd like it to create only one single file, not two interim files and a final clean output.

I've been modifying my script to try and add the hostname part, but so far haven't had any luck. What am I missing? This is the error I keep getting:

L:\>if exist c:\ipaddresses.log del c:\ipaddresses.log
i` was unexpected at this time.

L:\> for /f "skip=3 delims=: tokens=2 usebackq" i`) do @echo j >> c:\ipaddres
ses.log

Code:

*REM @ECHO OFF
REM ^^ watching the code

REM ------------------------------------------------------
REM Read the file names in the log directory on the server,
REM convert those file names to a list of hostnames.
REM ------------------------------------------------------

cd %1
if exist c:\names.log del c:\names.log
for /F "delims=" %%j in ('dir c:\Directory /A-D /B /O:GEN') do echo %%~nj >> c:\names.log

REM ------------------------------------------------------
REM Read the list of hostnames, convert them to a list of
REM hostnames / IP addresses.
REM ------------------------------------------------------

if exist c:\ipaddresses.log del c:\ipaddresses.log
for /f %%i in (c:\names.log) do (
for /f "skip=3 delims=: tokens=2 usebackq" %j in (`nslookup %i`) do @echo %i %j >> c:\ipaddresses.log
)

REM ------------------------------------------------------
REM Clean up our IP address file to strip out IPs within
REM the local group
REM ------------------------------------------------------

if exist c:\conlog.log del c:\conlog.log
type c:\ipaddresses.log |findstr /V /E 192.168.*.* >> c:\conlog.log*


----------



## Squashman (Apr 4, 2003)

Pretty sure the guys over at the DOSTips forums will help you. I post on both forums and I see no point in posting on both websites. They have a lot more knowledge of batch scripting over there then most of us here.


----------



## Squashman (Apr 4, 2003)

Remember that when you use a FOR LOOP in a batch file you need double up the percent signs for the variable. You are missing the double percent sign on 4 different loop variables.


----------

