I
I
Illium2018-03-21 12:02:49
Ping
Illium, 2018-03-21 12:02:49

Why does findstr behave inappropriately in batch file?

Friends, good day!
We have a batch file

@set iptoping=127.0.0.1
@set pdir=C:\pinglog
@echo Пингуем %iptoping% пакетами по 5000 байт с выводом в %pdir%\ping_%iptoping%.log. Для завершения закройте окно.
@MD %pdir%
@>"%pdir%\ping_%iptoping%.log" (
 for /l %%i in () do @>nul chcp 866& ping -l 5000 -n 1 %iptoping%| findstr /i "Превышен Заданный"|(
 for /f "delims=" %%j in ('more') do @>nul chcp 866& cmd/v/c "echo.!date! !time:~0^,8! %%j")
)

In this form, findstr searches the output of the ping command for lines containing "Exceeded" or "Specified", writes the line to a variable, and then outputs it to a file with the date and time.
However, it is worth adding (or replacing) "Answer" so that the command takes the following form
findstr /i "Превышен Заданный Ответ"
and if the address is available (response is received), the Log output begins to pour into the command line window,
Не удаётся найти указанный файл
while it is written adequately at first glance (i.e. "date time Response from ...", only there are much fewer lines in it than lines of spam in the command line.
Gentlemen, there is only one question - what file is he looking for and why can't he find it? Question two - how to prevent him (or hide messages) from spamming through the window Question three - can my lazy ass show an example of the same for PowerShell?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2018-03-21
@Illium

I changed it a little, as it seemed to me correct - it should display the date / time in the log when the node is unavailable.

@echo off
set "iptoping=127.0.0.1"
set "pdir=C:\pinglog"
echo Пингуем %iptoping% пакетами по 5000 байт с выводом в %pdir%\ping_%iptoping%.log. Для завершения закройте окно.
1>nul 2>&1 md %pdir%
>"%pdir%\ping_%iptoping%.log" (
for /l %%i in () do (
   for /f "delims=" %%a in ('ping -l 5000 -n 1 %iptoping% ^| findstr "Превышен Заданный Ответ" ') do echo.%date% %time:~0,8% %%a
  )
)

I can not understand what for in the ping parameter -l?
And yet, you need to put a pause, otherwise it turns out some kind of DoS attack.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question