PLEASE HELP ME!

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

PLEASE HELP ME!

#1 Post by Rileyh » 31 Oct 2011 23:10

Here is my issue:
I have a batch file with many repeats of this code:

Code: Select all

setlocal disableDelayedExpansion
set file="%b%.txt"
set "search=(randomtextstring)"
for /f "delims=" %%A in ('findstr /i /c:"%search%" %file%') do (
  set "ln=%%A"
  (somethingrandom)
  endlocal
)
if not defined _"%ln%" (goto :error)
NOTE- the variable %b% is predefined as a filepath

Now, I need it to stop after each occurrence of this code, do something and go onto the next occurrence of this code. Then, at the bottom of the batch file it loops to the top. This I can do, but I can't do the stopping part.

Please help me, I really need it. (please don't think that I'm pestering this forum- just I am so bad at batch that I need lots of help :))

Thank you in advance,
Rileyh

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

Re: PLEASE HELP ME!

#2 Post by Ed Dyreen » 01 Nov 2011 00:36

'

Code: Select all

:BreakFOR ()
::(
   for /l %%! (

      1, 1, 99

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

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

Re: PLEASE HELP ME!

#3 Post by Rileyh » 01 Nov 2011 18:52

Thanks Ed but could you explain how this works in relation to my question?

Thanks again,
Rileyh

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

Re: PLEASE HELP ME!

#4 Post by Ed Dyreen » 01 Nov 2011 21:42

'
The code above is a standard construction for breaking out of a predefined loop.
Loops run based upon conditions that are met or not met.
You will have to fill in the gaps for me cause I have no clue of what you are attempting to achieve :?

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

Re: PLEASE HELP ME!

#5 Post by Rileyh » 01 Nov 2011 21:58

Ed,
I am trying to get this code to search for a text string in the file test.txt. When this code is executed, it searches for the string "hello" in test.txt. Then, it sets a variable to the characters that occur AFTER the string searched for. Then, if the string is found, it does something in response to the find returning true.
What I want to do is when the string is found, it does the something and breaks the loop. I plan to have more of the same code after this in the batch file, so that when the loop is broken the batch file goes onto the next block of this code. And at the bottom of the batch file I will have a goto that goes to the top and searches for a different piece of text.

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

Re: PLEASE HELP ME!

#6 Post by Ed Dyreen » 01 Nov 2011 22:31

'
fails upon use of an exclamation mark, consequence of using delayed !

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

pause
exit /b 0
Untested !

Post Reply