Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Remove leading & trailing spaces from variables...

17,726 views
Skip to first unread message

H. Rodriguez

unread,
Jun 20, 2008, 1:03:09 PM6/20/08
to
Hello, Folks!

I am trying to find a scheme that will remove leading and trailing
spaces from an environmental variable for a batch file. I have checked out
some tutorial web sites but their schemes do not seem to work.

Specifically, I'm reading the filename of a database file to a temporary
file and reading that into a variable. The trouble is the filename produced
by the query still has leading and trailing spaces. I can't seem to get rid
of them from the database server side and the only option I see is to get
rid of them in the batch file. Using VBScript is **not** an option for me.
I can only use batch files.

So, here's what I'm talking about:

1) I get the filename from this query and I filter it using the FIND command
then I save the output (just one line) to a file (src.txt in this case).

osql -E -S localhost -d master -Q "SELECT filename FROM sysdatabases WHERE
name='%1'" | FIND /I "MYBASE" > src.txt

2) Then I try to read that one lined text file into a variable where I do
some other stuff with it like generate the name of the transaction log file.

SET /P src=< src.txt
SET srclog=%src:~0,-4%_log.ldf

As stated above, %src% has leading and trailing spaces which messes
things up. I have tried the following to get rid of those extraneous spaces
but they don't seem to work:

FOR /F "tokens=* delims= " %%a IN ("%src%") DO SET src=%%a (trailing)
FOR /L %%a IN (1,1,32) DO IF "!src:~-1!"==" " SET src=!src:~0:-1! (leading)

The number "32" represents the maximum number of leading spaces that can
be removed.

So, what am I doing wrong!?!?!

TIA...

-H.


billious

unread,
Jun 20, 2008, 1:33:44 PM6/20/08
to

"H. Rodriguez" <hrodr...@hotmail.com> wrote in message
news:qMCdnfveZI8Af8bV...@giganews.com...

I believe that


> FOR /F "tokens=* delims= " %%a IN ("%src%") DO SET src=%%a (trailing)

will strip the LEADING spaces, not the trailing - if that is what you mean.


This demo developed using XP
It may work for NT4/2K

----- batch begins -------
[1]@echo off
[2]set yss= many spaces first
[3]:: ensure that Your Source String has 20 trailing spaces
[4]set yss=%yss%%yss:~0,20%
[5]echo +%yss%+
[6]call :strip %yss%
[7]echo +%yss%+
[8]goto :eof
[9]
[10]:strip
[11]set yss=%*
[12]goto :eof
[13]
------ batch ends --------

Lines start [number] - any lines not starting [number] have been wrapped and
should be rejoined. The [number] that starts the line should be removed

The label :eof is defined in NT+ to be end-of-file but MUST be expressed as
:eof

Note that I'm bracketing the display of Your Source String with "+" for
visibility reasons.
* This is almost guaranteed to fail if the string contains certain
characters - "&" and "%" for instance. This may or may not be a problem.


H. Rodriguez

unread,
Jun 20, 2008, 2:26:24 PM6/20/08
to
Well,

I don't know how it worked but it work!

Thanks!

-H.

"billious" <billio...@hotmail.com> wrote in message
news:485beb21$0$20832$a82e...@reader.athenanews.com...


>
> I believe that
>> FOR /F "tokens=* delims= " %%a IN ("%src%") DO SET src=%%a (trailing)
>
> will strip the LEADING spaces, not the trailing - if that is what you
> mean.

Yeah, what you said is what I meant.

Timo Salmi

unread,
Jun 20, 2008, 2:42:19 PM6/20/08
to
H. Rodriguez <hrodr...@hotmail.com> wrote:
> I am trying to find a scheme that will remove leading and trailing
> spaces from an environmental variable for a batch file.

For your probable OS covered e.g. in
79} How can I trim leading and trailing spaces?
http://www.netikka.net/tsneti/info/tscmd079.htm

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FI-65101, Finland
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.htm

Ted Davis

unread,
Jun 20, 2008, 8:49:50 PM6/20/08
to
On Fri, 20 Jun 2008 13:03:09 -0400, H. Rodriguez wrote:

