-1

Hi – I am trying to build a nested Loops with 3 Categories of Variables. The nested loop is working fine if I am taking “normal” variables but what I want to do is combining all the variables. It should run through all the Markets-Variables and then Find and Replace some Text which is also related to the market. The problem is to concatenate the variables in the for loop – need some help there please!

Thanks a lot!

:: set market array variables
SET MARKET[1]=DE
:: 14 more variables ...

:: set find text array variables
SET %MARKET[1]%FTEXT[1]=_1_
:: 29 more variables ...

:: set replace text array variables
SET %MARKET[1]%RTEXT[1]=_2_
:: 29 more variables ...

FOR /L %%M IN (1,1,15) DO (
  ECHO ==================
  ECHO Renaming market !MARKET[%%M]!
  ECHO ==================
  FOR /L %%T IN (1,1,30) DO (
    ECHO Renaming text from !MARKET[%%M]!!FTEXT[%%T]! to !MARKET[%%M]!!RTEXT[%%T]!
    :: ACTION FOR RENAMING
    PAUSE
  )
)
HiKO
  • 1
  • 1
    Not sure what you mean. Have you put a "setlocal enabledelayedexpansion" in the top of your script? – Jon Tofte-Hansen Oct 09 '15 at 09:04
  • Please tell me, do you intend to create variables named like `DEFTEXT[1]`, `DERTEXT[1]`,... (`DE` being the value of `MARKET[1]`), or do you actually want the find/replace variables to be called `MARKET[1]FTEXT[1]`, `MARKET[1]RTEXT[1]`,...? – aschipfl Oct 09 '15 at 10:45

4 Answers4

0

Your find variables have names like DEFTEXT[1] so !MARKET[%%M]!!FTEXT[%%T]! was wrong because it expands to DE and FTEXT[1] separately, and since you don't have FTEXT[1] nothing useful happened.

Solution: construct the correct variable name and then expand it, I've used call set trick for this, here's your innermost loop:

setlocal enableDelayedExpansion
call set "from=%%!MARKET[%%M]!FTEXT[%%T]%%"
call set "to=%%!MARKET[%%M]!RTEXT[%%T]%%"
ECHO Renaming text from !from! to !to!
rem ACTION FOR RENAMING
...................
...................
endlocal

Note that the from and to variables exist only inside setlocal ... endlocal context. Also avoid using :: comments inside parenthesized blocks, use rem instead.

Community
  • 1
  • 1
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • Hey - thanks a lot for the Answer. This is exactly what I was looking for. The "call set" was fixing the problem. Yes I forgot to ad the SETLOCAL EnableDelayedExpansion here but it was in the original script. The main Idea was to have it a little bit more generic that was the reason why I made this combination of the variables. There can be up to 15 "Markets" with unknown names and for each market there can be up to 30 variables each for FIND and REPLACE. What is the difference between :: and rem for comments? – HiKO Oct 10 '15 at 11:11
  • @HiKO, that answer I've linked contains an explanation, you may look at other answers there too. – wOxxOm Oct 10 '15 at 11:20
0

you need another layer of expansion:

call ECHO Renaming text from %%!MARKET[%%M]!FTEXT[%%T]%% to %%!MARKET[%%M]!RTEXT[%%T]%%

(of course also setlocal enabledelayedexpansion- not shown in your question, but you probably have it in your original code)

Stephan
  • 53,940
  • 10
  • 58
  • 91
0

You need to expand the three variables that comprise the old and new names before using they to get the names. I used a FOR /F to do that; this method run faster than CALL:

FOR /L %%M IN (1,1,15) DO (
  ECHO ==================
  ECHO Renaming market !MARKET[%%M]!
  ECHO ==================
  FOR /L %%T IN (1,1,30) DO (
    FOR /F "TOKENS=1-3" %%A IN ("!MARKET[%%M]! !FTEXT[%%T]! !RTEXT[%%T]!") DO (
       ECHO Renaming text from !%%A%%B! to !%%A%%C!
       :: ACTION FOR RENAMING
    )
    PAUSE
  )
)

Also, note that the first variable just needs to be expanded in the outer loop:

FOR /L %%M IN (1,1,15) DO FOR /F %%A IN ("!MARKET[%%M]!") DO (
  ECHO ==================
  ECHO Renaming market !MARKET[%%M]!
  ECHO ==================
  FOR /L %%T IN (1,1,30) DO (
    FOR /F "TOKENS=1-2" %%B IN ("!FTEXT[%%T]! !RTEXT[%%T]!") DO (
       ECHO Renaming text from !%%A%%B! to !%%A%%C!
       :: ACTION FOR RENAMING
    )
    PAUSE
  )
)

Further details at Arrays, linked lists and other data structures in cmd.exe (batch) script

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
0

Although your question is not absolutely clear, I try to provide a suitable answer (I hope I extracted the correct intention of the script)...

  1. SETLOCAL EnableDelayedExpansion is missing (perhaps you have it in your final code though).
  2. I suppose your find/replace variables are intended to be called MARKET[1]FTEXT[1]/MARKET[1]RTEXT[1],... (in your code the first pair will be named DEFTEXT[1]/DERTEXT[1] where DE is the value of variable MARKET[1]).
  3. For string replacement use the VARIABLE:find=replace expansion syntax (see set /?). However, this is a bit tricky when both the find and replace string portions are contained in variables on their own. I implemented two variants -- see the related remarks REM in the code below.

The following code is supposed to do what you are trying to accomplish:

SETLOCAL EnableDelayedExpansion

REM set market array variables
SET MARKET[1]=DE
:: 14 more variables ...

REM set find text array variables
SET FTEXT[1]=_1_
:: 29 more variables ...

REM set replace text array variables
SET RTEXT[1]=_2_
:: 29 more variables ...

FOR /L %%M IN (1,1,3) DO (
  ECHO ==================
  ECHO Renaming market !MARKET[%%M]!
  ECHO ==================
  FOR /L %%T IN (1,1,2) DO (
    ECHO Replacing text portion !FTEXT[%%T]! by !RTEXT[%%T]!

    REM Use EITHER this code block for string replacement...
    IF NOT "!FTEXT[%%T]!"=="" (
      CALL SET MARKET[%%M]=%%^^MARKET[%%M]:!FTEXT[%%T]!=!RTEXT[%%T]!%%
    )

    REM ...OR use this one!
    FOR /F "tokens=1* delims==" %%R in ("!FTEXT[%%T]!=!RTEXT[%%T]!") do (
      IF NOT "%%R"=="" (
        SET MARKET[%%M]=!MARKET[%%M]:%%R=%%S!
      )
    )

    ECHO !MARKET[%%M]!

    PAUSE
  )
)
ENDLOCAL
aschipfl
  • 33,626
  • 12
  • 54
  • 99