TurdPile
TurdPile

Reputation: 1036

How to format find-replace variables inside for loop

I have this script:

setlocal EnableDelayedExpansion
set SECTION=dispid 3
set TARGET=pushscope
@echo on
for /F "tokens=1* delims=:" %%a in ('findstr /N /C:"%SECTION%" /C:"%TARGET%" %CHECKER%') do (
    if not defined StartSection (
        if not %%b==%%b:%SECTION%=% set StartSection=%%a
    ) else (
    goto :EOF
        if not defined TargetLine (
            goto :EOF :: Do nothing yet
            if not %%b equ !%%b:%TARGET%=! set TargetLine=%%a& goto checkercont
        )
    )
)

In StartSection (the problem area), I'm trying to do a find and replace to confirm that this line is the one I desire, and save that line number.

How do I write it so that it will parse the variables correctly and do the find and replace properly?

What I've tried. "[%b]" means the replaced %%b:

%%b:%SECTION%=% returns "[%b]:dispid 3=a" (Yes, it somehow gives me an 'a').
!%b:%SECTION%=! returns "!SECTIONa"
!%%b:%SECTION%=! returns "![%b]:dispid 3=!" (which then gives me an error saying 3 is not a command)
!%b:%SECTION%=%! returns "!SECTION!"
!%%b:!SECTION!=! returns "![%b]:!SECTION!=!"
%b:%%SECTION%%=% return "dispid 3" (and error)

I feel like for the last one that it parses the bold then the italics. %b:%%SECTION%%=% And using ! never seems to accomplish anything except display at least one back to me.

And yes, after a little bit I just started throwing random crap in there (got frustrated).

So what's the proper way to get this working?

Thanks

Upvotes: 0

Views: 106

Answers (3)

Endoro
Endoro

Reputation: 37569

try this:

@ECHO OFF &SETLOCAL
for /F "delims=:" %%a in ('findstr /N /C:"%SECTION%" "%CHECKER%"') do if not DEFINED StartSection set "StartSection=%%a"
IF NOT DEFINED StartSection (ECHO StartSection %section% NOT found&goto:eof)
for /F "delims=:" %%a in ('MORE +%StartSection% "%CHECKER%" ^|findstr /N /C:"%TARGET%"') do if not DEFINED TargetLine set "TargetLine=%%a"
ECHO StartSection at line #%StartSection%
IF NOT DEFINED TargetLine (ECHO TargetLine %target% NOT found) ELSE ECHO TargetLine at line #%TargetLine%

Upvotes: 3

Aacini
Aacini

Reputation: 67216

Excuse me. I think I don't understand what you tried to achieve with this line:

if not %%b==%%b:%SECTION%=% set StartSection=%%a

You must realize that %%b contains a line read from the file, NOT a variable! Let's analyze a little this example:

!%%b:%SECTION%=!

If %%b would contain the name of a variable, then the previous line would eliminate the value of SECTION from it. However, %%b does NOT contain the name of a variable, but a line read from a file!

If you want to check if the line read from a file contain the string given by SECTION, then you should do that this way:

setlocal EnableDelayedExpansion
set SECTION=dispid 3
set TARGET=pushscope
@echo on
for /F "tokens=1* delims=:" %%a in ('findstr /N /C:"%SECTION%" /C:"%TARGET%" %CHECKER%') do (
    if not defined StartSection (
        set "line=%%b"
        if not "!line!" == "!line:%SECTION%=!" set StartSection=%%a
    ) else (
. . .

Upvotes: 1

Magoo
Magoo

Reputation: 80023

The hardest type of question to answer on SO is the "This doesn't work - how do I fix it?" question - where the only clue to what is required is a construct that the OP admits isn't working... Divining quite what is required quite wears out the ol' crystal ball - and repair costs nowadays really eats into the bats' wings and lizards' tongues budget...

I really can't figure out what the obsession with double-inversion is all about, either. Why if not condition thingtwo else thingone ?? What's wrong with if condition thingone else thingtwo? Maybe I'm just old-fashioned - but perhaps it's politically incorrect or dicriminatory in some absurd way.

So - in the case under examination, assuming the desired result is to determine the linenumber of the first dispid 3 and that of the line containing pushscope that appears after the dispid 3 in the file %checker% (of which no sample is provided...)

@ECHO OFF
SETLOCAL
set SECTION=dispid 3
set TARGET=pushscope
SET checker=sourcefile.txt
SET startsection=
SET targetline=
for /F "tokens=1* delims=:" %%a in ('findstr /N /C:"%SECTION%" /C:"%TARGET%" %CHECKER%') do (
 IF DEFINED startsection (
  IF NOT DEFINED targetline (
    ECHO %%b|FIND "%target%">NUL
    IF NOT ERRORLEVEL 1 SET targetline=%%a
  )
 ) ELSE (
  ECHO %%b|FIND "%section%">NUL
  IF NOT ERRORLEVEL 1 SET startsection=%%a
 )
)

ECHO startsection=%startsection% targetline=%targetline%

GOTO :EOF

Which worked for me. YMMV.

Upvotes: 0

Related Questions