echo error cmd code example
Example 1: If ERRORLEVEL
@echo off
set host=host_ip
set logfile=Ping_test.log
set logfile_fail=Ping_test_fail.log
echo Target Host = %host% >%logfile%
for /f "tokens=*" %%A in ('ping %host% -n 1') do (echo (%%A>>%logfile% && GOTO Ping)
:Ping
for /f "tokens=* skip=2" %%A in ('ping %host% -n 1 ') do (
echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile%
if not %errolevel% 0 do (echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% errorlvel %errorlevel% %%A>>%logfile_fail%)
echo %errorlevel% %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A
timeout 1 >NUL
GOTO Ping
)
Example 2: If ERRORLEVEL
:Ping
for /f "tokens=* skip=2" %%A in ('ping %host% -n 1 ^& call echo %%^^errorlevel%%^>error.level') do (
set /p elv=<"error.level"
if not !elv!==0 (echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile_fail%)
echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile%
echo elv %elv% %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A
timeout 1 >NUL
GOTO Ping
)