@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