Asynchronous native batch tee script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Asynchronous native batch tee script

#16 Post by dbenham » 20 Feb 2014 10:10

jeb wrote:
dbenham wrote:The delayed expansion is somehow disabled when the command is aborted with Ctrl-C.


It has nothing to do with the Ctrl-C, but you could read this article :wink: Why does delayed expansion fail when inside a piped block of code?

jeb

Actually it does have something to do with Ctrl-C, since the value is expanded properly when I don't press Ctrl-C (I temporarily disabled the END condition test so that I could see the value). It fails to expand properly after I press Ctrl-C. My guess is the Ctrl-C kills the batch context, reverting to delayed expansion off. The loop continues to execute in command prompt mode since it is already fully parsed and is executing from memory.


Dave Benham

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Asynchronous native batch tee script

#17 Post by jeb » 20 Feb 2014 12:52

dbenham wrote:Actually it does have something to do with Ctrl-C, since the value is expanded properly when I don't press Ctrl-C (I temporarily disabled the END condition test so that I could see the value). It fails to expand properly after I press Ctrl-C.

I not able to reproduce your results (and I can't believe them)

A simplification of the test

Code: Select all

@echo off
setlocal enableDelayedExpansion
set yes=True
(find /n /v ""&echo !yes!&echo :END) | more


Startet with

Code: Select all

echo Hello | sample.bat


[1]Hello
!yes!
:END


And I can't see any cause why the !yes! should expand,
but it could be possible that you have enabled delayed expansion via registry at your system.

The only way to expand in a pipe, the expression must be in a single line command

Code: Select all

setlocal enableDelayedExpansion
set yes=True
echo !yes! | more
echo one ^& echo !yes!
(echo This fails & echo !yes!) | more


jeb

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Asynchronous native batch tee script

#18 Post by dbenham » 21 Feb 2014 21:12

Your right, of course. I had myself thoroughly confused. I have no idea any more what I was looking at.

I would have responded earlier, but my home computer died 2 nights ago from a power surge :evil:


Dave Benham

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

Re: Asynchronous native batch tee script

#19 Post by foxidrive » 21 Feb 2014 23:48

dbenham wrote:but my home computer died 2 nights ago from a power surge :evil:


D'oh!

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

Re: Asynchronous native batch tee script

#20 Post by einstein1969 » 01 Mar 2014 15:53

Hi Dave!

very nice work. A question:

why do you use the line?:

Code: Select all

(call )


einstein1969

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Asynchronous native batch tee script

#21 Post by Ed Dyreen » 01 Mar 2014 16:32

Code: Select all

(call ) ; exit and set errorLevel=0
(call)  ; exit and set errorLevel=1
slower but not limited to 0 v 1

Code: Select all

@echo off

call "%~f0" :exit 5
set errorLevel
pause

:exit
exit /b %~1
even slower but not requiring a function

Code: Select all

cmd /c exit 5

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Asynchronous native batch tee script

#22 Post by dbenham » 01 Mar 2014 17:14

Ed Dyreen wrote:

Code: Select all

(call ) ; exit and set errorLevel=0
(call)  ; exit and set errorLevel=1

I almost agree, except that neither syntax EXITs anything. It simply sets the ERROROLEVEL to either 0 or 1.


Dave Benham

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

Re: Asynchronous native batch tee script

#23 Post by einstein1969 » 01 Mar 2014 18:02

thanks Ed, thanks Dave.

einstein1969

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: Asynchronous native batch tee script

#24 Post by pieh-ejdsch » 06 Mar 2014 05:11

Good mornig,

you can read Input without using find.
Now you can read any comments for Input one key.
command with Output Redirect into Logfile. Then the simultan Batch only runs to read this logfile.
No Line number
only one empty line a time.

Code: Select all

::batchTee.bat usage: 
:: command >logfile |batchTee LogFile

@echo off
setlocal
set "LogFile=%~1"
if not exist %1 echo Datei nicht gefunden. &exit /b 1
set /a FileOpen =0

call :canIread <%1
exit /b 0

:canIread
:echo on
(type nul>>"%LogFile%") 2>nul && set /a FileOpen +=1
call :forL
if %FileOpen% gtr 20 exit /b
goto :canIread

:forL
for /l %%. in (1 1 100) do set /p "line=" && (
  set "LineEmpty="
  setlocal enabledelayedexpansion
  echo !line!
  endlocal
 ) || (
  if not defined LineEmpty (
   set /a FileOpen=0
   echo(
   set "LineEmpty=1"
  )
)
exit /b


Phil

alexsander.albani
Posts: 7
Joined: 10 Apr 2012 19:17

Re: redirection >&1 >&2 >&3 >&4

#25 Post by alexsander.albani » 31 Mar 2015 07:04

Hello.
I need to do a simple stuff, but I'm having difficulties...

How can I display a command result both at screen and in a text file at the same time?

For example, a dir command displayed at screen and logged in a text file in only one execution.
Is that possible?

Thanks.

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

Re: Asynchronous native batch tee script

#26 Post by foxidrive » 31 Mar 2015 21:08

See the top of the first post viewtopic.php?p=32561#p32561

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: redirection >&1 >&2 >&3 >&4

#27 Post by Aacini » 01 Apr 2015 10:49

alexsander.albani wrote:Hello.
I need to do a simple stuff, but I'm having difficulties...

How can I display a command result both at screen and in a text file at the same time?

For example, a dir command displayed at screen and logged in a text file in only one execution.
Is that possible?

Thanks.


There is a working Tee.bat program at this post.

Antonio

alexsander.albani
Posts: 7
Joined: 10 Apr 2012 19:17

Re: Asynchronous native batch tee script

#28 Post by alexsander.albani » 01 Apr 2015 14:26

Thank you Antonio and foxidrive.
I'm going to try it. :D

Post Reply