I decided to reformat my question due to comments of users (thanks to all) and new knowledge I have got. I'll not open a new post because the main problem remains the same:
How to handle strings produced by some program (e.g. CURL) line-by-line dynamically if those strings don't have LF
terminator?
CURL produces each line with interval approx 1 sec. I need to choose only some fields from CURL output for further processing (Current speed, Downloaded bytes, Downloded %), so I need to process each new line that appears.
But I can't because each line is ended with CR
.
The FOR loop doesn't show anything in CMD window until download is complete, and other users explained to me why:
FOR /F "delims=" %%x in ('curl ... http://some_url 2^>^&1') do echo %%x
So, I am forced to abandon the FOR
:
curl ... http://some_url | string_handler.bat
And using JREPL for replacing CR
to CRLF
on-the-fly:
curl ... http://some_url 2>&1 | jrepl "\r([^\n])" "\r\n$1" /xseq
But this solution produces the hole output, not line-by-line, after CURL is finished because if string doesn't have LF
terminator there is nothing to PIPE (thanks @Stephan). Maybe there is the solution to bypass the PIPE behaviour?
For solving problem I've made the simple script - simulation of CURL output. This is countdown timer and it produces every 1 sec a line with CR
terminator except first and last lines that have CRLF
:
:: bears.bat
@echo off
setlocal enabledelayedexpansion
::Define LF variable containing a linefeed (0x0A)
(set LF=^
%=empty line%
)
::Get a CR character (0x0D)
for /F %%a in ('copy /Z "%~F0" NUL') do set "CR=%%a"
:: First argument is CountDown high level, default 4 sec
if "%1"=="" (set /a high=4) else (set /a high=%1)
echo Hello bears
for /l %%i in (%high%,-1,0) do (
if %%i gtr 0 (
<nul set /p="Counter: %%i!CR!"
) else (
<nul set /p="Counter: %%i!CR!!LF!"
)
:: Pause 1 sec
if %%i gtr 0 ping 127.0.0.1 -n 2 >nul
)
exit /b