how to remove trailing and leading whitespace for user-provided input in dos?

Posted by OopOop on Stack Overflow See other posts from Stack Overflow or by OopOop
Published on 2010-06-08T23:06:53Z Indexed on 2010/06/08 23:12 UTC
Read the original article Hit count: 210

Filed under:
|
|
|
|

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

Thank you! :)

© Stack Overflow or respective owner

Related posts about input

Related posts about dos