gpt4 book ai didi

batch-file - 使用延迟变量值作为子字符串中的长度值

转载 作者:行者123 更新时间:2023-12-03 18:18:08 25 4
gpt4 key购买 nike

我正在从日志文件中提取笔记集。通过 FIND/V/N 找到文件中一组标题的行号注释集由仅包含“@@@”的行分隔我使用 findstr 返回此行,它返回为“xxx:@@@”,例如'14:@@@' 或 '149:@@@' 等等。

在下面的代码片段中,进入FOR/IN 循环的变量的典型数据是

  • _ndelm = "@@@" ~ 标记注释结束的分隔符
  • _infil = "notes.log" 保存数据集的文件
  • _atpos = 笔记标题的行位置

在执行期间,enabledelayedexpansion 循环中的局部变量包含以下需要 !..! 的临时值在处理的时刻:

  • strel=如上findstr返回的字符串分隔符行
  • numel=从strel=='xxx'中提取的行号,一般1-4个字符长
  • end = 将成为​​分隔符行的(整数)行号

在此代码片段中,需要 IF 语句来解决我的问题:我找不到在单个 中使用 !end! 的方法SET 语句,因此解决方法是 4 个 IF。:

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
)

理想情况下,我想编写这样的代码

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

我到处寻找类似的用例,但无济于事,并根据经验尝试了 % 和 ! 的任意组合。

如果有人知道我可以使用的语法,那就太好了:-)

谢谢

文件摘录:

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.
@@@

最佳答案

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

诀窍是使用 for 循环参数,因为在执行延迟扩展之前扩展参数。

关于batch-file - 使用延迟变量值作为子字符串中的长度值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59703305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com