2

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...)
Chris Cooper
  • 4,982
  • 1
  • 17
  • 27
Marcus
  • 450
  • 4
  • 14

2 Answers2

1
@echo off
    setlocal enableextensions disabledelayedexpansion

    rem Value to test
    set "myText=1 <> () & !"" "

:loop    
    rem Quote it to avoid problems with special characters.
    rem Closing quote is not included in value
    SET "char=%mytext:~0,1%"

    rem Test if we have something to test
    if not defined char goto :done

    rem Test first problematic characters that need escape
    if ^%char%==^" echo quote           & goto :next
    if ^%char%==^& echo ampersand       & goto :next
    if ^%char%==^> echo greater than    & goto :next
    if ^%char%==^< echo lower than      & goto :next
    if ^%char%==^^ echo caret           & goto :next
    if ^%char%==^( echo open bracket    & goto :next
    if ^%char%==^) echo close bracket   & goto :next

    %= ... =%

    rem Test for spaces 
    if "%char%"==" " echo space         & goto :next

    rem Test the rest of the options
    if %char%==1 echo one               & goto :next
    if %char%==! echo exclamation       & goto :next

    %= ... =%

    rem Once done, go to next character

:next
    set "myText=%myText:~1%"
    if defined myText goto :loop

:done
    exit /b
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Hi MC ND, thank you for this very good commented example code. This helped me to get my task done. The quotes around variables made this thing working. Thanks again – Marcus Oct 24 '14 at 12:04
0

You need to make sure there is clearly something to evaluate, even when there is nothing or only white-space to evaluate, so try this:

if [%char%]==[] (...do something...)
if [%char%]==[0] (...do something...)
if [%char%]==[1] (...do something...)
etc...
Chris Cooper
  • 4,982
  • 1
  • 17
  • 27