2

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)

)
4
  • 1
    Have you tried a for /f loop, beginning capture of the first token of each line until the first 2 characters of token 1 is no longer //? Commented May 1, 2015 at 15:39
  • yes, but how would it know to only take from Import: and not from View:? Commented May 1, 2015 at 15:46
  • 1
    You should enable delayed expansion. if /i "%%I"=="Import:" set "import=true" and begin capturing on the next loop iteration. Then if defined import if not "!variable:~0,2!"=="//" goto next to break out of the loop. Show what you've tried so far and you'll get answers. Commented May 1, 2015 at 15:51
  • @rojo - thank you for quick replies.. please take a look at my original question.. i have added my try script.. but i'm still stuck.. Commented May 1, 2015 at 17:16

2 Answers 2

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 _

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

Sign up to request clarification or add additional context in comments.

Comments

1

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[

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.