Format a string inside For...Loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Thor
Posts: 43
Joined: 31 Mar 2016 15:02

Format a string inside For...Loop

#1 Post by Thor » 20 Apr 2017 01:22

Hi all,

I have the following FOR /F Loop and I would like to format the SET command based on a known value of "count" variable.
For example, inside the FOR /F Loop:
- If count value = 1 the set command will look like: "set c1=%%A"
- If count value = 2 the set command will look like: "set c1=%%A &set c2=%%B"
- If count value = 3 the set command will look like: "set c1=%%A &set c2=%%B &set c3=%%C"
etc.

Code: Select all

@echo off
set "inputTokens=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26"
set count=3
for /f "tokens=1-26* delims=," %%A in (%inputTokens%) do (
  if "%count%" equ "1" (set c1=%%A)
  if "%count%" equ "2" (set c1=%%A &set c2=%%B)
  if "%count%" equ "3" (set c1=%%A &set c2=%%B &set c3=%%C)
  if "%count%" equ "4" (set c1=%%A &set c2=%%B &set c3=%%C &set c4=%%D)
  if "%count%" equ "5" (set c1=%%A &set c2=%%B &set c3=%%C &set c4=%%D &set c5=%%E)
  ...
)

I wonder if there is any shorter way to format the set command instead of listing all 26 possibilities for it.

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Format a string inside For...Loop

#2 Post by SIMMS7400 » 20 Apr 2017 01:37

Ensure you wrap your set commands in quotes to prevent the errant white space at the end of:

Code: Select all

set c1=%%A &set c2=%%B


Should be

Code: Select all

set "c1=%%A" & set "c2=%%B"

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Format a string inside For...Loop

#3 Post by penpen » 20 Apr 2017 03:41

There is a shorter way to do your task - you even don't need the "for/f" loop - just use the string replacement functionality to build the needed command:

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion
set "inputTokens=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26"
set count=3
echo on

set/A"i=1"&if !i! leq !count! set "c!i!=%inputTokens:,="&set/A"i+=1"&if !i! leq !count! set "c!i!=%"

@echo off
set c
endlocal
goto :eof
Note: The environment variable "i" is used as a counter.

penpen

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

Re: Format a string inside For...Loop

#4 Post by Aacini » 20 Apr 2017 05:15

I think your code have an error. If the purpose of the FOR /F command is that %%A be 1, %%B be 2, etc. then the %inputTokens% expansion should be enclosed in quotes this way:

Code: Select all

for /f "tokens=1-26* delims=," %%A in ("%inputTokens%") do (

If this is true, then you may do the same process in this simpler way:

Code: Select all

for /L %%i in (1,1,%count%) do set "c%%i=%%i"

However, if inputTokens string may have any other tokens (not the numbers from 1 to 26), then the code below do the same task:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set "inputTokens=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26"
set count=3

for /L %%i in (1,1,%count%) do (
   for /F "tokens=1* delims=," %%A in ("!inputTokens!") do (
      set "c%%i=%%A"
      set "inputTokens=%%B"
   )
)

Note that this code works with any number of tokens, but the inputTokens variable is deleted in the FOR, so a copy of it must be done before.

Antonio

Thor
Posts: 43
Joined: 31 Mar 2016 15:02

Re: Format a string inside For...Loop

#5 Post by Thor » 20 Apr 2017 08:54

SIMMS7400: Thanks for tips of putting double quotes.

penpen and Aacini: Thanks both for your input. Your codes all works fine and as expected.

Post Reply