Tech Support > Operating Systems > MS-DOS > Remove leading & trailing spaces from variables...
Remove leading & trailing spaces from variables...
Posted by H. Rodriguez on June 20th, 2008


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.


Posted by billious on June 20th, 2008



"H. Rodriguez" <hrodriguez@hotmail.com> wrote in message
news:qMCdnfveZI8Af8bVnZ2dnUVZ_hadnZ2d@giganews.com ...
I believe that
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.



Posted by H. Rodriguez on June 20th, 2008


Well,

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

Thanks!

-H.

"billious" <billious_1954@hotmail.com> wrote in message
news:485beb21$0$20832$a82e2bb9@reader.athenanews.c om...
Yeah, what you said is what I meant.



Posted by Timo Salmi on June 20th, 2008


H. Rodriguez <hrodriguez@hotmail.com> wrote:
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:ts@uwasa.fi <http://www.uwasa.fi/~ts/> ; FI-65101, Finland
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.htm

Posted by Ted Davis on June 21st, 2008


On Fri, 20 Jun 2008 13:03:09 -0400, H. Rodriguez wrote:

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. (tdavis@mst.edu) MST (Missouri University of Science and Technology)
used to be UMR (University of Missouri - Rolla).




Similar Posts