breaking a "for" loop with conditions

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

breaking a "for" loop with conditions

#1 Post by Rileyh » 01 Nov 2011 21:51

Hi,
I have the code:

Code: Select all

@echo off
setlocal disableDelayedExpansion
set file=test.txt"
set "search=test"
for /f "delims=" %%A in ('findstr /i /c:"%search%" %file%') do (
  set "ln=%%A"
  echo %search%
  endlocal
)
if not defined _"%ln%" (goto :error)
:BreakFOR ()
::(
   for /l %%! in (

      1, 1, *

  ) do (
      %= break the loop if condition met =%
      if 1 equ 0 goto :BreakFOR "{}"
   )
::)
:BreakFOR {}
pause


This code is supposed to search for a string in a text file and do something if the string is found.
The next part is supposed to break the for string after the string has been found and the action carried out. I plan to have another occurrence of this code directly after this one, so once the loop has been broken the batch file will go on to the next occurrence of this code.

Now, all this code does not work. It just flashes cmd and says "the syntax of the command is incorrect".

Could you help me please,
Regards,
Rileyh

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

Re: breaking a "for" loop with conditions

#2 Post by Ed Dyreen » 01 Nov 2011 22:42

'
Look at my previous post in the other thread please, you open 2 threads on the same topic, why :?:

As I said predefined loop.
for /l %%! in (

1, 1, *

) do (

Code: Select all

for /l %%! in (

   1, 1, 99

) do (


for /? Rileyh for /?

Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Re: breaking a "for" loop with conditions

#3 Post by Rileyh » 02 Nov 2011 00:44

I wouldn't ask another question if I knew the answer.
I checked the "for /f" and all it says is:
for /l %%variable in (start,step,end) do command (command-parameters)
The set is a sequence of numbers from start to end, by step amount. So
(1,1,5) would generate the sequence 1,2,3,4,5 and (5,-1,1) would generate the
sequence 5,4,3,2,1


It does not say about breaking a for loop and continuing the batch file. And before you post about "google it" I checked google and it only has websites on the FOR command in which the posts were almost exact copies of the /? version.
That is why I started another thread.
In the new thread I ask about HOW TO IMPLEMENT the code you posted, not what it does.
So please answer me like that.

Regards (and no offence intended)
Rileyh

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

Re: breaking a "for" loop with conditions

#4 Post by Ed Dyreen » 02 Nov 2011 01:07

'
This is how you could do it:

Code: Select all

@echo off SetLocal EnableExtensions EnableDelayedExpansion

set   "$file=test.txt"
set  "$match=hello"
set "$string="
::
for %%? in (

   "!$match!"

) do    for /f "usebackq tokens=*" %%! in (

   "!$file!"

) do (
   set "$t=%%~!"
   set "$compare=!$t:%%~?=!"
   if /i ["!$compare!"] neq ["!$t!"] (
      ::
      set "$string=!$t:*%%~?=!"
      echo.$string=!$string!_
      goto :BreakFor "()"
   )
)
:BreakFor ()
pause
exit /b 0
Breakfor gives ugly code and it's not always faster, next example is a little cleaner but slower when the input file gets larger:

Code: Select all

@echo off SetLocal EnableExtensions EnableDelayedExpansion

set   "$file=test.txt"
set  "$match=hello"
set "$string="
::
for %%? in (

   "!$match!"

) do    for /f "usebackq tokens=*" %%! in (

   "!$file!"

) do if not defined $string (
   set "$t=%%~!"
   set "$compare=!$t:%%~?=!"
   if /i ["!$compare!"] neq ["!$t!"] (
      ::
      set "$string=!$t:*%%~?=!"
      echo.$string=!$string!_
   )
)

pause
exit /b 0

Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Re: breaking a "for" loop with conditions

#5 Post by Rileyh » 02 Nov 2011 18:40

So do I just copy and paste this code into my batch file and it will do what I want?
Sorry, but I don't have barely any idea on how to implement this code.
But thank you (I think :)) for the code. I have a feeling that this will be helpful.

Regards,
Rileyh

Post Reply