?
Solved

replace = in a batch string

Posted on 2011-03-07
4
Medium Priority
?
3,351 Views
Last Modified: 2012-08-13
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?)


0
Comment
Question by:knightEknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 57

Accepted Solution

by:
Bill Prew earned 1400 total points
ID: 35059912
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
 
LVL 33

Author Comment

by:knightEknight
ID: 35060123
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
 
LVL 85

Assisted Solution

by:oBdA
oBdA earned 600 total points
ID: 35069028
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
 
LVL 33

Author Comment

by:knightEknight
ID: 35069323
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

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question