Find and Replace Asterisk in Text File

I am trying to remove two characters in a tab delimited text file with the following batch code in Wiondows XP:

@echo off
del CleanedPuget_Sound_Formulary.txt
setLocal enabledelayedexpansion
for /f "tokens=* delims=" %%a in (Puget_Sound_Formulary.txt) do (
set tl=%%a
Set tl=!tl:"*"=!
Set tl=!tl:"=!
echo !tl!>>CleanedPuget_Sound_Formulary.txt )

The (") removal works but, I need to know how to remove the (*) Asterisk from the file.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hmm - you're looping through the file based on * as a delimeter - thus it'll not show up.
Either lose that token, or try something like munge (free resource kit tool)?
By the way, your code should work fine otherwise:

set tl=!tl:"*"=!
should remove "*" for you (replacing it with !).

Good luck!
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Please ignore both comments, they are not correct.

Token=* is not searching for the token "asterisk", it is a wildcard for "all tokens". There are no delimiters ("delims="). That combination leads to reading one complete line into a single var, preserving leading spaces (without the delims clause, leading spaces would be removed).

set tl!tl:"*"=!    will not replace anything, as it is no "valid" syntax.
The asterisk is a wildcard character here. The (stupid) cmd parser will not allow for escaping it by including into double quotes, or prepending a caret. That is a flaw, but you cannot get around it. The asterisk has to be followed by characters (not identical to the equal sign) - and then it is a wildcard. Characters followed by an asterisk will use the asterisk literally, not as wildcard. Having the wildcard alone does not work.


set x=a*b*c
echo !x:*=!           output: !x*=!
echo !x:**=!          output: b*c
echo !x:b*=!          output: a*c

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Because of that restrictions, it is better to use VBS or external tools like chgstr ( to replace special characters.
Bill PrewCommented:
I tend to agree with Qlemo, typically the easiest and fastest way to do search and replace type activities isn't via a BAT file, but rather using a separate utility program.  Here is the list I have accumulated of free ones, I have a few other pay ones that I have used but I won't mention those here.  Sorry it's a bit of a long list, but they are all different and selecting one to use can be a somewhat personal thing.  Some are command line based, others GUI.  Some are regex capable, others aren't.  So handle multiple files, some just one.  Etc...

tonydemarcoAuthor Commented:
Closest to a solution.

I was able to use combinations of:
Set tl=!tl:%***      =%!
Set tl=!tl:%***=%!
Set tl=!tl:**=%!
Set tl=!tl:^M**=^M!

to come to a near perfect solution.

Thanks Qlemo
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.