Solved unexpected error

May 18, 2011 at 23:39:17
Specs: Windows XP
well i'm new to batch prgramming and i'm trying to find all the active ip's in a subnet through command prompt.
i have created a new.bat file which goes like
@echo off
set my-path=c:\
set my-file=ip's.txt
FOR /L %i in (1,1,255) do (
ping -n 1 192.145.23.%i
if %ERRORLEVEL% EQU 0 echo 192.145.23.%i was left on! >>%my-path%%my-file%
)

but when i run the file through cmd an error is display as "i was unexpected at this time"
what to do??


See More: unexpected error

Report •


#1
May 19, 2011 at 00:03:31
✔ Best Answer
Change %i to %%i (Script for variables need two %'s, command prompt only needs one.).

Also change:

if %ERRORLEVEL% EQU 0 

to:

if not errorlevel 1

Standard %vars% don't update in loops.


Report •

#2
May 22, 2011 at 20:41:27
thanks man...the batch file works fine now

Report •

#3
May 22, 2011 at 21:37:21
also help me abt

Report •

Related Solutions

#4
May 22, 2011 at 23:15:25
also i wanted to get the hostname in the file....
i know using nbtstat -a "IP" will give the hostname,,,but i want it in the notepad file along with the ip.

can u help me with that???


Report •

#5
May 23, 2011 at 00:29:44
What format do you want the output?

You could probably just add:

if not errorlevel 1 (
    echo 192.145.23.%%i was left on!
    netstat -a | find "192.145.23.%%i"
) >> "%my-path%%my-file%"

If you want a specific format you have give more details.


Report •

#6
May 23, 2011 at 00:34:27
It just occurred to me that nslookup may do the job easier:

nslookup 192.145.23.%%i | find "Name"


Report •

#7
May 23, 2011 at 01:24:22
i've tried using nslookup(netstat doesn't seem to work)....for some ip's an error is shown as
can't find 192.145.23.3: Non-existent domain,,,even though ping was successfull on that ip...

i need the output in the following manner

ip hostname
xxxx.xxxx.xxx.xxx xxxxxxxxx


Report •

#8
May 23, 2011 at 03:08:33
Sorry for the trouble but can you post what exactly the information that netstat gives you that you want to extract?

I gather that the problem is that netstat outputs the hostname, but not address or visa versa..


Report •

#9
May 23, 2011 at 03:13:16
What about "ping -a" does that show the name you want?

Report •

#10
May 23, 2011 at 03:38:32
A little stitched together, but:

@echo off
set my-path=c:\
set my-file=ip's.txt
FOR /L %%i in (1,1,255) do (
    set online=
    for /f "tokens=1,2" %%a in ('ping -n 1 -a 192.145.23.%%i^|findstr /i "pinging reply"^|sort /r ') do (
        if defined online (
            >> "%my-path%%my-file%" echo 192.145.23.%%i %%b
        )
        if /i "%%a"=="reply" set online=1
    )
)


Report •

#11
May 23, 2011 at 21:18:24
well nslookup is working just fine.it shows the output in the format

output as using nslookup:

"ip address" was left on!!
name: dssda.weqweq.com
"ip address" was left on!!
name: fgdfgd.yuy.com

i wanted the output in the format
ipaddress, name
ip1 , name1.com
ip2 , name2.com
ip3 , name3.com
ip4 , name4.com
ip5 ,name5.com

ie ip address and hostname as the columns and each row as an record

thanks for the above but it's the format of the output that i'm concerned with..
i got the hostname and the respectives ip's. just need them in a notepad file as above mentioned


Report •

#12
May 24, 2011 at 00:42:35
Well if #10 didn't do it for you (the comma could be added in):

@echo off
set my-path=c:\
set my-file=ip's.txt
FOR /L %%i in (1,1,255) do (
    ping -n 1 192.145.23.%%i
    if not errorlevel 1 (
        for /f "tokens=1* delims=:" %%a in ('nslookup 192.145.23.%%i | find /i "name:"  ') do (
            >> "%my-path%%my-file%"  echo 192.145.23.%%i,%%b
        )
    )
)


Report •

#13
May 24, 2011 at 01:04:40
#12 doesn't seem to work.. even the batch file is not executing. the cmd window opens and closes in a split second

could you plz check the code again...

Report •

#14
May 24, 2011 at 01:08:14
My mistake, left out a single character....

