Page 1 of 1

How to preserve exact number of space characters after split by for

Posted: 04 May 2019 00:56
by sincos2007

Code: Select all

:test2
setlocal DisableDelayedExpansion

set "line1=   abc   #   %%var1%%   #   ^!var2^!"


for /f "usebackq delims=# tokens=1,2,3*" %%i in ('%line1%') do (
	echo {%%i}
	echo {%%j}
	echo {%%k}
)

endlocal
goto :eof
There are 3 space characters at both sides of “abc” in variable line1. But after I split line1 by for loop, only one space character is left.

How to preserve the three space characters at both sides of “abc” after split by for?

Thanks

Re: How to preserve exact number of space characters after split by for

Posted: 04 May 2019 10:50
by Aacini
Don't use usebackq switch and enclose the variable in quotes:

Code: Select all

@echo off

:test2
setlocal DisableDelayedExpansion

set "line1=   abc   #   %%var1%%   #   ^!var2^!"


for /f "delims=# tokens=1,2,3*" %%i in ("%line1%") do (
	echo {%%i}
	echo {%%j}
	echo {%%k}
)

endlocal
goto :eof
Antonio

Re: How to preserve exact number of space characters after split by for

Posted: 07 May 2019 11:43
by sincos2007
Hi Antonio,

Thanks for your help, your code works well.