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?
QlemoConnect With a Mentor 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

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!
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

QlemoBatchelor, 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.