8

I would like to run commands in a batch file on multiple computers.

For example:

@echo off
ping %1
pause
exit

If this batch file were called pingme.bat, and I type pingme.bat yahoo.com, then it would ping yahoo.com. My problem is I want the batch file to accept input from a text file.

For example, pingme.bat computers.txt would read the names of computers listed in the computers.txt file, and do whatever command I specified to be done to them.

%1 accepts the input I specify when I type the batch file. Now I would like the batch file to read the lines from that input text file and use them as arguments to the command.

The lines in the text are in list form, not using commas or anything.

kc2001
  • 5,008
  • 4
  • 51
  • 92
Dre_Dre
  • 745
  • 2
  • 6
  • 15

3 Answers3

10

One way to do so would be to place the URLS in a text file like so:

www.google.com
www.yahoo.com

Then run the following batch

for /f %%a in (%1) do (
echo Pinging %%a...
ping %%a
)

and run it from cmd as pingme.bat URLs.txt

Alternatively, you may specify the text file's name within the batch, and run it without the parameter

for /f %%a in (URLs.txt) do (
echo Pinging %%a...
ping %%a
)

Here's another approach

This particular batch will pull from the list, and write to output.txt if the ping was successful

@ECHO OFF
SET output=output.txt
IF EXIST "%output%" DEL "%output%"
FOR /f %%a IN (URLs.txt) DO (
    CALL :ping %%a

)
GOTO :EOF

:ping
ping -n 1 %1 | find "Approximate round trip" >NUL || ECHO %1>>"%output%"

Hopefully that sets you in the right direction.

  • 2
    Why SETLOCAL/ENDLOCAL inside 1st loop?. Why call a subroutine in 2nd loop? In both cases you are slowing things down for no reason that I can see. – dbenham May 18 '12 at 13:41
  • @dbenham Good point. That was left in there since modifying the script for the SO post. The subroutine is simply for organization. Thank, you. – Michael Capobianco May 18 '12 at 13:42
1

You may use a FOR loop - save this as pingme.bat:

FOR /F "tokens=*" %%L IN (%1) DO (
    ping %%L
    pause 
)

and call it with the text file as parameter pingme.bat computers.txt.

marapet
  • 54,856
  • 12
  • 170
  • 184
0

To find IP addresses of multiple URL's in text file and to get the output in text file:

FOR /F "tokens=*" %%L IN (%1) DO (
    nslookup %%L >> output.txt
    pause 
)

Save the script as "ping.bat" and call ping.bat URL.txt from the command prompt.

lenz
  • 5,658
  • 5
  • 24
  • 44
Xterm
  • 1
  • 1