Read webs from a file and ping them with a loop (?) - batch-file

im making an script to ping different websites. I want to read addresses from a file, ping them one time, and write the results on another text file.
echo off
cls
echo "TEST" > %cd%\out.txt
time >> %cd%\out.txt
ping -n 1 google.com>> %cd%\out.txt
pause>null
I dont know how to make the loop for pinging and also, how to read line by line from the file. Can anyone help me?
Thanks.

Something along the lines of the following should be what you want:
#echo off& setlocal
echo google.com>sites.txt
echo yahoo.com>>sites.txt
set cd=.
for /f %%w in (sites.txt) do call :NextSite %%w
exit /b
:NextSite
echo. >>%cd%\out.txt
echo. >>%cd%\out.txt
echo. | time | find "current" >>%cd%\out.txt
ping -n 1 %1 >>%cd%\out.txt
The logic starts at the line with the "for" statement; the preceding lines merely set up a test environment. You will have your own %cd% setting and filename to replace "sites.txt"

Related

In a batch file how to write to a file and see also what is going on with a ping test?

I have a big batch file with PING and Iperf tests and it works, everything is written in a .txt file, I only want to see what is going on in the PING test for example, apparently >> command only writes to the text file. My solution is to send one time the PING without the >> to write to the file and another one with the >> to write to the file but this takes a lot of time for the purpose of the batch file.
Can anyone help me with a simpler solution?
thanks
here is part of the code:
ECHO.
(
ECHO Test started on %DATE% %TIME%
C:\Windows\System32\ping.exe %SERVER% | findstr /r /c:"[0-9] *ms"
if %errorlevel% == 0 (
echo.
echo TEST de PING OK ! next test iPERF
) else (
echo TEST de PING NOK
ECHO Done
PAUSE
EXIT
)
) >> "%LOGFILE%.client.log"
There is a special device con, which is essentially your command line window.
(
echo this goes to file
>con echo this goes to screen explicitely
echo this goes to file too
)>file.log

Reading a Text File and Reacting (Batch)

I looked for quite a while and couldn't find a good solution to my problem. I want a batch program to "look" in a .txt file for the command "" and if that word is in it, then to execute a different command. If the command existed, I would want to do something like set %textfile%=text.txt and if that worked I would then do if %textfile%==update goto update which would be an easy way to start an automatic update if this was in a loop. So basically, is there a command that sets a text file in %text%? This is the code that I am trying to add this into:
#echo off
color 0f
:start
echo Welcome to Master control pannel
ping 127.1 -n 4 >nul
cls
:options
cls
echo What would you like to do first? (Type the number of the operation you want to start)
echo.
ping 127.1 -n 2 >nul
echo 1. Run a command off of all computers
::(I want to run a command by sending a message to a text file but want recieving computor to be able to read it and execute it, how could I read the command and then do what it say, for example, if the command says "echo Hello" I would want recieving computor to say "Hello" )
echo 2. Stops the current command
echo 3. List all computers
echo 4. Open remote shutdown program
echo 5. Delete a computor (in progress)
echo 6. (Unfinished)
echo 7. (Unfinished)
echo 8. (Unfinished)
echo 9. (Unfinished)
echo 10. Exit
set /p choose=(1-9):
if %choose%==1 goto o1
if %choose%==2 goto o2
if %choose%==3 goto o3
if %choose%==4 goto o4
if %choose%==5 goto o5
if %choose%==6 goto close
if %choose%==7 goto close
if %choose%==8 goto close
if %choose%==9 goto close
if %choose%==10 goto o10
goto options
:close
cls
goto start
:o1
echo Stopping current command
del command.txt
echo. 2>command.txt
echo Command stopped!
pause
cls
goto start
I would greatly appreciate some help or comments to what I could do or add to this. Thanks!
Not an answer but several hints.
a variable can hold only single lines not a whole file.
if you want to get the first line of a file into a var use `Set /P "var="
set %textfile%=text.txt would store test.txt literally into a var whose name is the content of the var textfile.
you are mixing goto o1 and :01 with the label
`

