I wrote a script which goes through each character of an input string and depending on the character I need to do different things. This works quite well as long as my input doesn't contain any white space or double-quote character. I know that I have to escape special characters, but for some reason it seems like I do it wrong for space and double-quote.
If I run the batch with the argument "ab cd", a and b are processed correctly and then the script stops on the white space with the error message :"(SET was unexpected at this time)". This seems to refer to:
ECHO char: %char%
if %char%==0 (SET file=0.wav)
Which has not thrown this error before. But with %char% set to as white space this line seems to be problematic. The scheme showing how everything should work together is:
console input: myScript.bat "ab cd" -> mytext = "ab cd" -> (loop) char = "a" ->char = "b" -> char = " " -> "(SET was unexpected at this time)".
See a more complete version (without loop) of the code below.
:: get input (allow quoted inputs like "ab cd")
SET mytext=%~1
:: get first character from input
SET char=%mytext:~0,1%
:: works fine
if %char%==1 (...do something...)
if %char%==2 (...do something...)
if %char%==a (...do something...)
if %char%==b (...do something...)
::..also special characters work fine (some need escape sequence)
if %char%==^( (...do something...)
if %char%==^) (...do something...)
if %char%==: (...do something...)
if %char%==- (...do something...)
if %char%==+ (...do something...)
if %char%==$ (...do something...)
if %char%==. (...do something...)
if %char%==^! (...do something...)
if %char%==^' (...do something...)