The simple answer is no.
The problem you are facing is that the asterisk * is a special character when used with the search and replace method SET. It matches multiple characters in a limited but useful way. You can find out about it here .
The difficult answer is yes !
I will give you two solutions. One incomplete solution, but elegant, the other complete and not elegant.
Both methods will look for * and replace it with x.
Both methods will search and modify the following line:
*love*
The first method that comes to mind is to use the FOR / L operator, which requires you to know how many characters the environment variable contains.
:: Major Edit ::
I thought I knew the different lines of the maximum sizes of the environment variables, but dbenham took me to school, showed me the function of the stroke length , and at the same time completely changed my mind about the two solutions that I Presentation.
With the exception of the Windows 95/98 / ME limit, the maximum size of the environment variable is 256 characters. It seems that all versions of Windows using CMD.EXE have a limit of 8,192 characters, which is significantly lower than what is suggested in the documentation.
Both versions require delayed expansion of the environment variable, but for two different reasons. One, because I work inside a FOR statement. Another, because you cannot put the% pair in another% pair, because the shell matches the second% that it encounters with the first% that it encounters, but we need to use the variable inside another variable expression. (You'll see.)
This solution uses the strLen function (on line 3) from DosTips.com, which can be found here . Just put it in a file called strLen.bat and marvel at the speed!
Solution 1: (FOR / L Solution) :: Preferred Solution ::
setlocal ENABLEDELAYEDEXPANSION set nam=*love* rem calling strLen 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 can be accelerated by adding the contents of strLen.bat to the routine, but I did not want to confuse the author.
If for some reason you do not want to use strLen, then the next quick method is likely to use the GOTO loop.
Solution 2: (Go to 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 to the strLen function. It works faster than any batch function is entitled!