@echo off
set my-path=c:\
set my-file=ip's.txt
FOR /L %%i in (1,1,255) do (
    ping -n 1 192.145.23.%%i
    if not errorlevel 1 (
        for /f "tokens=1* delims=:" %%a in ('nslookup 192.145.23.%%i ^ find /i "name:"') do (
            >> "%my-path%%my-file%"  echo 192.145.23.%%i,%%b
        )
    )
)
pause


Report •

#15
May 24, 2011 at 01:16:13
i have added another variable "j"...does this change things or the code below should serve the purpose because it isn't.....

@echo off
set my-path=c:\
FOR %%j in (54,58) do (
FOR /L %%i in (1,1,25) do (
ping -n 1 192.145.%%j.%%i
if not errorlevel 1 (
for /f "tokens=1* delims=:" %%a in ('nslookup 192.145.%%j.%%i | find /i "name:" ') do (
>> "%my-path%new.txt echo 192.145.%%j.%%i,%%b
)
)
)
)
pause


Report •

#16
May 24, 2011 at 01:30:47
can't i just store the value of Name in a variable and try to output it using
set c=nslookup 192.145.%%j.%%i | find "Name"
echo 192.145.%%j.%%i,%c%

Report •

#17
May 24, 2011 at 01:52:07
Edit: Link.

What you have looks fine to me, with one exception, your missing the closing double quote on the file name(should be ...new.txt" echo...). based on your path you could just remove the first quote(I add them in as good practice).

The only way to capture the text from a command to a variable in batch is "for /f" (ignoring outside utilities). You could use an environment variable(setting from "for /f") but you would also have to use delayed expansion. See this link for some of the issues. http://www.computing.net/howtos/sho...


Report •

#18
May 24, 2011 at 01:59:25
I just realized that that you will end up with leading spaces, this will fix that:

"tokens=1* delims=: "


Report •

#19
May 24, 2011 at 02:04:35
nope this is not helping (or working) at all...

@echo off
set my-path=c:\
FOR %%j in (54,58) do (
FOR /L %%i in (1,1,25) do (
ping -n 1 192.145.%%j.%%i
if not errorlevel 1 (
echo 192.145.%%j.%%i
nslookup 192.145.%%j.%%i | find "Name"
) >> %my-path%new.txt

) )

gives me an output like

192.145.54.1
name: zpoz.qsqs.com
192.145.54.2
name: qweqw.tyut.com

i just need the code such that the notepad file reads like
192.145.54.1,zpoz.qsqs.com
192.145.54.2,qweqw.tyut.com

plz help me with this,,,i've been scratching my head for this all day


Report •

#20
May 24, 2011 at 02:08:39
Does this work ;)


@echo off
set my-path=c:\
FOR %%j in (54,58) do (
    FOR /L %%i in (1,1,25) do (
        ping -n 1 192.145.%%j.%%i
        if not errorlevel 1 (
            for /f "tokens=1* delims=: " %%a in ('nslookup 192.145.%%j.%%i ^| find /i "name:" ') do (
                >> "%my-path%new.txt" echo 192.145.%%j.%%i,%%b
            )
        )
    )
)
pause


Report •

#21
May 24, 2011 at 02:15:38
thanks a lot man...

Report •

#22
May 24, 2011 at 02:37:03
why doesn't this add date and time to the txt when executed

@echo off
set my-path=c:\chinmaya\
echo ip_address,hostname,date,time>"%my-path%new.txt
FOR %%j in (54,58) do (
FOR /L %%i in (1,1,25) do (
set c=%date%
set d=%time%
ping -n 1 192.145.%%j.%%i
if not errorlevel 1 (
for /f "tokens=1* delims=: " %%a in ('nslookup 192.145.%%j.%%i ^| find /i "name:" ') do (
>> "%my-path%new.txt" echo 192.145.%%j.%%i,%%b,%c%,%d%
set c=
set d=
)
)
)
)



Report •

#23
May 24, 2011 at 04:04:00
http://www.computing.net/howtos/sho...
@echo off
SetLocal EnableDelayedExpansion
set my-path=c:\chinmaya\
echo ip_address,hostname,date,time>"%my-path%new.txt"
FOR %%j in (54,58) do (
    FOR /L %%i in (1,1,25) do (
        ping -n 1 192.145.%%j.%%i
        if not errorlevel 1 (
            for /f "tokens=1* delims=: " %%a in ('nslookup 192.145.%%j.%%i ^| find /i "name:" ') do (
                >> "%my-path%new.txt" echo 192.145.%%j.%%i,%%b,!date!,!time!
            )
        )
    )
)


Report •

Ask Question