I'm trying to remove an asterisk from an environmental variable string, but can't seem to do it.
I'm creating an m3u file based around search strings, so for instance I if I want to make an m3u file containing every song with the word love in it, I would enter:
m3u *Love*
And m3u.bat would create the file:
xLovex.m3u
But the regular method of replacing characters does not work with an asterisk. (Though I don't have that problem with the question mark.)
set nam=%nam:*=x%.m3u
Instead creates the filename
x.m3u
Another solution to the stated problem is to use a PowerShell replace command within your batch script.
In the above code, the second line
Once that is done, you read the value back into your variable.
To further explain the replace command, the first single quotes is what you are searching for. We are using square brackets to identify the * character as a hex character (\x2A is the hex value for *). After the comma, the second set of single quotes contains no value so that the searched object is removed. To prevent a space between xLovex and the .m3u, we have to use -replace '.{1}$' before writing the result to the text file.
Once you are done with the text file, enter a line to delete it.
Although there were already some very good and robust ways explained here, I'd still like to add another option for the sake of completion.
It's not as good as the other options but I personally use it in some cases where I'd like to keep the code clean and where I know that it will suffice:
The way it works is by using
for /f
'sdelims
to cut the string into two parts, which are then put back together, getting rid of the * in the process:Obviously, the downside to this is that it can only be used to remove one *.
To remove more, we can either just use more tokens...
... or we can put the first line in a
for /l
-loop:Another thing to note is that you can define more than one character in
delims
, and they will all be removed at once:The easy answer is no.
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:
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.
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 ::
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)
Special thanks to dbenham for pointing out the strLen function. It works faster than any batch based function has a right to!
See this answer, and with set-ast.bat you'll want to put
set-ast nam "x"
in your file where needed.set-ast
takes the parameters<variable-to-modify> <string-to-replace-asterisks-with>