I need to replace character *
from a string which is some what like this:
*10.*31.**2.*65
I want to remove all the *
from this string using batch script.
I need to replace character *
from a string which is some what like this:
*10.*31.**2.*65
I want to remove all the *
from this string using batch script.
You can simplify the removal process to one line. For example, if I had a string like this:
set string=hello!
and I wanted to remove the "!" in it, I can just do
set string=%string:!=%
The string will now have the value "hello"
instead of "hello!"
edit: As jeb pointed out in the comments, you can't replace "*"
using this method. This is because *
is a wildcard character for batch file pattern matching (brilliant explanation of it here). I did not know this at the time of posting this answer as I assumed this replacement method worked for all input strings. If you specifically need to replace *
, I'd recommend following magoo's answer. Otherwise, I will be keeping this answer up for those who need a general, short, one-liner solution to batch string replacements.
You don't need an entire script for this. You just need a single for /f
loop.
C:\Users\rojo>set test=*10.*31.**2.*65
C:\Users\rojo>echo %test%
*10.*31.**2.*65
C:\Users\rojo>for /f "tokens=1-4 delims=.^*" %I in ("%test%") do @set test=%I.%J.%K.%L
C:\Users\rojo>echo %test%
10.31.2.65
If you want to put the for
loop into a batch script, use %%I
, %%J
, %%K
and %%L
instead of the single percents.
Another solution to the stated problem is to use a PowerShell replace command within your batch script.
set var=*10.*31.*2.*65
echo %var%>var.txt && powershell -command "(get-content var.txt) -replace '[\x2A]+', '' | set-content var.txt"
set /p var=<var.txt
echo %var%
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.
Another option is to use a caret character to search for everything except for what is identified in the brackets. This would remove everything we other than want you want to keep. To do that you would replace the second line with:
echo %var%>var.txt && powershell -command "(get-content var.txt) -replace '[^\x30-x39\x2E]+', '' | set-content var.txt"
The first set of characters, \x-30-x39, identify 0 - 9. The \x2E are periods.
Once you are done with the text file, enter a line to delete it.
if exist var.txt del var.txt
@echo off
setlocal EnableDelayedExpansion
set "str=*10.*31.**2.*65"
set "char=" & set "repl=" & set "loc=0"
echo !str!
call :replace
echo !repl!
exit /b
:replace
set "char=!str:~%loc%,1!
if "!char!" == "" exit /b
if "!char!" equ "*" set "char=_"
set "repl=!repl!!char!"
set /a "loc+=1"
goto :replace
endlocal
This is another solution. maybe a little slow, but call set is too slow.
This code replaces the a to b character:
Set search=%search:a=b%