Using set /P to read from a Pipe splits the text

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
einstein1969
Expert
Posts: 961
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Using set /P to read from a Pipe splits the text

#1 Post by einstein1969 » 08 Mar 2016 15:01

Hi,

I have a problem with tuning this code. The problem is the SET/P.

I have semplified the code and tested variuos method.

Can anyone expert comment/explain the result?

Code: Select all

@echo off

setlocal EnableDelayedExpansion

if not "%1"=="" goto :%1

echo HB 1 ^| find "TTL" ^| MAIN
"%~f0" HB 1 | find "TTL" | "%~f0" MAIN

echo(

echo HB 1 ^| findstr "TTL" ^| MAIN
"%~f0" HB 1 | findstr "TTL" | "%~f0" MAIN

echo(

echo HB 1 ^| find "TTL" ^| more ^| MAIN
"%~f0" HB 1 | find "TTL" | more | "%~f0" MAIN

pause

exit /b

:HB

 for /L %%N in (0,1,5) do (

    ping -n 2 127.0.0.1 >nul
    start "HBPING" /B ping -n %2 127.0.0.1

 )

exit /b

:MAIN

 for /L %%n in (0,1,5) do (

   set _HB=
   set/p "_HB="

   if defined _HB (
     echo("!_HB!"
   ) else echo("N/D"

 )

exit/b


results:

Code: Select all

HB 1 | find "TTL" | MAIN
"Risposta da 127.0.0.1: byte=32 durata<1m"
"s TTL=128"
"Risposta da 127.0.0.1: byte=32 durata<1m"
"s TTL=128"
"Risposta da 127.0.0.1: byte=32 durata<1m"
"s TTL=128"

HB 1 | findstr "TTL" | MAIN
"Risposta da 127.0.0.1: byte=32 durata<1ms TTL=128"
"N/D"
"N/D"
"N/D"
"N/D"
"N/D"

HB 1 | find "TTL" | more | MAIN
"Risposta da 127.0.0.1: byte=32 durata<1ms TTL=128"
"N/D"
"Risposta da 127.0.0.1: byte=32 durata<1ms TTL=128"
"N/D"
"Risposta da 127.0.0.1: byte=32 durata<1ms TTL=128"
"N/D"
Premere un tasto per continuare . . .


The best for me is the third.

It's possible eliminate the N/D cicle or the segmentation of the line?

Einstein1969

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: HeartBeat Demos

#2 Post by foxidrive » 08 Mar 2016 15:57

einstein1969 wrote:Hi,

I have a problem with tuning this code. The problem is the SET/P.

I have semplified the code and tested variuos method.

Can anyone expert comment/explain the result?


einstein, I'm commenting here on how you can improve your question.
Essentially, try and explain what you're doing in your code and what the purpose is.


As it stands you've shown some code, and results, and asked for help - but a reader doesn't know what your code is doing or what it's for. It's not exceptionally long code, but you're forcing a reader to examine and study your code to try and figure out the function - before they can help you with your task. The only clue is "heartbeat" but that doesn't explain what your code is attempting to achieve.

If you explain the function then it removes that entire step and a reader can focus on your problem.

If there's a problem with SET /P then explain what the problem is.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: HeartBeat Demos

#3 Post by ShadowThief » 08 Mar 2016 16:00

So... it's a radar simulator? Cool to look at, I guess.

einstein1969
Expert
Posts: 961
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: HeartBeat Demos

#4 Post by einstein1969 » 09 Mar 2016 03:32

foxidrive wrote:
einstein1969 wrote:Hi,

I have a problem with tuning this code. The problem is the SET/P.

I have semplified the code and tested variuos method.

Can anyone expert comment/explain the result?


einstein, I'm commenting here on how you can improve your question.
Essentially, try and explain what you're doing in your code and what the purpose is.


As it stands you've shown some code, and results, and asked for help - but a reader doesn't know what your code is doing or what it's for. It's not exceptionally long code, but you're forcing a reader to examine and study your code to try and figure out the function - before they can help you with your task. The only clue is "heartbeat" but that doesn't explain what your code is attempting to achieve.

If you explain the function then it removes that entire step and a reader can focus on your problem.

If there's a problem with SET /P then explain what the problem is.


Thanks foxidrive.

Behaviour requested:
This code try to catch the output of multiple PING using a PIPE with the tecnich of reading via SET/P.
Why?
The multiple output of PING is executed at intervarls of TIME of 1 second. But if there are more PINGS at the time
the output is overlying and the interval of time resulted is few centisecond or less!

The FIND "128" or FIND "TTL" is for isolating the only lines that BEAT.

The part in the MAIN wait "without using CPU" until FIND trasmit the line. Then the MAIN Beat at the same frequency
of the first part of pipe chain.

With this trick I partially implementing Delay()/Wait() function in dos batch. This not use active CPU for execute!!!

The problem:
When the set/P read from Pipe has strange behaviour and split the input Line. Then the MAIN part BEAT 2 time instead of one.
When I using the MORE the line is split at different place (I think at the end), but it sent a second command at SET/P with
NO DATA or DATA that is removed. Then the MAIN beat/cicle 2 time. This behaviour is not wanted.

I can open a separate thread for this problem?

Einstein1969

einstein1969
Expert
Posts: 961
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Using set /P to read from a Pipe splits the text

#5 Post by einstein1969 » 10 Mar 2016 10:49

Hi,

I have simplified the code.

The output of PING is locale dependent. Can anyone confirm my output / put the results?

Code: Select all

@echo off

setlocal EnableDelayedExpansion

if not "%1"=="" goto :%1

echo PING ^| find "TTL" ^| MAIN
( ping -n 2 127.0.0.1 >nul & ping -n 4 127.0.0.1 ) | find "TTL" | "%~f0" MAIN

echo(

echo PING  ^| find "TTL" ^| more ^| MAIN
( ping -n 2 127.0.0.1 >nul & ping -n 4 127.0.0.1 ) | find "TTL" | more | "%~f0" MAIN

pause

exit /b

:MAIN

 for /L %%n in (0,1,7) do (

   set _HB=
   set/p "_HB="
   
   if defined _HB (
     echo("!_HB!"
   ) else echo("N/D"

 )

exit/b


my result:

Code: Select all

PING | find "TTL" | MAIN
"Risposta da 127.0.0.1: byte=32 durata<1m"
"s TTL=128"
"Risposta da 127.0.0.1: byte=32 durata<1m"
"s TTL=128"
"Risposta da 127.0.0.1: byte=32 durata<1m"
"s TTL=128"
"Risposta da 127.0.0.1: byte=32 durata<1m"
"s TTL=128"

PING  | find "TTL" | more | MAIN
"Risposta da 127.0.0.1: byte=32 durata<1ms TTL=128"
"N/D"
"Risposta da 127.0.0.1: byte=32 durata<1ms TTL=128"
"N/D"
"Risposta da 127.0.0.1: byte=32 durata<1ms TTL=128"
"N/D"
"Risposta da 127.0.0.1: byte=32 durata<1ms TTL=128"
"N/D"
Premere un tasto per continuare . . .


Why with more is different?

Einstein1969

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Using set /P to read from a Pipe splits the text

#6 Post by ShadowThief » 10 Mar 2016 13:39

PING | find "TTL" | MAIN
"Reply from 127.0.0.1: bytes=32 time<1ms "
"TTL=128"
"Reply from 127.0.0.1: bytes=32 time<1ms "
"TTL=128"
"Reply from 127.0.0.1: bytes=32 time<1ms "
"TTL=128"
"Reply from 127.0.0.1: bytes=32 time<1ms "
"TTL=128"

PING | find "TTL" | more | MAIN
"Reply from 127.0.0.1: bytes=32 time<1ms TTL=128"
"N/D"
"Reply from 127.0.0.1: bytes=32 time<1ms TTL=128"
"N/D"
"Reply from 127.0.0.1: bytes=32 time<1ms TTL=128"
"N/D"
"Reply from 127.0.0.1: bytes=32 time<1ms TTL=128"
"N/D"
Press any key to continue . . .

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Using set /P to read from a Pipe splits the text

#7 Post by foxidrive » 11 Mar 2016 02:11

I forgot to reply. I'd done some experimenting and only got the results you already showed, and life got in the way here.

This is in Win 8.1 32 bit from your latest post.
I'd only guess that the oddness when using pipes sometimes, is involved.

Code: Select all

PING | find "TTL" | MAIN
"Reply from 127.0.0.1: bytes=32 time<1ms "
"TTL=128"
"Reply from 127.0.0.1: bytes=32 time<1ms "
"TTL=128"
"Reply from 127.0.0.1: bytes=32 time<1ms "
"TTL=128"
"Reply from 127.0.0.1: bytes=32 time<1ms "
"TTL=128"

PING  | find "TTL" | more | MAIN
"Reply from 127.0.0.1: bytes=32 time<1ms TTL=128"
"N/D"
"Reply from 127.0.0.1: bytes=32 time<1ms TTL=128"
"N/D"
"Reply from 127.0.0.1: bytes=32 time<1ms TTL=128"
"N/D"
"Reply from 127.0.0.1: bytes=32 time<1ms TTL=128"
"N/D"
Press any key to continue . . .

einstein1969
Expert
Posts: 961
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Using set /P to read from a Pipe splits the text

#8 Post by einstein1969 » 11 Mar 2016 07:26

I'm waiting for a German or other locale, but I have noticed that the FIND plus set/P break the
input of PING at 40 characters.
With the MORE seem that this limit at 40 not work. But it's introduced a strange other LINE with no chars or this chars are stripped.

I have probed without FIND and MORE and the output of PING is more splitted (at space).

Code: Select all

@echo off

setlocal EnableDelayedExpansion

if not "%1"=="" goto :%1

echo PING ^| find "TTL" ^| MAIN
( ping -n 2 127.0.0.1 >nul & ping -n 4 127.0.0.1 ) | find "TTL" | "%~f0" MAIN

echo(

echo PING  ^| find "TTL" ^| more ^| MAIN
( ping -n 2 127.0.0.1 >nul & ping -n 4 127.0.0.1 ) | find "TTL" | more | "%~f0" MAIN

echo(

echo PING  ^| MAIN
( ping -n 2 127.0.0.1 >nul & ping -n 4 127.0.0.1 ) | "%~f0" MAIN

pause

exit /b

:MAIN

 for /L %%n in (0,1,7) do (

   set _HB=
   set/p "_HB="
   
   if defined _HB (
     echo("!_HB!"
   ) else echo("N/D"

 )

exit/b


output:

Code: Select all

PING | find "TTL" | MAIN
"Risposta da 127.0.0.1: byte=32 durata<1m"
"s TTL=128"
"Risposta da 127.0.0.1: byte=32 durata<1m"
"s TTL=128"
"Risposta da 127.0.0.1: byte=32 durata<1m"
"s TTL=128"
"Risposta da 127.0.0.1: byte=32 durata<1m"
"s TTL=128"

PING  | find "TTL" | more | MAIN
"Risposta da 127.0.0.1: byte=32 durata<1ms TTL=128"
"N/D"
"Risposta da 127.0.0.1: byte=32 durata<1ms TTL=128"
"N/D"
"Risposta da 127.0.0.1: byte=32 durata<1ms TTL=128"
"N/D"
"Risposta da 127.0.0.1: byte=32 durata<1ms TTL=128"
"N/D"

PING  | MAIN
"N/D"
"con 32 byte di dati:"
"Risposta da 127.0.0.1: "
"byte=32 "
"durata<1ms "
"TTL=128"
"Risposta da 127.0.0.1: "
"byte=32 "
Premere un tasto per continuare . . .


Einstein1969

einstein1969
Expert
Posts: 961
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Using set /P to read from a Pipe splits the text

#9 Post by einstein1969 » 11 Mar 2016 07:50

foxidrive wrote:I forgot to reply. I'd done some experimenting and only got the results you already showed, and life got in the way here.

This is in Win 8.1 32 bit from your latest post.
I'd only guess that the oddness when using pipes sometimes, is involved.


Yes, i know that the pipe and set/p have strange behaviour, but I need some information for found a solution for
the implementation of delay()/wait()/sleep() function.

If the slipt of PING is "always" 2 lines in each locale than I can use TWO set/P for bypass the problem.

Code: Select all

Set/P =
Set/P =


should work.

The MORE block at 65535! I have tested :(

Einstein1969

Post Reply