Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a delayed variable value to as a length value in a substring

Tags:

batch-file

I am extracting note sets from a log file. The line number of the header to a set in the file is found via FIND /V /N The note set is delimited with a line containing only "@@@" I use findstr to return this line which returns as 'xxx:@@@', e.g. '14:@@@' or '149:@@@' and so on.

in the snippet of code below, typical data for the variables on entry into the FOR/IN loop would be

  • _ndelm = "@@@" ~ the delimiter marking the end of the note
  • _infil = "notes.log" the file holding the dataset
  • _atpos = the line position of the note header

During execution the local variables in the enabledelayedexpansion loop hold the following temporary values requiring !..! in the moment of processing:

  • strel= the string delimiter line returned by findstr as above
  • numel= the extracted line number from strel=='xxx', generally 1-4 chars long
  • end = will become the (integer) line number of the delimiter line

In this code snippet the IF statements are needed to get around my problem: I can't find a way to use !end! in a single SET statement, so the workaround is 4 IFs.:

SET _ndelm=@@@
SET /a _atpos=1
FOR /f "usebackq tokens=1 delims=[]" %%i IN (`FINDSTR /b /l /n /c:%_ndelm% %_infil% `) DO (
SET strel=%%i
CALL stringlen %%i end & rem gets the character length of the delimiter line]
SET /a end-=4
IF [!end!] EQU [1] (SET numel=!strel:~0,1!)
IF [!end!] EQU [2] (SET numel=!strel:~0,2!)
IF [!end!] EQU [3] (SET numel=!strel:~0,3!)
IF [!end!] EQU [4] (SET numel=!strel:~0,4!)
SET /a end=!numel!
IF !end! GEQ %_atpos% GOTO fetchlines
)

Ideally, I would like to code something like this

(SET numel=!strel:~0,!end!!)
SET /a end=!numel!

I've hunted high and low to find a similar use case, but to no avail, and tried any number of combinations of %'s and !'s empirically.

If anyone has a syntax I could use ~ that would be great :-)

Thanks

file extract from the file:

11/01/2020 16:05:29.87 *** DONE *** All Batches [LEFiles needing /r]
 find all batches that should not be run, apply the /r switch and amend 
 accordingly. Start at root and work down the main line before tackling the 
 side branches.
  ~ Main *** done ***
  ~ libs *** done ***
  ~ rest *** done ***
  ~
@@@
11/01/2020 15:18:47.02 >>> open >>> LEfiles runit [add a 'don't run!' switch]
 some batches just can't be -or shouldn't be- run from the help system.
 code a don't run switch [/r]=don't run
@@@
09/01/2020 10:12:52.83 *** DONE *** strcharrepl [bug] abend on "*" to " "
 error message '= % was unexpected at this time.' after returning from a call to
 nameFiledatetime which itself crashes on spaces - workaround for that needed
 Suspect it's the same problem - returning a string with spaces is the generic
 issue that needs a solution.
 Maybe always return with quotes and then strip those? 
 ~soln [strcharrepl] not much that can be done because redirection chars
  always act in re-dir ways. Added notes to provide guidance in use.
 ~soln [zFN] surround filename with quotes and replace spaces with backslashes
  before call, replace with spaces after.
@@@
like image 235
ilinkcs Avatar asked Apr 20 '26 05:04

ilinkcs


1 Answers

For %%e in (!end!) Do set numel=!strel:~0,%%e!

The trick is to use a for loop parameter, because the parameter is expanded before the delayed expansion will be executed.

like image 102
jeb Avatar answered Apr 22 '26 23:04

jeb