replace = in a batch string

I'm looking for a quick way to do a string substitution of the '=' symbol.

In batch strings you can do simple search/replace like this:

  set myvar=this=that
  echo  %myvar% - %myvar:t=T%

  result:  this=that - This=ThaT

But I'm looking for a simple way to replace the '=' symbol WITHOUT having to loop thru the string and re-build it.
Here's what I've tried:

  echo %myvar:==X%
  echo %myvar:^==X%

  desired result:  thisXthat

Any non-looping ideas?  (or sourced confirmation that it can't be done?)


LVL 33
knightEknightAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Bill PrewConnect With a Mentor Commented:
I can't reference a definitive source for this, but as one who has done a good bit of work with BAT files I think it's safe to accept that the syntax of the SET command doesn't allow the equal sign to be replaced.

There are two ways to work around this, but you may not like either :-).

First, if you know you are going to have to do this ahead of time, you could use a different character in the initial assignment in place of the = sign.  Then, if you need to you can replace that character with either an = sign when you need it that way, or some other character, like an X in your example.  Here's a small example:

set myvar=this#that
echo %myvar% - %myvar:t=T%
echo %myvar:#==%
echo %myvar:#=X%

Open in new window

Another approach that does require rebuilding the string, but isn't too painful is to use a FOR statement.  Here's a simple example of that.  Basically we let the FOR split the original string on the = sign, and then rebuild it with the desired character.

set myvar=this=that
echo %myvar% - %myvar:t=T%
for /F "tokens=1* delims==" %%A in ("%myvar%") do set myvar=%%AX%%B
echo %myvar%

Open in new window

~bp
0
 
knightEknightAuthor Commented:
Thanks Bill.  Actually I've already worked similar solutions before I posted.  The problem with the first is that I would first have to substitute all the equals with pounds in all the variables, which is the problem I'm trying to resolve.  As for the second solution, that is exactly what I'm doing now.  It works well enough, but it's just messy.
0
 
oBdAConnect With a Mentor Commented:
Unfortunately, you can't tweak the set command into replacing an equal sign (and it won't work with an asterisk "*", either, btw.).
To "hide" the mess, you can move it into a function (the quotes around the equal sign are required, they're considered command line delimiters ...).
@echo off
setlocal enabledelayedexpansion
set myvar=this=that=those
call :ReplaceSpecialChar1 myvar "=" "X"
echo Result of ReplaceSpecialChar1: [%myvar%]
set myvar=this=that=those
call :ReplaceSpecialChar2 myvar "=" "X"
echo Result of ReplaceSpecialChar2: [%myvar%]

REM More custom code ...


REM Only subroutines from here ...
goto :eof
REM Will only replace the first occurrence of the character to be replaced:
:ReplaceSpecialChar1
for /f "tokens=1* delims=%~2" %%a in ("!%1!") do (
  if "%%b"=="" (set %1=%%a) else (set %1=%%a%~3%%b)
)
goto :eof

REM Will replace all occurrences of the character to be replaced:
:ReplaceSpecialChar2
set work=!%1!
set /a i = 0
:ReplaceLoop
if "!work:~%i%,1!"=="" (set %1=!work!&goto :eof)
if not "!work:~%i%,1!"=="%~2" (set /a i += 1&goto ReplaceLoop)
set head=!work:~0,%i%!
set /a i += 1
set work=!head!%~3!work:~%i%!
goto :ReplaceLoop

Open in new window

0
 
knightEknightAuthor Commented:
yes, that is actually similar to what I'm doing now.

Thanks guys - it is good enough for me to know that the real answer is that there isn't one!  :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.