There's no such thing as a stupid question, but they're the easiest to answer.


DOS/Other DOS/Other
Search Search
Search for:
Tech Support Guy > > >

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


autoamerican's Avatar
autoamerican autoamerican is offline
Member with 1 posts.
Join Date: Jul 2012
04-Jul-2012, 01:33 PM #1
Batching: Get a hostname and IP from a list of file names
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, 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


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's Avatar
Trusted Advisor with 19,725 posts.
Join Date: Apr 2003
Location: 1265 Lombardi Ave
04-Jul-2012, 03:51 PM #2
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's Avatar
Trusted Advisor with 19,725 posts.
Join Date: Apr 2003
Location: 1265 Lombardi Ave
04-Jul-2012, 03:54 PM #3
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.
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools

You Are Using: Server ID
Trusted Website Back to the Top ↑