Solved

Availability using a for command in a batch file

Posted on 2009-06-30
5
219 Views
Last Modified: 2012-05-07
Hi everyone,
Guys im having a problem with a batch file using a for statement.

I have a text file listing computer names called "c:\computers.txt"
A batch file called check.bat

When I run check.bat, I want it to read c:\computers.txt, and for each computer, check if it can be pinged or not, and write either:

<Computername>:  Down
or
<Computername>:  Up

to the file:
availability.txt

I just cant seem to get this working.

Any help greatly appreciated.

If for example, i have 3 systems in computers.txt, the output I get when running the batch file is:

IP address must be specified.
IP address must be specified.
IP address must be specified.

and in the availabilty.txt file, I get:

: Down
: Down
: Down


I dont know where im going wrong :>)
@echo off

for /f %%c in (c:\computers.txt) do ( CALL :GETINFO)
 

:GETINFO

Set Availability=

ping -n 2 -w 100 %1

IF ERRORLEVEL 1 GOTO UP

goto UP
 

:UP

Set Availability=Up
 

:DOWN

Set Availability=Down
 

echo %1: %Availability%>>availability.txt

Open in new window

0
Comment
Question by:Simon336697
  • 3
  • 2
5 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 24745297
Try this...
@echo off

setlocal enabledelayedexpansion

for /f %%c in (c:\computers.txt) do (

  call :GETINFO %%c

  echo %%c: !Availability!>>availability.txt

)

goto :eof

 

:GETINFO

if [%1]=[] goto :eof

Set Availability=

ping %1 -n 2 -w 100 

If %errorlevel%==1 goto DOWN

 

Set Availability=Up

goto :eof

 

:DOWN

Set Availability=Down

Open in new window

0
 
LVL 1

Author Comment

by:Simon336697
ID: 24745351
Hi sirbounty, hope you are well mate.
Sirbounty,

When i run your script, im getting the following mate :>)

=[] was unexpected at this time.
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 24745361
Try that once more...missed an = on line 10...
@echo off

setlocal enabledelayedexpansion

for /f %%c in (c:\computers.txt) do (

  call :GETINFO %%c

  echo %%c: !Availability!>>availability.txt

)

goto :eof

 

:GETINFO

if [%1]==[] goto :eof

Set Availability=

ping %1 -n 2 -w 100 

If %errorlevel%==1 goto DOWN

 

Set Availability=Up

goto :eof

 

:DOWN

Set Availability=Down

Open in new window

0
 
LVL 1

Author Comment

by:Simon336697
ID: 24745461
Sirb,

That works great mate.
Great to see you on here as always Sirb :>)
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 24745601
Glad I could help once again. :^)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now