18

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.

BDM
  • 3,760
  • 3
  • 19
  • 27
Jimmy
  • 415
  • 2
  • 5
  • 9

7 Answers7

46

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.

mgthomas99
  • 5,402
  • 3
  • 19
  • 21
  • 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 '16 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 '16 at 06:35
  • 1
    This uses syntax `%variable:StrToFind=NewStr%`, see https://ss64.com/nt/syntax-replace.html – Wayne Uroda Oct 28 '21 at 17:16
8
@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%=
Aacini
  • 65,180
  • 12
  • 72
  • 108
Magoo
  • 77,302
  • 8
  • 62
  • 84
3

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.

rojo
  • 24,000
  • 5
  • 55
  • 101
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
David
  • 134
  • 7
  • I've never seen the structure `command1 > file | command2`. Does that do anything special? Would `&&` be a better choice than `|`? – mwfearnley Nov 20 '19 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 '19 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.

kygg
  • 21
  • 3
0

This code replaces the a to b character:

Set search=%search:a=b%
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Mahmood
  • 57
  • 3
  • Even the first comment on the question shows this already and explains why it can't be used here for `*` – jeb Dec 17 '21 at 09: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'
sebnukem
  • 8,143
  • 6
  • 38
  • 48