Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Batch File: String manipulation

Posted on 2011-09-27
7
Medium Priority
?
563 Views
Last Modified: 2012-05-12
Hi there,

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

Open in new window

0
Comment
Question by:ReneGe
7 Comments
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 2000 total points
ID: 36711127
A lot of those characters are very special and are messing everything up.
First, in order to even get the special characters in Title to test you need to use ^ to escape them and get them in. Then you need to handle the ! first since it will kill parts of the string. At the same time, add quotes around the string so nothing else messes it up and remove them last.

Like this:
@ECHO OFF

SETLOCAL enabledelayedexpansion

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:"=%
echo %Title%
SET Var=\,:,/


FOR %%A IN (%Var%) DO SET Title=!Title:%%A=!

ECHO %Title%
PAUSE
EXIT

Open in new window

0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 36711131
Oops. Line 12 is not needed and is a stray debugging line.
0
 
LVL 10

Author Comment

by:ReneGe
ID: 36711240
Thanks a million Tommy

Cheers,
Rene
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 59

Expert Comment

by:Bill Prew
ID: 36716756
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%

Open in new window

~bp
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 36716864
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.
0
 
LVL 10

Author Comment

by:ReneGe
ID: 36717013
Thanks bp and Tommy :)

Cheers
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36812954
A slightly improved solution would have been:

@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%

Open in new window


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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question