I'm running a batch file that runs a ftp connection.
Everything is working;
my only problem is that I want to see the result of the ftp session.
I have done this:
#echo off
(
for /f "usebackq delims=" %%a in ("IP.txt") do (
ping -n 1 %%a |find "TTL=" >nul
if errorlevel 1 (echo %%a failed) else (echo %%a passed
start c:\ftp -s:test.txt %aa)
)>%%a.txt
)>output.log
but this only creates me an empty txt file.
If I do the same command not in a batch file - it's working
What could be the problem?
Thanks
Related
#echo off
set source_file=C:\TEMP\CopyFile\TestFileToCopy.txt
set ip_list=C:\TEMP\PCList\PCList.txt
set log_file=C:\TEMP\Logs\copy_log.txt
for /F "usebackq tokens=*" %%i in (%ip_list%)
do (set destination=\%%i\c$\TEMP echo Checking availability of %%i...ping -n 1 %%i | find "TTL=" > nul
if errorlevel 1 (echo %%i is unavailable. >> %log_file%)
else (robocopy %source_file% %destination% /E
if errorlevel 0 ( echo File copy to %%i completed successfully. >> %log_file% )
else (echo File copy to %%i failed. >> %log_file%)
)
)
echo File copy complete. Check %log_file% for results.
pause
I try to make the above code to work, but a cmd window opens and the notepad window with PCList.txt open and nothing happens. I want to copy a file to many remote computers and check with ping if the pc is live.
I can't use ping command because it is blocked on my server.
I followed the anser from https://superuser.com/questions/1172790/bat-file-to-check-servers-are-up/1173006#answer-1173006. But it uses ping command in batch file.
Here is my code:
REM add urls in urls.txt
for /f %%a in (C:\Job-Batches\server_check\urls.txt) do (
echo Pinging %%a ...
ping -n 2 %%a | find "Reply" > NUL
if not errorlevel 1 (
REM ip is down
echo %%a is down
) else (
REM ip is up
echo %%a is up
)
)
Can someone suggest, if it can be done without ping...
Thanks in advance!
I would like to get the ip address from a site (eg. google.com gives me 173.194.66.103), and i do this by pinging the website.
For /f "tokens=2 delims=[]" %%a in ('ping -n 1 www.google.com^|find "Pinging"') do set ip=%%a
pause
This works greatly in CMD, but when i type it in a batch file it doesn't load. And when i try to close the window i get the error:
"The process tried to write to a nonexistent pipe.
^C^C^C^C^C^C^C^C> was unexpected at this time.
'a' is not recognized as an internal or external command, operable program or batch file"
I believe it is the ping part, which gives the same error in batch when run seperately.
ping -n 1 www.google.com
You code can be simplified to
for /f "tokens=2 delims=[]" %%a in ('ping -n 1 www.google.com') do set "ip=%%a"
There is no need for the find as the only line that will have a second token is the one with the square brackets.
But the problem persits. Why does it generate the indicated error?
The usual response is Your batch file is named ping.cmd, ping.bat, so, when you call ping from your batch file, the batch file is calling itself (or in your case and original code this will also happen with find.cmd or find.bat).
Rename the batch file (recomended) or change the command to call ping.exe (the same for find.exe) to avoid conflicts.
Try something like that :
#echo off
Title Getting The IP address
Setlocal EnableDelayedExpansion
set myServer=www.google.com
for /f "tokens=1,2 delims=[]" %%a IN ('ping -n 1 !myServer!') DO (
if "%%b" NEQ "" set ip=%%b
)
echo The IP address of !myServer! is !ip!
EndLocal
Pause
I'm trying to create a little batch file that checks multiple PCs read from a text file. For any PCs it finds are pingable, it writes a line in a "results" text file saying so. Here's what I've got:
#Echo off
set file=C:\logs\registercheck.txt
date /t >%file%
FOR /F %%I IN (C:\work\regnames.txt) DO (ping /n 1 %%I | ping /n 1 %%I | IF errorlevel 1 goto :nextreg | echo %%I is still on and has not been powered off! >>%file% | :nextreg)
PAUSE
So...when I run the file, I get multiple lines of "goto was unexpected at this time" and the only thing written in my output text file is the date. What am I doing wrong?
Thank you!
#Echo off
setlocal enableextensions disabledelayedexpansion
set "logFile=C:\logs\registercheck.txt"
set "inputFile=C:\work\regnames.txt"
>>"%logFile%" date /t
for /f "usebackq delims=" %%i in ("%inputFile%") do (
ping -n 1 %%i >nul 2>nul
if not errorlevel 1 (
>>"%logFile%" echo(%%i is still on and has not been powered off!
)
)
You have two errors.
The first is that to put all the commands in a single line, the separator is not the pipe character (|) but the ampersand (&)
The second is that inside the do code block of the for command, if one goto is executed, the for command is finished, independently of where the label is placed. And labels inside for code blocks usually generate errors (depends of its position).
If instead of the previous code, you want a single line loop, it can be written as
for /f "usebackq delims=" %%i in ("%inputFile%") do ( ping -n 1 %%i >nul 2>nul & if not errorlevel 1 >>"%logFile%" echo(%%i is still on and has not been powered off! )
or
for /f "usebackq delims=" %%i in ("%inputFile%") do ( ping -n 1 %%i >nul 2>nul && >>"%logFile%" echo(%%i is still on and has not been powered off! )
that makes use of the && construct. It is intended as a shortcut for the if not errorlevel 1 .... If the command at the left of the && does not raise an errorlevel, then the command on the right side is executed.
This for the batch sintax. Now the ping. There is a difference in how ping command behaves depending of the ip version. It is not the same to ping an ipv4 address than to ping an ipv6 address. If needed you can grab from here a subrotine to handle the differences.
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.
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.
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.
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.