Mihir
Mihir

Reputation: 557

Parse string using batch script

How can i parse string using batch script?

Goal is to save in a array everything under Import: and strip out "#head" for example --> "//MPackages/Project/config/abc.txt" and "//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

MY try

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

)

Upvotes: 2

Views: 185

Answers (2)

rojo
rojo

Reputation: 24466

This is what I had in mind:

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

Upvotes: 1

npocmaka
npocmaka

Reputation: 57252

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

try this.It should set the desired variables in numbered list like _1_ ; _2_ ; ...

Upvotes: 2

Related Questions