1

I have a batch file that reads in a line of text that I can escape however is necessary.

I need to deploy this file to a small user base, so installing sed on everyone's PC isn't an option.

So I've been trying to do this with a batch file.

Input:

TAG1 GRfoo XY ^(as generated by Boolean^) minimum enclosed area ^(um2^) ^> ^= 0.10

... that input is eventually parsed in this line of code:

for /f "usebackq tokens=2*" %%i in (`echo %*`) DO (

... where %* is the argument passed through a CALL: call:retTokenThreePlus !tagline!

I have tried seemingly every combination of escaping the >: ^> ^^^>, ^^^^^>, etc. Etc.

I can't seem to figure out how to not have the > disappear from what is passed to the :retTokenThreePlus method.

Thanks!

Batch file relevant code:

IF "%3"=="" (
  :: This monstrosity sets tagline to the FINDSTR result.
  for /f "usebackq tokens=*" %%a in (`FINDSTR /C:"TAG%2 " %tagfile%`) do set tagline=%%a
  call:retTokenTwo !tagline!
  set tagrule=!token!
  echo !tagline!
  call:retTokenThreePlus !tagline!
  set CMDLINE=!CMDLINE! !tagrule! !token!
  goto :write
)

:: Method to return only the second token of the parm list passed to it
:retTokenTwo
  set token=%2
  echo TOKEN2 %token%
  goto :eof

:: Method to return tokens 3+ of the parm list passed to it
:retTokenThreePlus
  REM setlocal enabledelayedexpansion
  for /f "usebackq tokens=2*" %%i in (`echo %*`) DO (
    set "token=%%j"
  )
  REM echo TOKEN3 !token!
  goto :eof

Output with echo on:

C:\gf28hpp\layout\icwinlp>set CMDLINE=$ 

C:\gf28hpp\layout\icwinlp>set token="" 

C:\gf28hpp\layout\icwinlp>IF "" == "" (
for /F "usebackq tokens=*" %a in (`FINDSTR /C:"TAG1 " block_checkdrc.tags`) do set tagline=%a  
 call:retTokenTwo !tagline!  
 set tagrule=!token!  
 echo !tagline!  
 call:retTokenThreePlus !!tagline!!  
 set CMDLINE=!CMDLINE! !tagrule! !token!  
 goto :write 
)  ELSE (
 ...[redacted]... 
) )  
) 

C:\gf28hpp\layout\icwinlp>set tagline=TAG1 GRfoo XY ^(as generated by Boolean^) minimum enclosed area ^(um2^) ^> ^= 0.10 

C:\gf28hpp\layout\icwinlp>set token=GRfoo 

C:\gf28hpp\layout\icwinlp>echo TOKEN2 GRfoo 
TOKEN2 GRfoo

C:\gf28hpp\layout\icwinlp>goto :eof 
TAG1 GRfoo XY ^(as generated by Boolean^) minimum enclosed area ^(um2^) ^> ^= 0.10

C:\gf28hpp\layout\icwinlp>REM setlocal enabledelayedexpansion 

C:\gf28hpp\layout\icwinlp>for /F "usebackq tokens=2*" %i in (`echo TAG1 GRfoo XY (as generated by Boolean) minimum enclosed area (um2) `) DO (set "token=%j" ) 

C:\gf28hpp\layout\icwinlp>(set "token=XY (as generated by Boolean) minimum enclosed area (um2) " ) 

C:\gf28hpp\layout\icwinlp>REM echo TOKEN3 !token! 

C:\gf28hpp\layout\icwinlp>goto :eof
stevesliva
  • 5,351
  • 1
  • 16
  • 39
  • Did you try writing the value with the `>` char to a file and then reading it from there? – Jason Faulkner Dec 04 '14 at 18:56
  • @JasonFaulkner - no. Part of me insists that's unnecessary. Playing with it more, it's may be the CALL which is losing the <, and it's getting treated as a line continuation symbol somewhere. I'm leaning towards punting and letting my handful of users learn that { and } mean > and < in these strings. But I also do not understand why the CALL is not passing through %* arguments with an escaped ^<, and I'd like to. – stevesliva Dec 04 '14 at 19:00
  • Better solution since I can edit the input being parsed... ***Add double-quotes around the whole input string that contains < or >.*** And forget about any batch wildcards in there. Though it passes through to my output. Oh well. – stevesliva Dec 04 '14 at 19:10
  • Maybe a [batch script heredoc](http://stackoverflow.com/a/15032476/1683264) would be of benefit? I apologize if that's not relevant. I've got one foot out the door and couldn't pay your question the attention it deserves. Toodles! – rojo Dec 04 '14 at 21:46
  • @rojo... it might. Nothing a recursive method can't solve I suppose. – stevesliva Dec 05 '14 at 00:23

0 Answers0