How to remove the char from environment variable

B

Blue Fish

Hello:

May I know how can I remove the last char or remove the last char if it
was space from the environment variable?

It was because I have found that the variable has space at the end.

Thanks a lot!
 
B

billious

Blue Fish said:
Hello:

May I know how can I remove the last char or remove the last char if it
was space from the environment variable?

It was because I have found that the variable has space at the end.

Thanks a lot!

set var=%var:~0,-1%
 
T

Tom Lavedas

set var=%var:~0,-1%

That does in fact remove the last character, but might introduce a new
problem in the instance where the variable does NOT end in a space.

Might I suggest a less succinct, but more robust approach that avoids
this problem ...

@echo off
set test=test case >nul
echo [%test%]
set test=%test%#$#
set test=%test: #$#=%
set test=%test:#$#=%
echo [%test%]

The first SET statement is just to create an example to test and the
two ECHO statements just illustrate the process. The working parts of
the solution are the three SETs after the first ECHO. The string #$#
is arbitrary, but constructed to be an unusual string that is unlikely
to be encountered in the target string. This solution, as was yours,
is applicable to Win NT derivative OSs and does not work with Win 9x
variants.

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

billious

Tom Lavedas said:
set var=%var:~0,-1%

That does in fact remove the last character, but might introduce a new
problem in the instance where the variable does NOT end in a space.

Might I suggest a less succinct, but more robust approach that avoids
this problem ...

@echo off
set test=test case >nul
echo [%test%]
set test=%test%#$#
set test=%test: #$#=%
set test=%test:#$#=%
echo [%test%]

The first SET statement is just to create an example to test and the
two ECHO statements just illustrate the process. The working parts of
the solution are the three SETs after the first ECHO. The string #$#
is arbitrary, but constructed to be an unusual string that is unlikely
to be encountered in the target string. This solution, as was yours,
is applicable to Win NT derivative OSs and does not work with Win 9x
variants.

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

I'd suggest there really is no universal foolproof solution.

If the test is required as well, then

if "%var:~-1%"==" " set var=%var:~0,-1%

should work, but will fail if the contents of VAR has a terminating
double-quote ('"') - or, I'd venture (without testing) the various other
characters that have a special meaning to batch - especially ><%!,;

Lacking further information, it's not really possible to go further.

CALL :sub %var%
....
goto :eof
....
:sub
set var=%1
goto :eof

would also work if VAR contains only the characters that have no special
meaning, for instance.
 
B

Blue Fish

Thanks for all information , all of them are quite useful for me.

Thanks a lot!
 
B

Blue Fish

Thanks for all information , all of them are quite useful for me.

Thanks a lot!
 
T

Timo Salmi

Blue Fish said:
May I know how can I remove the last char or remove the last char if it
was space from the environment variable?

79} How can I trim leading and trailing spaces?
202569 Dec 23 2007 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
Http link format http://garbo.uwasa.fi/pub/pc/link/tscmd.zip
tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi

All the best, Timo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top