Solved

replace = in a batch string

Posted on 2011-03-07
4
2,838 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
  • 2
4 Comments
 
LVL 51

Accepted Solution

by:
Bill Prew earned 350 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 83

Assisted Solution

by:oBdA
oBdA earned 150 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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 this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now