Trim Right

Trim spaces from the end of a string via substitution.

Description: Trimming spaces at the end of a variable seems a little tricky. The following example shows how to use the string substitution feature to trim up to 31 spaces from the end of a string. It assumes that the string to be trimmed never contains two hash "##" characters in a row.
Script:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
set str=15 Trailing Spaces to truncate               &rem
echo."%str%"
set str=%str%##
set str=%str:                ##=##%
set str=%str:        ##=##%
set str=%str:    ##=##%
set str=%str:  ##=##%
set str=%str: ##=##%
set str=%str:##=%
echo."%str%"
Script Output:
 DOS Script Output
"15 Trailing Spaces to truncate               "
"15 Trailing Spaces to truncate"