set and white spaces

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tebee
Posts: 13
Joined: 21 May 2011 08:41

set and white spaces

#1 Post by tebee » 21 May 2011 14:34

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: set and white spaces

#2 Post by dbenham » 21 May 2011 15:17

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

Post Reply