BBMAN225
BBMAN225

Reputation: 673

How do I remove all symbols in batch?

So let's say I have this variable %s% and I have to take out all the symbols because they will interffere with the code.. These symbols include:

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

So basically every symbol I can think of typing, how should I go about doing this? The following code was a HUGE failure

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:"/"=""!

I've scoured the internet and found nothing, any ideas?

Upvotes: 1

Views: 150

Answers (3)

dbenham
dbenham

Reputation: 130919

EDIT
My first attempt fails if all of the following conditions are true:

  • :StripSymbols is CALLed while delayed expansion is enabled
  • The string is long and requires multiple iterations
  • The string has at least 2 ! remaining with non-symbols in between after the first iteration

This first attempt is generally fast, but can slow down considerably with pathalogical cases that require many iterations.

@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

Here are the test results. Note how test5 fails.

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


Here is my bullet proof 2nd attempt

This 2nd attempt should always be fast, and it should also be bullet proof

@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

And here are the corrected results

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

Upvotes: 1

jeb
jeb

Reputation: 82410

A first step can be this, it remove all chars from the badChars-list and also the exclamation mark.
But the characters ~*= can't be replaced this way.

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!

Upvotes: 2

David Ruhmann
David Ruhmann

Reputation: 11367

I have not tested this code myself, but have had this as a bookmark for some time.

See Post #7 from this thread 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

Upvotes: 2

Related Questions