Page 1 of 1

For Question

Posted: 24 May 2011 16:19
by Cleptography
I have a question about the for command and depending on how deep it goes why does it require to be closed off differently.

Example 1 Structure Works:

Code: Select all

for
   call
      for
         if
            call
               for
                  for
                     if
                        echo.
                           del
                              del
                     ) else (
                        echo.
                           del
                              del
))))))

...but if I try to close it all off on separate lines it requires six more braces

Example 2 Structure Works:

Code: Select all

for
   call
      for
         if
            call
               for
                  for
                     if
                        echo.
                           del
                              del
                        ) else (
                           echo.
                              del
                                 del
                                 )
                              )
                           )
                        )
                     )
                  )
               )
            )
         )
      )
   )
)


Example 3 Structure Does Not Work:

Code: Select all

for
   call
      for
         if
            call
               for
                  for
                     if
                        echo.
                           del
                              del
                        ) else (
                           echo.
                              del
                                 del
                              )
                           )
                        )
                     )
                  )
               )

Re: For Question

Posted: 24 May 2011 19:34
by Ed Dyreen
can you show me the output ?

Re: For Question

Posted: 24 May 2011 20:21
by Cleptography
Thanks for getting back to me Ed.
I'm not sure which output you are asking for as in the script out or the error out, I will assume the latter of the two. So in example one which works fine except if I add )))))))))))) a typical )))))) was unexpected error is shown. In example three no script output occurs until I add the remaining 6 ) then the script outputs the data. I never noticed this behavior before I just always put the ) on one line because it looked cleaner to me until I ran into an error and was messing around with format did I discover this happening.

I will try and explain the script as best as possible without posting only because of what I use it for. Nothing malicious, just that it automates some downloads through a website which in any other case blocks automated responses so I had to write up a few programs to finally bypass what they were blocking.
So goes like this.

Code: Select all

for [searches through a text doc for information that I need]
   call [cpp program that downloads webpage]
      for [searches the html and grabs the data I need]
         if [the data matches what I need grab it] (
            call [the cpp app again to download another page]
               for [parse through that data and grab what I need]
                  for [reparse the above for again to get exactly whats needed]
                     if [unrelated condition is met append do the below] (
                        echo. [print what I want to see]
                           del [del downloaded file1]
                              del [del downloaded file2]
                     ) else (
                        echo. [print the same data but only if the unrelated condition was not met]
                           del [del downloaded file1]
                              del [del downloaded file2]
))))))

Why not just do it all in another language, simply because I like batch, and for no other reaseon.
So why )))))) works on one line and 12 ) are needed on separate lines is beyond my knowledge.
Truthfully it doesn't matter as the script I use works and does what I need it to, but non the less
it would be nice to understand what is going on.

Re: For Question

Posted: 24 May 2011 20:27
by Ed Dyreen

Code: Select all

for
   call
      for

Do you really expect that to work :?:

don't u mean
for %%a in ( hello there ) do echo.%%a%%b

Re: For Question

Posted: 24 May 2011 20:39
by Cleptography
Ed,
I updated the above post, yes I understand for call for alone does not work the rest of the code is there as it should be %%a in ("") etc... etc...
Thank You-

Re: For Question

Posted: 24 May 2011 21:00
by Ed Dyreen
I don't think we understand each other .

Code: Select all

@echo off
for %%? in ( hello ) do (
 echo.%%?
 if 1 equ 1 ( echo.>nul
 if 1 equ 1 ( echo.>nul
 if 1 equ 1 ( echo.>nul
 if 1 equ 1 ( echo.iffy
 for %%? in ( there ) do (
 echo.%%?
for %%? in ( h ) do (
 echo.%%?
 ))))
)))
pause
exit /b

Maybe it's not for that is having problems

Re: For Question

Posted: 24 May 2011 21:49
by Cleptography
Thanks for the reply again Ed. I guess it's not really even a problem. Just curious why 6 )'s on a single line make the script work but 6 )'s one on each line does not work, why it is needed to have to add 12 )'s in total when specifying them on separate lines.

Re: For Question

Posted: 24 May 2011 22:13
by Ed Dyreen
that is a mistery to me too :|

Re: For Question

Posted: 25 May 2011 00:20
by jeb
Hi Cleptography,

do you can show a small sample, as I can't reproduce your problem.

Code: Select all

setlocal
set var=orig
for %%a in (1) do (
   set var=change
   for %%b in (2) do (
      echo %%a %%b
   )
)
echo %var%


Code: Select all

setlocal
set var=orig
for %%a in (1) do (
   set var=change
   for %%b in (2) do (
      echo %%a %%b
))
echo %var%

Both variants seems to be works the same way

jeb

Re: For Question

Posted: 25 May 2011 01:42
by orange_batch
Crushing this down because I'm not used to indenting batch lol...

Code: Select all

for (
call
for (
if (
call
for (
for (
if (
echo.
del
del
) else (
echo.
del
del
))))))

I don't see what the problem would be?

How about a working example.

Code: Select all

for /f %%a in ("1") do (
call echo:First.
for /f %%b in ("2") do (
if %%a NEQ %%b (
call echo:Second. %%a is not equal to %%b.
for /f %%c in ("3") do (
for /f %%d in ("4") do (
if %%c NEQ %%d (
echo:Third. %%c is not equal to %%d.
echo:del some file.
echo:del some file.
) else (
echo:Fourth. %%c is equal to %%d.
echo:del some file.
echo:del some file.
))))))

Change %%c between 3 and 4 to trigger the else.

You have a syntax error somewhere, most likely.