How can you ask a question and answer it at the same time? Stack Overflow is usually not a FAQ site. People ask questions here if they cannot find an answer on their own.
–
Frank BollackJul 27 '12 at 9:55

Why are you answering in the 3rd person? :)
–
Bali CJul 27 '12 at 9:57

Agreed - it is definitely OK. I've done it a few times myself. But unfortunately it rubs some people the wrong way, so you will want to be careful. Just make sure the question and answer is novel and useful. You will want to do extra research to make sure it hasn't already been asked and isn't common knowledge. This particular question is a good choice as replacing asterisk is a long standing thorn for those of us that like to work with batch.
–
dbenhamJul 27 '12 at 12:07

2

@Frank Bollack, it's an option right on the page when you ask a question. Down at the bottom, a check-box says "Answer your own question". If you check Dialecticus's link, you will find the FAQ says "it is not merely OK to ask and answer your own question, it is explicitly encouraged" In big, bold letters. And lastly, I searched for the better part of a day, and couldn't find anything that addressed this, so when I figured it out on my own I thought there should be some info about it on the net.
–
James KJul 27 '12 at 22:12

1 Answer
1

The problem that you're encountering stems from the fact that the asterisk * is a special character when used with the SET search and replace method. It matches multiple characters in a limited, but still useful, way. You can learn about that here.

The hard answer is Yes!

I will provide you with two solutions. One an incomplete solution but elegent,
the other complete and inelegent.

Both methods will search for * and replace it with an x.
Both methods will both search and modify the following string:

*love*

The first method that comes to mind is using a 'FOR /L' statement, and requires that you know how many characters long the environmental variable is.

::Major Edit::

I thought I knew the various maximum size strings of environmental variables, but dbenham has taken me to school, shown me a kick-in-the-behind length function, and in the mean time completely reversed my opinions of the two solutions I'm presenting.

Other than for the Windows 95/98/ME limitation of a 256 Character maximum environmental variable size. It seems that all versions of Windows using CMD.EXE have a limitation of 8,192 characters, well below what the documentation suggests.

Both versions require delayed environmental variable expansion, but for two different reasons. One because I'm operating inside a FOR statement. The other because you cannot put a % pair inside another % pair because the command processor matches the second % that it encounters to the first one it encounters, but we need to use a variable inside another variable expression. (You'll see.)

This solution uses the strLen function (in line 3) from DosTips.com that can be found Here. Just slap it into a file called strLen.bat and be amazed at it's speed!

Solution 1: (FOR /L Solution) :: Preferred Solution ::

setlocal ENABLEDELAYEDEXPANSION
set nam=*love*
call strLen nam len
for /l %%x in (0,1,%len%) do if not "!nam:~%%x,1!"=="" if "!nam:~%%x,1!"=="*" (
set /a plusone=%%x+1
for /l %%y in (!plusone!, 1, !plusone!) do (
set nam=!nam:~0,%%x!x!nam:~%%y!
)
)
echo %nam%
ENDLOCAL

I think this is a quick and elegant solution It could be sped up by adding the contents of strLen.bat to the routine, but I wanted no confusion as to the author.

If you, for some reason, do not wish to use strLen, then the next quickest method would probably use a GOTO loop.

Solution 2: (Goto Solution)

setlocal ENABLEDELAYEDEXPANSION
set nam=*love*
set num=0
:loop
set /a plusone=%num%+1
if "!nam:~%num%,1!"=="*" set nam=!nam:~0,%num%!x!nam:~%plusone%!
set /a num=%num%+1
if not "!nam:~%num%,1!"=="" goto :loop
echo %nam%
EndLocal

Special thanks to dbenham for pointing out the strLen function. It works faster than any batch based function has a right to!

@dbenham - Thanks for the tips. I try to stay within the confines of the tools that Microsoft supplies with it's operating systems, or provide free downloads for. But I still appreciate the tips! :)
–
James KJul 27 '12 at 22:30

I am well aware of the Windows limits, but you do not realize that CMD.EXE is further constrained: see support.microsoft.com/kb/830473. Also, I don't think you understood the links I provided. Everything there is written using pure native batch, so it absolutely meets your goal of MS supplied tools. If you are unwilling to use a batch routine written by someone else, then that would be like me saying I couldn't use any batch script you posted here because MS did not supply it. Trust me, you will be amazed by the performance and usefulness of the strLen routine.
–
dbenhamJul 27 '12 at 23:51