> Hello, Folks!
>
> I am trying to find a scheme that will remove leading and trailing
> spaces from an environmental variable for a batch file. I have checked
> out some tutorial web sites but their schemes do not seem to work.
>
> Specifically, I'm reading the filename of a database file to a
> temporary
> file and reading that into a variable. The trouble is the filename
> produced by the query still has leading and trailing spaces. I can't seem
> to get rid of them from the database server side and the only option I see
> is to get rid of them in the batch file. Using VBScript is **not** an
> option for me. I can only use batch files.
>
> So, here's what I'm talking about:
>
> 1) I get the filename from this query and I filter it using the FIND
> command then I save the output (just one line) to a file (src.txt in this
> case).
>
> osql -E -S localhost -d master -Q "SELECT filename FROM sysdatabases WHERE
> name='%1'" | FIND /I "MYBASE" > src.txt
>
> 2) Then I try to read that one lined text file into a variable where I do
> some other stuff with it like generate the name of the transaction log
> file.
>
> SET /P src=< src.txt
> SET srclog=%src:~0,-4%_log.ldf

Screen dump - use %% instead of % in a batch file - src is set to " foo "

c:\myfiles>set src= foo

c:\myfiles>echo -%src%-
- foo -

c:\myfiles>for %A in (%src%) do echo -%A-

c:\myfiles>echo -foo-
-foo-

c:\myfiles>

Obviously, you wouldn't need the "-" - they are there to show where
the spaces are.

--

T.E.D. (tda...@mst.edu) MST (Missouri University of Science and Technology)
used to be UMR (University of Missouri - Rolla).

ryanoasis

unread,
Jul 20, 2008, 1:21:58 PM7/20/08
to

Hey thats nice and a simpler solution! Thank you , you solved my
backup problems when i dated my files for deletion.

However how exactly does just having it go through a for loop remove
the spaces? I have read some of the FOR documentation
but I do not get why THAT which you have removes the spaces.

Thanks!

Richard Bonner

unread,
Jul 21, 2008, 8:23:26 AM7/21/08
to
H. Rodriguez (hrodr...@hotmail.com) wrote:
> I am trying to find a scheme that will remove leading and trailing
> spaces from an environmental variable for a batch file. I have checked out
> some tutorial web sites but their schemes do not seem to work.

> Specifically, I'm reading the filename of a database file to a temporary
> file and reading that into a variable. The trouble is the filename produced
> by the query still has leading and trailing spaces. I can't seem to get rid
> of them from the database server side and the only option I see is to get
> rid of them in the batch file. Using VBScript is **not** an option for me.
> I can only use batch files.

(Snip)

> -H.

*** DOS users may employ XSET to pipe a file name into a variable and
use its "/SEPARATOR" switch to remove white space.


One or more links to websites for the referred program(s) can be found
at:
http://www.chebucto.ca/~ak621/DOS/Websites.html


Richard Bonner
http://www.chebucto.ca/~ak621/DOS/

Tom Lavedas

unread,
Jul 21, 2008, 10:26:55 AM7/21/08
to

Spaces are delimiters that the command processor parses out, unless
they are contained within paired double quotes.

