问题描述
我写了一个脚本,经过输入字符串的每个字符,并根据我需要做不同的事情的字符。这工作得很好,只要我输入不包含任何空格或双引号字符。我知道我有逃避特殊字符,但由于某种原因,它似乎像我这样做是错的空间和双引号。
如果我运行批处理的说法AB CD,A和B都正确处理,然后脚本将停止与错误消息的空白:(SET是在这个时候意外)。这似乎是指:
ECHO焦炭:焦炭%%
如果字符%%== 0(SET文件= 0.wav)
这之前没有抛出这个错误。但随着焦炭%%的设定为空白此行似乎是有问题的。展示如何一切都应该一起工作的方案是:
控制台输入:myScript.batAB CD - > MYTEXT =AB CD - >(循环)CHAR =A - >字符=B - >字符= - >(集显在这个时候意外)。
请参阅下面的code更完整的版本(不循环)。
::获取输入(允许引用输入,比如AB CD)
SET MYTEXT =%〜1::获得输入第一个字符
SET字符=%MYTEXT:〜0.1%
::工作正常
如果%的木炭%== 1(...做某事)
如果焦炭%==%2(...做某事)
如果焦炭%==%一(...做某事)
如果焦炭%==%B(...做某事):: ..也特殊字符做工精细(有的需要转义序列)
如果焦炭%==%^((...做某事)
如果焦炭%==%^)(...做一些事情...)
如果字符%==%(...做某事)
如果%的木炭%== - (...做某事)
如果焦炭%==%+(...做某事)
如果焦炭%==%$(...做某事)
如果焦炭%==%。 (...做一点事...)
如果焦炭%==%^! (...做一点事...)
如果焦炭%==%^'(...做某事)
关闭@echo
SETLOCAL ENABLEEXTENSIONS disabledelayedexpansion REM值来测试
设置会将myText = 1&所述;>()及!,:循环
REM报价,以避免有特殊字符的问题。
REM收盘报价中不包含的价值
设置字符=%MYTEXT:〜0.1% REM测试,如果我们有一些测试
如果没有定义字符转到:完成 REM测试需要先越狱存在问题的字符
如果^%字符%== ^回声报价&放大器;转到:下一个
如果^%字符%== ^&安培;呼应符号&安培;转到:下一个
如果^%字符%== ^>回声比&放越大;转到:下一个
如果^%字符%== ^<回声比&安培低;转到:下一个
如果^%字符%== ^^回声插入符号&安培;转到:下一个
如果^%字符%== ^(回声开括号和放大器;转到:下一个
如果^%字符%== ^)密切呼应托架和放大器;转到:下一个 %= ... =% REM试验空间
如果%字符%==回声空间和放大器;转到:下一个 REM测试的其余选项
如果%的木炭%== 1呼应的One&安培;转到:下一个
如果焦炭%==%!回声感叹和放大器;转到:下一个 %= ... =% REM完成后,进入下一个字符:下一个
设置=会将myText会将myText%:〜1%。
如果会将myText定义转到:循环:完成
退出/ B
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...)
@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
这篇关于过去,逃避,承认在Windows批处理文件特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!