Solved

Batch File: String manipulation

Posted on 2011-09-27
7
555 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 51

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to dynamically set the form action using jQuery.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now