For loop value with spaces

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

For loop value with spaces

#1 Post by SIMMS7400 » 16 Sep 2024 17:20

Hi All -

I am reaching out to see if there is a cleaner way to prepare a passed value for use in a for loop. For instance, I'm passing this value to a function :
CALL :TEST "ACR_US Bank_Regions,AR_EFT"
However, I need to iterate over both values, but space is considered a delimiter. So I am doing the following :

Code: Select all

SET "AUTOMATCH=!AUTOMATCH:, =,!"   & REM Ensure no spaces after comma
SET "AUTOMATCH=!AUTOMATCH: =@!"    & REM Temporarily replace spaces with @
SET AUTOMATCH="!AUTOMATCH:,=","!"  & REM Wrap double quotes around each DLR
SET "AUTOMATCH=!AUTOMATCH:@= !"    & REM Remove @ and replace with spac
Then using it like this :

Code: Select all

FOR %%A IN (!AUTOMATCH!,= !) DO ECHO %%A
Is there a cleaner way?

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

Re: For loop value with spaces

#2 Post by SIMMS7400 » 17 Sep 2024 03:36

I looks like I can just do this, so simple :

Code: Select all

SET "AUTOMATCH=%~1"

Code: Select all

FOR %%A IN ("%AUTOMATCH:,=" "%") DO (

Post Reply