Log ping and date isn’t working

batchpingredirection

:START
echo %date% %time% && ping -n 1 192.168.1.1 >> pingReport.txt 2>&1
echo %date% %time% && ping -n 1 192.168.1.2 >> pingReport.txt 2>&1
echo %date% %time% && ping -n 1 192.168.1.3 >> pingReport.txt 2>&1
echo %date% %time% && ping -n 1 192.168.1.4 >> pingReport.txt 2>&1
goto START

This echos Date and time on my screen only, but not into the file.

How can I get date and time in the txt file too?

Best Answer

  • echo and ping are two commands separated by "&&". The way you wrote it, the second command (after "&&") is sent to the text file.

    If you want both commands output to be sent to the text file, you can put them inside parentheses:

    :START
    (echo %date% %time% && ping -n 1 192.168.1.1) >> pingReport.txt 2>&1
    (echo %date% %time% && ping -n 1 192.168.1.2) >> pingReport.txt 2>&1
    (echo %date% %time% && ping -n 1 192.168.1.3) >> pingReport.txt 2>&1
    (echo %date% %time% && ping -n 1 192.168.1.4) >> pingReport.txt 2>&1
    goto START
    
  • Related Question