Note that to assure there are no spaces in the processed result, use
something like this ((for NT variants) ...

for %A in (%src%) do (set src=%src%)

In non-NT OS variants, you just need to be absolutely certain that the
FOR has no trailing spaces.

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/

bke...@keadle.net

unread,
Oct 22, 2011, 7:17:53 AM10/22/11
to
This works for me:

set MYVAR= Variable with spaces
echo [%MYVAR%]
echo.
for /f "tokens=* delims= " %%A in ('echo %MYVAR% ') do set MYVAR=%%A
set MYVAR=%MYVAR:~0,-1%
echo [%MYVAR%]

NOTE: you must "force" at least on trailing space in 'echo %MYVAR% '

Timo Salmi

unread,
Oct 22, 2011, 11:22:39 AM10/22/11
to
On 22.10.2011 14:17 bke...@keadle.net wrote:
> This works for me:
> set MYVAR= Variable with spaces
> echo [%MYVAR%]
> echo.
> for /f "tokens=* delims= " %%A in ('echo %MYVAR% ') do set MYVAR=%%A
> set MYVAR=%MYVAR:~0,-1%
> echo [%MYVAR%]

Yes it does. Note that it also deletes multiple spacings. Try with
set MYVAR= Variable with spaces

All the best, Timo

--
Prof. (emer.) Timo Salmi, Vaasa, Finland
http://www.netikka.net/tsneti/homepage.php
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.php

Konrad

unread,
Oct 24, 2011, 12:22:45 PM10/24/11
to
Hi,
this one works for both leading and trailing spaces but also replaces
multiple spaces by one space within the string:

set MYVAR= Variable with spaces &set DummyForSpacesAtTheEnd=x
echo [%MYVAR%]
for /f "delims=" %%A in ('echo %MYVAR%') do call :Sub1 %%A
echo [%MYVAR%]
exit /b 0

:Sub1
set MYVAR=%*
exit /b 0

Regards
Konrad


<bke...@keadle.net> schrieb im Newsbeitrag
news:8591403.955.1319282274063.JavaMail.geo-discussion-forums@yqjh13...

Konrad K

unread,
Oct 24, 2011, 12:38:19 PM10/24/11
to
This one also works for multiple spaces (at least in Win XP):

...
for /f "delims=" %%A in ('echo "%MYVAR%"') do call :Sub1 %%~A
...

Regards
Konrad



"Konrad" <x...@arcor.de> schrieb im Newsbeitrag
news:4ea591a9$0$6638$9b4e...@newsspool2.arcor-online.net...

Konrad K

unread,
Oct 24, 2011, 12:40:38 PM10/24/11
to
This one also works for multiple spaces (at least in Win XP):

...
for /f "delims=" %%A in ('echo "%MYVAR%"') do call :Sub1 %%~A
...

Regards
Konrad



"Konrad" <x...@arcor.de> schrieb im Newsbeitrag
news:4ea591a9$0$6638$9b4e...@newsspool2.arcor-online.net...

foxidrive

unread,
Oct 24, 2011, 12:40:57 PM10/24/11
to
This seems to work well at keeping spaces in the string.

@echo off
set "MYVAR= Variable with spaces "
echo [%MYVAR%]
for /f "delims=" %%A in ("%MYVAR%") do call :Sub1 %%A
echo [%MYVAR%]
pause
goto :EOF

:Sub1
set MYVAR=%*




On 25/10/2011 03:22, Konrad wrote:
> Hi,
> this one works for both leading and trailing spaces but also replaces
> multiple spaces by one space within the string:
>
> set MYVAR= Variable with spaces&set DummyForSpacesAtTheEnd=x
> echo [%MYVAR%]
> for /f "delims=" %%A in ('echo %MYVAR%') do call :Sub1 %%A
> echo [%MYVAR%]
> exit /b 0
>
> :Sub1
> set MYVAR=%*
> exit /b 0
>
> Regards
> Konrad
>
>
> <bke...@keadle.net> schrieb im Newsbeitrag
> news:8591403.955.1319282274063.JavaMail.geo-discussion-forums@yqjh13...
>> This works for me:
>>
>> set MYVAR= Variable with spaces
>> echo [%MYVAR%]
>> echo.
>> for /f "tokens=* delims= " %%A in ('echo %MYVAR% ') do set MYVAR=%%A
>> set MYVAR=%MYVAR:~0,-1%
>> echo [%MYVAR%]
>>
>> NOTE: you must "force" at least on trailing space in 'echo %MYVAR% '
>
>


--
Regards,
Mic

terren...@gmail.com

unread,
Apr 28, 2013, 3:34:48 AM4/28/13
to
Thanks! This worked perfectly for me as well.

larry...@gmail.com

unread,
Apr 8, 2016, 2:11:26 PM4/8/16
to
Konrad's solution of using a subroutine worked great for what i needed. I had to trim trailing space and save everything to a file, so i added a few other lines:

:Sub1
SET MYVAR=%*
SET MYVAR=%MYVAR:~0,-1%
ECHO "%MYVAR%" >> output2.txt
EXIT /b 0
0 new messages