Problem with Data from Parentheses to be Piped to Findstr

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

Problem with Data from Parentheses to be Piped to Findstr

#1 Post by Meerkat » 25 Jan 2017 16:07

I have a sample code:

Code: Select all

@echo off
setlocal enabledelayedexpansion

set l=100
for /l %%. in (1,1,%l%) do (
    set /a "C_%%.=%%.*%%."
)

::First section: This works
(
for /l %%. in (1,1,%l%) do echo %%.
)|findstr /BC:"4"
pause

::Second section: This prints stuff
(
for /l %%. in (1,1,%l%) do echo !c_%%.!
)
pause

::Third section: This does NOT work
(
for /l %%. in (1,1,%l%) do echo !c_%%.!
)|findstr /BC:"4"
pause
From what I observed, the third section does not work because delayed variables are ECHOed and piped thru Findstr, but I don't see a connection. How can I fix that?

Meerkat

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Problem with Data from Parentheses to be Piped to Findstr

#2 Post by aGerman » 25 Jan 2017 16:31

You could run it in a separate cmd process. Give that a go:

Code: Select all

(
cmd /von /q /c "for /l %%. in (1,1,%l%) do echo !c_%%.!"
)|findstr /BC:"4"


Steffen

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

Re: Problem with Data from Parentheses to be Piped to Findstr

#3 Post by dbenham » 25 Jan 2017 17:26

Yes - aGerman has the solution.

The reason your code fails is because each side of the pipe is run in a new cmd session where delayed expansion defaults to off.

See http://stackoverflow.com/q/8192318/1012053 for more information.


Dave Benham

Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

Re: Problem with Data from Parentheses to be Piped to Findstr

#4 Post by Meerkat » 26 Jan 2017 04:19

aGerman wrote:

Code: Select all

(
cmd /von /q /c "for /l %%. in (1,1,%l%) do echo !c_%%.!"
)|findstr /BC:"4"


Steffen


Thanks! Just a followup question. Just in case, how can I put it in the IN clause of FOR loop?

Meerkat

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

Re: Problem with Data from Parentheses to be Piped to Findstr

#5 Post by jeb » 26 Jan 2017 05:05

One way is to use a cmd variable first.

Code: Select all

set "cmd=(cmd /von /q /c "for /l %%. in (1,1,%l%) do echo !c_%%.!")^| findstr /BC:"4""
setlocal EnableDelayedExpansion
for /F "delims=" %%X in ('!cmd!') do echo ## %%X

But I suppose, that it's much too complicated to use it this way.

This should work, too

Code: Select all

for /l %%. in (1,1,%l%) do (
   set "content=!c_%%.!"
   if "!content:~0,1!"=="4" echo ** !content!

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

Re: Problem with Data from Parentheses to be Piped to Findstr

#6 Post by pieh-ejdsch » 26 Jan 2017 07:55

command IF is parsing two times.
It also run without auxiliary variables:

Code: Select all

for /l %%. in (1,1,%l%) do if :!c_%%.:~0^,1!==:4 echo !c_%%.!
Phil

Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

Re: Problem with Data from Parentheses to be Piped to Findstr

#7 Post by Meerkat » 26 Jan 2017 09:34

Thanks for the answers! :D

Meerkat

Post Reply