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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sincos2007
Posts: 44
Joined: 05 Apr 2019 05:52

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

#1 Post by sincos2007 » 04 May 2019 00:56

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

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

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

#2 Post by Aacini » 04 May 2019 10:50

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

sincos2007
Posts: 44
Joined: 05 Apr 2019 05:52

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

#3 Post by sincos2007 » 07 May 2019 11:43

Hi Antonio,

Thanks for your help, your code works well.

Post Reply