0

The code below echo s two "B"s and not one B and an "a". Can someone explain to me why this is and how to get the result I want.

@echo off

set code=A
set code=%code:A=B%
echo %code%

set code=a
set code=%code:A=B%
echo %code%

pause
Hadus
  • 1,551
  • 11
  • 22
  • wow it seems like nobody bothers to answer :( – Hadus May 07 '16 at 16:01
  • If it helps the answer should have the same functionality as the set command but case sensitive. – Hadus May 07 '16 at 16:03
  • Oh no, trust me I've noticed it, I'm just trying to answer it myself... – Bloodied May 07 '16 at 16:05
  • thanks I've been googleing for at least an hour – Hadus May 07 '16 at 16:10
  • 1
    Perhaps [this](https://stackoverflow.com/questions/15986001/how-to-search-and-replace-case-sensitive-string-using-batch) or [this](http://www.dostips.com/forum/viewtopic.php?f=3&t=1986) ? shame it seems to be so difficult. – Bloodied May 07 '16 at 16:14
  • it seems way too complicated but i'll read it – Hadus May 07 '16 at 16:17
  • all of them use a file and not a variable and the answer shouldn't be about file manipulation and they use find what is too slow for putting in a loop – Hadus May 07 '16 at 16:22
  • http://stackoverflow.com/questions/28095092/string-manipulation-with-case-sensitivity this is the answer ... its - no; case sensitivity by default; but you can tricks; you can try moving it undef IF cause you can run IF /I - everything under that will be case sensitive – Drako May 07 '16 at 16:29

1 Answers1

2

Got this and edited it to fit your question from this question

@echo off
setlocal enabledelayedexpansion
set "string=AaBbCc"
set "result="
for /l %%G in (0,1,999) do (
    set char=!string:~%%G,1!
    if "!char!" equ "A" set "char=B"
    set "result=!result!!char!"
)
echo %result%
endlocal
pause

It works by going through each character (max of ~1000) and replacing a literal string "A" with "B". This can be slower on longer strings but should work fine. delayedExpansion needs to be enabled to reference the string it builds up 1 character at a time.

Bloodied
  • 1,004
  • 7
  • 20
  • and I think it would be better if we replaced 999 with a variable that has the length of the string right? – Hadus May 07 '16 at 16:30
  • You could shorten it if you know the variable you're going to replace is much shorter yes. – Bloodied May 07 '16 at 16:34