I know how to do this when the variable is pre-defined. However, when asking for the user to enter in some kind of input, how do I trim leading and trailing whitespace? This is what I have so far:
@echo off
set /p input=:
echo. The input is %input% before
::trim left whitespace
for /f "tokens=* delims= " %%a in ("%input%") do set input=%%a
::trim right whitespace (up to 100 spaces at the end)
for /l %%a in (1,1,100) do if "!input:~-1!"==" " set input=!input:~0,-1!
echo. The input is %input% after
pause
set newVarNoSpaces=%someVarWithSpaces: =%
The
:trimAll
subroutine below:!
,%
,^
, etc.)Read comments for references and info.
HTH and HNY! ;)
You need to enable delayed expansion. Try this:
You can also strip whitespace by passing in those variables with whitespace as parameters into a sub and then inside your sub simply set them again with the passed in parameters.
The solution below works very well for me.
Only 4 lines and works with most (all?) characters.
Solution:
Test:
from http://www.netikka.net/tsneti/info/tscmd079.htm
for removing leading spaces.