Trim Right

Trim spaces from the end of a string via "FOR" command.

Description: Trimming spaces at the end of a variable seems a little tricky. The following example shows how to use a FOR loop to trim up to 31 spaces from the end of a string. It assumes that Delayed Expansion is enabled.
Script:
1.
2.
3.
4.
set str=15 Trailing Spaces to truncate               &rem
echo."%str%"
for /l %%a in (1,1,31) do if "!str:~-1!"==" " set str=!str:~0,-1!
echo."%str%"
Script Output:
 DOS Script Output
"15 Trailing Spaces to truncate               "
"15 Trailing Spaces to truncate"