The "trim" and "trimRight" work correctly if and only if the variable passed in is the same as the one used in your function:
Code: Select all
set s= trim or trimRight this line &call:trimRight s&echo."!s!"
Your Function:
Code: Select all
:trimRight -- trim trailing spaces
: -- %~1: variable reference, string to be trimmed
SETLOCAL
:trimLeft_LOOP
set s=!%~1!
if "%s:~-1%"==" " set s=%s:~0,-1%&goto:trimLeft_LOOP
(ENDLOCAL & REM.-- RETURN VALUES
IF "%~1" NEQ "" SET %~1=%s%
)
GOTO:EOF
Above works correctly, however, entering the following will result in an endless loop.
Code: Select all
set thisVar= trim or trimRight this line &call:trimRight thisVar&echo."!thisVar!"
:: OR
set thisVar= trim or trimRight this line &call:trim thisVar&echo."!thisVar!"
In the function, set s=!%~1! must be assigned before :trimLeft_LOOP
The Modified Function:
Code: Select all
:trimRight -- trim trailing spaces
: -- %~1: variable reference, string to be trimmed
SETLOCAL
set s=!%~1! << Moved to here, before the loop label.
:trimLeft_LOOP
if "%s:~-1%"==" " set s=%s:~0,-1%&goto:trimLeft_LOOP
(ENDLOCAL & REM.-- RETURN VALUES
IF "%~1" NEQ "" SET %~1=%s%
)
GOTO:EOF
The function will now work as I'm sure you originally intended.
Glen