why does this FOR statement output %f and this one doesn't?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
taripo
Posts: 228
Joined: 01 Aug 2011 13:48

why does this FOR statement output %f and this one doesn't?

#1 Post by taripo » 09 Aug 2011 18:47

x2.bat outputs %f, and x3.bat doesn't. Why?

C:\>type x2.bat <ENTER>
for %%f in (a b) do (
echo %%f
echo tw
)

C:\>x2 <ENTER>

C:\>for %f in (a b) do (
echo %f
echo tw
)

C:\>(
echo a
echo tw
)
a
tw

C:\>(
echo b
echo tw
)
b
tw

C:\>



C:\>type x3.bat <ENTER>
for %%f in (a b) do (
echo %%f
)
C:\>x3 <ENTER>

C:\>for %f in (a b) do (echo %f )

C:\>(echo a )
a

C:\>(echo b )
b

C:\>

---

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

Re: why does this FOR statement output %f and this one doesn

#2 Post by dbenham » 09 Aug 2011 22:49

:? :?: Both outputs look fine to me. They each echo a and b which corresponds to %f.

Dave Benham

taripo
Posts: 228
Joined: 01 Aug 2011 13:48

Re: why does this FOR statement output %f and this one doesn

#3 Post by taripo » 09 Aug 2011 23:25

dbenham wrote::? :?: Both outputs look fine to me. They each echo a and b which corresponds to %f.

Dave Benham


x2.bat has an extra iteration that x3.bat doesn't have, look at x2.bat's output before the iteration that echos 'a'. I'd like to know what's happening there. Why that is.

taripo
Posts: 228
Joined: 01 Aug 2011 13:48

Re: why does this FOR statement output %f and this one doesn

#4 Post by taripo » 09 Aug 2011 23:30

oh my mistake, I mixed up display of execution of the for, with the output of the for..

I only made a one line difference between x2.bat and x3.bat (without echo tw) and the output looked a bit different 'cos cmd put the ( ) on one line for x3.bat . and I got a bit confused with the pattern.. sorry.

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

Re: why does this FOR statement output %f and this one doesn

#5 Post by dbenham » 09 Aug 2011 23:39

I still don't see the difference, apart from the obvious absence of "echo tw".

Here are the outputs side by side

Code: Select all

C:\>x2 <ENTER>            |   C:\>x3 <ENTER>
                          |
C:\>for %f in (a b) do (  |   C:\>for %f in (a b) do (echo %f )
echo %f                   |
echo tw                   |
)                         |
                          |
C:\>(                     |   C:\>(echo a )
echo a                    |   a
echo tw                   |
)                         |
a                         |
tw                        |
                          |
C:\>(                     |   C:\>(echo b )
echo b                    |   b
echo tw                   |
)                         |
b                         |
tw                        |
                          |
C:\>                      |   C:\>


Your last post came while I was editing my post

Dave Benham

Post Reply