如何批量删除所有符号?

时间:2013-01-09 18:41:49

标签: batch-file

所以,假设我有这个变量%s%,我必须取出所有符号,因为它们会与代码相互关联..这些符号包括:

〜`@#$%^&安培; *()_- + = {[}] | \ :;“'<!?,> /

所以基本上每个符号我都能想到打字,我应该怎么做呢?以下代码是一个巨大的失败

SET s=!s:"^~"=""!
SET s=!s:"`"=""!
SET s=!s:"^!"=""!
SET s=!s:"@"=""!
SET s=!s:"#"=""!
SET s=!s:"^$"=""!
SET s=!s:"^%"=""!
SET s=!s:"^^"=""!
SET s=!s:"&"=""!
SET s=!s:"*"=""!
SET s=!s:"("=""!
SET s=!s:")"=""!
SET s=!s:"-"=""!
SET s=!s:"_"=""!
SET s=!s:"+"=""!
SET s=!s:"="=""!
SET s=!s:"{"=""!
SET s=!s:"}"=""!
SET s=!s:"["=""!
SET s=!s:"]"=""!
SET s=!s:"^|"=""!
SET s=!s:"\"=""!
SET s=!s:":"=""!
SET s=!s:";"=""!
SET s=!s:"'"=""!
SET s=!s:'"'=""!
SET s=!s:"<"=""!
SET s=!s:","=""!
SET s=!s:">"=""!
SET s=!s:"."=""!
SET s=!s:"?"=""!
SET s=!s:"/"=""!

我在互联网上搜索过,没有发现任何想法?

3 个答案:

答案 0 :(得分:2)

第一步可以是这个,它从badChars列表和感叹号中删除所有字符。
但是字符~*=不能以这种方式替换。

set "str=Hello,;:$%%!&<>x"
setlocal EnableDelayedExpansion

set "badChars=`@#$%%^&()_-+{[}]|\:;'<,>.?/""
for /L %%n in (0,1,30) do (
  for /f "eol=A delims=" %%C in ("!badChars:~%%n,1!") DO (
    if defined str set "str=!str:%%C=!"
  )
)
set "str=%str:!=%"
echo(!str!

答案 1 :(得分:2)

我自己没有测试过这段代码,但已经将它作为书签了一段时间。

请参阅此帖子Post #7

中的http://forums.techguy.org/dos-other/823979-solved-removing-special-characters-loop.html
:_GetName
SET /P folder=Folder Name:
Set folder="%folder:"=%"
Setlocal EnableDelayedExpansion
For %%I In (^| ^& ^< ^> ^^ + ^( ^) \ / . @ # $ { } [ ] ' : ` ^%% ^") Do Set folder=!folder:%%I=!
:: Now remove any !
SetLocal DisableDelayedExpansion
Set folder="%folder:!=%"
EndLocal&Set folder=%folder:~1,-1%
:_parse
Set _Flag1=
For /F "Tokens=1* Delims=~=*;,?" %%J In ('Echo !folder!') Do (
    Set folder=%%J%%K
    Set _Flag1=%%J
    Set _Flag2=%%K
)
If NOT "%_Flag2%"=="" Goto _parse
If Not Defined _Flag1 Echo None of the characters you entered are valid. Please try again&Goto _GetName
:: Now revert to previous settings. The Set is needed to pass the
:: Folder variable back to the previous environment
EndLocal&Set folder=%folder%
Echo You said your folder name will be %folder%.
@pause

答案 2 :(得分:1)

修改
如果满足以下所有条件,我的第一次尝试失败:

  • :StripSymbols在启用延迟扩展时被调用
  • 字符串很长,需要多次迭代
  • 该字符串在第一次迭代后至少有2 !个非符号

第一次尝试通常很快,但是对于需要多次迭代的病理案例,可能会大大减慢。

@echo off
setlocal disableDelayedExpansion

set "test1=!Hello`@#$%%^&()_-+{[}]|\:;'<,>.?/"~*= World!"
call :test test1

set "test2="
call :test test2

set "test3=`@#$%%^&()_-+{[}]|\:;'<,>.?/"~*=!"
call :test test3

set "test4=,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,0,1,2,3,4,5,6,7,8,9,"
call :test test4

exit /b

:test StrVar
setlocal enableDelayedExpansion
echo Before: %~1=!%~1!
call :stripSymbols %1
echo  After: %~1=!%~1!
echo(
exit /b


:stripSymbols  StrVar
setlocal enableDelayedExpansion
for /f tokens^=1-25*^ delims^=`@#$%%^^^&()_-+{[}]^|\:^;'^<^,^>.?/~*^=!^" %%A in ("a,!%~1!") do (
  endlocal
  set "%~1=%%B%%C%%D%%E%%F%%G%%H%%I%%J%%K%%L%%M%%N%%O%%P%%Q%%R%%S%%T%%U%%V%%W%%X%%Y%%Z"
  if "%%Z" neq "" goto :stripSymbols
)
exit /b

以下是测试结果。注意test5是如何失败的。

Before: test1=!Hello`@#$%^&()_-+{[}]|\:;'<,>.?/"~*= World!
 After: test1=Hello World

Before: test2=
 After: test2=

Before: test3=`@#$%^&()_-+{[}]|\:;'<,>.?/"~*=!
 After: test3=

Before: test4=,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,0,1,2,3,4,5,6,7,8,9,
 After: test4=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789

Before: test5=,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z!0,1,2,3,4,5,6,7,8,9!
 After: test5=ABCDEFGHIJKLMNOPQRSTUVWXYZ


这是我的防弹第二次尝试

第二次尝试应该总是很快,而且它也应该是防弹

@echo off
setlocal disableDelayedExpansion

set "test1=!Hello`@#$%%^&()_-+{[}]|\:;'<,>.?/"~*= World!"
call :test test1

set "test2="
call :test test2

set "test3=`@#$%%^&()_-+{[}]|\:;'<,>.?/"~*=!"
call :test test3

set "test4=,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,0,1,2,3,4,5,6,7,8,9,"
call :test test4

set "test5=,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z!0,1,2,3,4,5,6,7,8,9!"
call :test test5

exit /b

:test StrVar
setlocal enableDelayedExpansion
echo Before: %~1=!%~1!
call :stripSymbols %1
echo  After: %~1=!%~1!
echo(
exit /b


:stripSymbols  StrVar
setlocal enableDelayedExpansion
set "str=!%~1!"
setlocal disableDelayedExpansion
for /l %%N in (1 1 171) do (
  setlocal enableDelayedExpansion
  for /f tokens^=1-25*^ delims^=`@#$%%^^^&(^)_-+{[}]^|\:^;'^<^,^>.?/~*^=!^" %%A in ("a,!str!") do (
    endlocal
    set "str=%%B%%C%%D%%E%%F%%G%%H%%I%%J%%K%%L%%M%%N%%O%%P%%Q%%R%%S%%T%%U%%V%%W%%X%%Y%%Z"
    if "%%Z" equ "" goto :break
  )
)
:break
endlocal & endlocal & set "%~1=%str%"
exit /b

以下是更正后的结果

Before: test1=!Hello`@#$%^&()_-+{[}]|\:;'<,>.?/"~*= World!
 After: test1=Hello World

Before: test2=
 After: test2=

Before: test3=`@#$%^&()_-+{[}]|\:;'<,>.?/"~*=!
 After: test3=

Before: test4=,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,0,1,2,3,4,5,6,7,8,9,
 After: test4=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789

Before: test5=,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z!0,1,2,3,4,5,6,7,8,9!
 After: test5=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789