For /f processing delimited string

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Blazer
Posts: 13
Joined: 20 Dec 2018 09:47

For /f processing delimited string

#1 Post by Blazer » 25 Dec 2018 07:28

for /f "delims=?" %A in ("123?456?789") do echo %A

This runs the loop once and sets %A to 123

I want it to loop for each substring based on the delimiter so that I can run a number of commands on %A

Loop 1 %A = 123
Loop 2 %A = 456
Loop 3 %A = 789

Thank you

IcarusLives
Posts: 112
Joined: 17 Jan 2016 23:55

Re: For /f processing delimited string

#2 Post by IcarusLives » 25 Dec 2018 09:24

You want something like this...?

Code: Select all

@echo off & setlocal enableDelayedExpansion


for /f "tokens=1-2 delims=?" %%a in ("echo?set") do (

	for /l %%c in (1,1,10) do (
		%%b /a "a[%%c]=!random! %% 2"
		%%a %%c = !a[%%c]!
	)
	
)
pause & exit
I couldn't manage to get commands like for and if to work in this instance.

Commands that use ( ) in their syntax seem to break even if escaping the closing ).

Hopefully this points you in the right direction

Blazer
Posts: 13
Joined: 20 Dec 2018 09:47

Re: For /f processing delimited string

#3 Post by Blazer » 26 Dec 2018 07:23

@IcarusLives

Thanks for your help. 8)

Aacini
Expert
Posts: 1596
Joined: 06 Dec 2011 22:15
Location: México City, México

Re: For /f processing delimited string

#4 Post by Aacini » 30 Dec 2018 09:34

If you have the target string in a variable, and the substrings have not ? * wild-card characters, then you may use this simple trick:

Code: Select all

set "var=123?456?789"
for %A in ("%var:?=" "%") do echo %~A
Antonio

Blazer
Posts: 13
Joined: 20 Dec 2018 09:47

Re: For /f processing delimited string

#5 Post by Blazer » 02 Jan 2019 05:02

@Aacini

Thank you, your batch-fu is strong 8)

Post Reply