如何使用批处理脚本解析字符串?
目标是将数据保存在导入:下的所有内容中并删除“#head” 例如 - > “//MPackages/Project/config/abc.txt”和“//Packages/Project/config/cde.txt”
的test.txt
Version: 4.5.0
Import:
//MPackages/Project/config/abc.txt #head
//Packages/Project/config/cde.txt #head
View:
//MPackages/Project/config/ac.txt #head
//Packages/Project/config/de.txt #head
我的尝试
@echo off
set buildlog="devel.p4inc"
setlocal EnableDelayedExpansion
for /F "tokens=*" %%A in (devel.p4inc) do (
if /i "%%A"=="Import:" set "import=true"
IF DEFINED import (echo %%A)
)
答案 0 :(得分:2)
@echo off
set "vf=version.txt"
setlocal enableDelayedExpansion
set counter=1
for /f "usebackq tokens=1 delims=#" %%a in ("%vf%") do (
set "line=%%a"
if "!line:View=!" neq "!line!" if "!in!" equ "true" (
set in=false
rem echo ###
)
if "!in!" equ "true" (
set "_!counter!_=%%a"
set /a counter=counter+1
)
rem echo !line!
if "!line:Import=!" neq "!line!" (
set in=true
rem echo --
)
)
set _
试试这个。它应该在编号列表中设置所需的变量,如_1_
; _2_
; ...
答案 1 :(得分:1)
这就是我的想法:
@echo off
setlocal EnableDelayedExpansion
set "buildlog=devel.p4inc"
set idx=0
for /F "usebackq" %%A in ("%buildlog%") do (
if defined import (
set "config=%%A"
if "!config:~0,2!"=="//" (
set "config[!idx!]=%%A"
set /a idx += 1
) else set "import="
) else if /i "%%A"=="Import:" set "import=true"
)
rem // display config array
set config[