ReneGe
asked on
Batch File: String manipulation
Hi there,
I want to remove the following caracters from a variable \ : | < > & ^ " ! /.
Thanks for fixing my script,
Rene
I want to remove the following caracters from a variable \ : | < > & ^ " ! /.
Thanks for fixing my script,
Rene
@ECHO OFF
SETLOCAL enabledelayedexpansion
SET Title=T\h:i|s< >i&s ^a t"e!s/t:
SET Var=\,:,|,<,>,&,^,",!,/
FOR %%A IN (%Var%) DO SET Title=!Title:%%A=!
ECHO %Title%
PAUSE
EXIT
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Oops. Line 12 is not needed and is a stray debugging line.
ASKER
Thanks a million Tommy
Cheers,
Rene
Cheers,
Rene
FWIW, this will also work:
SET Title=T\h:i^|s^< ^>i^&s ^^a t"e^!\s/t:
Set "Title=%Title:!=%"
Set "Title=%Title:|=%"
Set "Title=%Title:>=%"
Set "Title=%Title:<=%"
Set "Title=%Title:&=%"
Set "Title=%Title:\=%"
Set "Title=%Title::=%"
Set "Title=%Title:/=%"
Set "Title=%Title:"=%"
ECHO %Title%
~bp
Of course.
The idea of using the FOR loop was that it was easier to add more characters later. The only problem was that the FOR loop cannot be used on some of the special characters.
The idea of using the FOR loop was that it was easier to add more characters later. The only problem was that the FOR loop cannot be used on some of the special characters.
ASKER
Thanks bp and Tommy :)
Cheers
Cheers
A slightly improved solution would have been:
But I forgot to post this comment yesterday!
As you can see, there is no need to delay expansion of variables, no need to a FOR-loop and no need to precede 'special' characters with a ceret (^) - all that is merely required is to wrap your whole character string in double-quotes.
The only other thing to note is the order in which to substitute out characters - especially the first few.
@echo off
set title="T\h:i|s< >i&s ^a t"e!s/t:"
echo %title%
set title="%title:"=%"
set title=%title:>=%
set title=%title:<=%
set title=%title:|=%
set title=%title:^=%
set title=%title:&=%
set title=%title:\=%
set title=%title::=%
set title=%title:!=%
set title=%title:/=%
set title=%title:"=%
echo %title%
But I forgot to post this comment yesterday!
As you can see, there is no need to delay expansion of variables, no need to a FOR-loop and no need to precede 'special' characters with a ceret (^) - all that is merely required is to wrap your whole character string in double-quotes.
The only other thing to note is the order in which to substitute out characters - especially the first few.