There's a small bug in the routine. You must have enabled Delayed Expansion before calling the routine, or RESULT gets set to !RESULT:~1,-1! (the actual text, not the expanded variable).
This is because the Endlocal statement reverts Delayed Expansion to the state is was in before the SetLocal statement that enabled it in the TRIM routine. If it was disabled before calling TRIM, the delayed expansion of !RESULT:~1,-1! doesn't happen, and that is seen as a text string, not a variable.
Small bug
This is because the Endlocal statement reverts Delayed Expansion to the state is was in before the SetLocal statement that enabled it in the TRIM routine. If it was disabled before calling TRIM, the delayed expansion of !RESULT:~1,-1! doesn't happen, and that is seen as a text string, not a variable.