For Question

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

For Question

#1 Post by Cleptography » 24 May 2011 16:19

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
                              )
                           )
                        )
                     )
                  )
               )

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

Re: For Question

#2 Post by Ed Dyreen » 24 May 2011 19:34

can you show me the output ?

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: For Question

#3 Post by Cleptography » 24 May 2011 20:21

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.
Last edited by Cleptography on 24 May 2011 20:37, edited 2 times in total.

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

Re: For Question

#4 Post by Ed Dyreen » 24 May 2011 20:27

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

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: For Question

#5 Post by Cleptography » 24 May 2011 20:39

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-

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

Re: For Question

#6 Post by Ed Dyreen » 24 May 2011 21:00

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

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: For Question

#7 Post by Cleptography » 24 May 2011 21:49

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.

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

Re: For Question

#8 Post by Ed Dyreen » 24 May 2011 22:13

that is a mistery to me too :|

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

Re: For Question

#9 Post by jeb » 25 May 2011 00:20

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

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: For Question

#10 Post by orange_batch » 25 May 2011 01:42

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.

Post Reply