Solved

Batch File: String manipulation

Posted on 2011-09-27
7
557 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 500 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 53

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

839 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