Solved

Batch File: String manipulation

Posted on 2011-09-27
7
559 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 54

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

734 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