Page 1 of 1

set and white spaces

Posted: 21 May 2011 14:34
by tebee
Hi,
i know white spaces are used as string delimiters, but how to avoid the last byte in environment variable equal to space?
Ex.:
ECHO ON & SETLOCAL EnableDelayedExpansion & SETLOCAL EnableExtensions

FOR /F "delims= " %%G in ('DIR /S /B ^| FINDSTR ".ex1 .ex2 .ex3 .ex4"') DO (
SET TT=%%G & SET TT=!TT:~0,-4! & ECHO.%%Gs!TT!.EXTe
)
EXIT /B

gives always [file.ex_]s[filename] .EXTe

Notice that TT=!TT:~0,-4! is same as TT=%%~nG

Thank you

Re: set and white spaces

Posted: 21 May 2011 15:17
by dbenham
It is very easy to banish those pesky unwanted spaces by using quotes! Note how the opening quote is before the variable name.

Code: Select all

SET "TT=%%G" & SET "TT=!TT:~0,-4!" & ECHO.%%Gs!TT!.EXTe


It is an interesting trick that not just spaces are ignored after the ending quote:

Code: Select all

set "var=value" stuff after the last quote is ignored

But you probably don't want to get in the habit of doing something like the above.

Dave Benham