@echo off
set str = This string has a lot of spaces
echo %str%
set str=%str:=%
echo %str%
@echo off
setlocal EnableDelayedExpansion
set S= This is a test
echo %S%.
for /f "tokens=* delims= " %%a in ('echo %S%') do (set b=%%a & set b=!b: =_! & echo !b!)
endlocal & goto :EOF
:Trim
SetLocal EnableDelayedExpansion
set Params=%*
for /f "tokens=1*" %%a in ("!Params!") do EndLocal & set %1=%%b
exit /b
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET /p NAME=- NAME ?
ECHO "%NAME%"
CALL :TRIM NAME
ECHO "%NAME%"
GOTO :EOF
:TRIM
SetLocal EnableDelayedExpansion
Call :TRIMSUB %%%1%%
EndLocal & set %1=%tempvar%
GOTO :EOF
:TRIMSUB
set tempvar=%*
GOTO :EOF