20

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.

1
  • Note: * is a special character, so you can't use the %var:a=b% syntax.
    – mwfearnley
    Nov 20, 2019 at 10:33

7 Answers 7

49

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.

3
  • 4
    This isn't an answer to the qeustion, as you can't replace a * with this method. As replace expressions wirh a beginning * has a special meaning.
    – jeb
    Jun 15, 2016 at 15:49
  • 1
    transform from 1.2.3.4 to 1,2,3,4 setlocal set string=1.2.3.4 set string=%string:.=,%
    – Justin
    Jul 20, 2016 at 6:35
  • 2
    This uses syntax %variable:StrToFind=NewStr%, see ss64.com/nt/syntax-replace.html Oct 28, 2021 at 17:16
9
@ECHO OFF
SETLOCAL
SET mystring=*10.31.*2.*65
:deaster
FOR /f "tokens=1* delims=*" %%i IN ("%mystring%") DO (
   SET mystring=%%j
   IF DEFINED mystring (
      SET mystring=%%i%%j
      GOTO deaster
   ) ELSE (
      SET mystring=%%i
   )
)
ECHO result=%mystring%=
4

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.

2

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

  • writes your string into a text file
  • calls a PowerShell command to get the contents of that file
  • replaces the * character with null
  • overwrites the text file with the new value

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
2
  • I've never seen the structure command1 > file | command2. Does that do anything special? Would && be a better choice than |?
    – mwfearnley
    Nov 20, 2019 at 10:26
  • Thanks @mwfearnley. You are correct. The && is the right choice. The && means to execute the second command once the first command succeeds. The pipe sends the output of the first command to be used by the second. I have updated the line to use &&.
    – David
    Dec 27, 2019 at 23:52
1
@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.

1

This code replaces the a to b character:

Set search=%search:a=b%
1
  • Even the first comment on the question shows this already and explains why it can't be used here for *
    – jeb
    Dec 17, 2021 at 9:15
-1

Look into sed, the stream editor. If you are on Windows (assuming you are), you can find precompiled binaries on the web.

echo "*10.31.*2.*65" | sed 's/\*//g'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.