How do you call variables put into a text file from a batch into a batch file?

I was Creating a quiz as a test of my skills and was able to successfully put text and a variables into a separate text file. When I tried to call them from the batch file, the variables were blank. How can I fix this? Also, if anyone can tell me how to recall a specific line, I would be grateful.
My Code (Shortened):
Echo What is your Name?
set /p Name=Name=
echo Test Results: > TestResults.txt
echo Name: %Name% >> TestResults.txt
cls
echo What's 2 + 2?
set /p Problem=2 + 2=
echo 2 + 2= %Problem% >> TestResults.txt
cls
echo Who is President?
set /p President=President=
echo President= %President% >> TestResults.txt
cls
echo Goodbye!
pause>nul
cls
for /f "delims= " %%G in (TestResults.txt) do echo %%G
pause>nul
If you look closely at your output, it should give you a clue to the problem.
When you TYPE the resultant file, it should have all the expected data:
c:\>TYPE TestResults.txt
Test Results:
Name: Simple Simon
2 + 2= 16
President= Bugs Bunny
But your FOR /F loop is producing:
Test
Name:
2
President=
Nothing appears after the fist space in each line. Look at your DELIMS option and it should be obvious you are setting the token delimiter to be a <space>. Simply make sure the DELIMS option is the last option in the list, and put the closing quote immediately after DELIMS=, and prolem is solved.
for /f "delims=" %%G in (TestResults.txt) do echo %%G

Problems in my simple chat program

I made a simple batch chat that write the message to an txt file.
I need help with print the file every time it changed and with hidding output.
I used the type, delay and cls to print the file but it didnt work, it didnt printed the file.
launcher.bat:
start cmd /k
call room.bat
call chat.bat
using the launcher photo
room.bat(the problem):
:chat1
cls
TYPE room.txt
timeout /t 0.5
goto chat1
chat.bat(working but show extra info about the os and the file):
#echo off
cls
set D=%Date%
cls
echo enter your name
SET /P name=[name]
pause
:room
cls
SET /P chatpublic=[everyone]
SET "
echo %name%: %chatpublic% |%D%|>> room.txt
pause
goto room
without the launcher photo
Here's the culprit:
echo %name%: %chatpublic% |%D%|>> room.txt
That's because the | vertical line has a special meaning in Windows command line interpreter: commandA | commandB: pipe the output from commandA into commandB.
If you do want use a | vertical line in another sense (e.g. display it with an echo command), then you should escape it as follows:
echo %name%: %chatpublic% ^|%D%^|>> room.txt

taking and writing variables for a batch file

Basically here's what I want: A batch file that prompts the user to set a variable,
set /p x=
then the batch file writes the variable to a file of some sort (abc.txt) Then later on, in a different batch file, the program retrieves the variable from the text document, and sets it as %x% again for whatever use. If there are any questions, or if I'm not clear enough, please comment, and I will revise. thanks.
In Batch Files, you can redirect input and ouput using < and > respectively.
Input.bat
#echo off
:: Take input and set value to x
set /p "x=: "
:: Print out the value of x to the screen, but redirect this to a text file
Echo %x% >> abc.txt
Echo EOF & Pause & Exit
Read.bat
#echo off
:: Set x to the first line in abc.txt
set /p x=< abc.txt
Echo First Line of abc.txt: %x%
Echo.
:: Set x to last line in abc.txt, incase it is multi-line file
for /f "delims=" %%a in (abc.txt) do (set x=%%a)
Echo Last Line of abc.txt: %x%
Echo.
Echo EOF & Pause & Exit
That should help you understand.
Mona.
I figured out a way that works best for me.
So I have the variable %x%, right? I got it from this:
set /p x=
then I write a mini-batch file.
echo set y=%x% >> abc.bat
then later on in a different script, I can use
call abc.bat
the variable y will be the value that I had in the origional batch script.

Resources