How can I get a batch file for inputting input from a txt file?

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 was called pingme.bat and I type pingme.bat yahoo.com , then it will be ping yahoo.com. My problem is that I want the batch file to accept input from a text file.

Like pingme.bat computers.txt , and it will read the names of the computers listed in the file and execute any command that I specify for them.

%1 accepts the input that I specify when entering the batch file, now I would like the batch file to read the lines in txt and do this.

Lines in the text are in list form, without using a comma or anything else.

+7
source share
3 answers

One way to do this is to place the URLS in a text file as follows:

www.google.com
www.yahoo.com

Then run the next batch

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

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

Alternatively, you can specify the name of the text file in the package and run it without a parameter

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

Here is a different approach

This particular batch will pull from the list and write to output.txt if 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%" 

We hope this helps you in the right direction.

+9
source

You can use the FOR loop - save this as pingme.bat:

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

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

+1
source

To find the IP addresses of multiple URLs in a text file and get them in a 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 line.

0
source

All Articles