0

If I simplified my requirement, I want to get last two digits of these strings using batch file. This is not the exact requirement. but I made it simple for understanding. :)

 11-22-33
 11-22-44
 11-22-55
 11-22-66

expected outcome is

33
44
55
66

This is the code I wrote

 @echo off
    SetLocal EnableDelayedExpansion

    REM Fill strings to a array
    set DIR_COUNT=0
    for %%x in ("11-22-33" "11-22-44" "11-22-55" "11-22-66") do (    
        set /A DIR_COUNT+=1
        set  CONTENT_DIR_NAME=%%~x
        set "DIR_LIST[!DIR_COUNT!]=!CONTENT_DIR_NAME!"
    )

    REM This part is working when I hard code the index of array to 1. I placed this two lines for testing purpose of above code.
    for %%a in (%DIR_LIST[1]:-= %) do set mfgName=%%a
    echo %mfgName%

    REM this loop doesn't work because the syntax not correct. 
    for /L %%i in (1,1,%DIR_COUNT%) do (    
        for %%a in (%!DIR_LIST[%%i]!:-= %) do (
            set mfgName=%%a
            echo !mfgName!
        )
    )

As my understanding syntax of (%!DIR_LIST[%%i]!:-= %) is not correct. Any ideas how to DelayedExpansion inside a DelayedExpansion and correct this syntax

Squashman
  • 13,649
  • 5
  • 27
  • 36
Nayana Adassuriya
  • 23,596
  • 30
  • 104
  • 147
  • Why are you putting the data into a pseudo array? Why are you using two `SET` commands to assign the data to the pseudo array when it can just be done in one? – Squashman May 21 '18 at 03:09
  • @squashman: my requirement is complected than this. I have to do some manipulations to the strings before go in to the second for loop. For others understanding I have simplified the question in this way. – Nayana Adassuriya May 21 '18 at 03:12
  • `for %%a in (!DIR_LIST[%%i]:-= !) do (` You might have to escape the equals symbol. – Squashman May 21 '18 at 03:15
  • `(!DIR_LIST[%%i]:-= !)` or `(!DIR_LIST[%%i]:- !)` doesn't work. I have tried – Nayana Adassuriya May 21 '18 at 03:20
  • Why would you get rid of the equals symbol when you are trying to do string substitution? – Squashman May 21 '18 at 03:22

1 Answers1

1

Here is the correct syntax for your batch file.

@echo off
SetLocal EnableDelayedExpansion

REM Fill strings to a array
set DIR_COUNT=0
for %%x in ("11-22-33" "11-22-44" "11-22-55" "11-22-66") do (    
    set /A DIR_COUNT+=1
    set CONTENT_DIR_NAME=%%~x
    set "DIR_LIST[!DIR_COUNT!]=!CONTENT_DIR_NAME!"
)

for /L %%i in (1,1,%DIR_COUNT%) do (    
    for %%a in (!DIR_LIST[%%i]:-^= !) do (
        set mfgName=%%a
        echo !mfgName!
    )
)
pause
Squashman
  • 13,649
  • 5
  • 27
  • 36
  • it works after moving `echo !mfgName!` out of the loop. could you help to explain a bit what is the magic of escaping `=` here. – Nayana Adassuriya May 21 '18 at 03:51
  • @NayanaAdassuriya, what do you mean it works when it is out of the loop? The echo works just fine inside the nested `FOR` command. – Squashman May 21 '18 at 04:05
  • Yes, it works fine. but to get my expected output mentioned in the question which is `33 44 55 66` I have to move `echo !mfgName!` outside of the loop. Anyway I'm more curious about the rational behind the escape =, if you could explain a bit. – Nayana Adassuriya May 21 '18 at 04:50
  • How cmd.exe parses a line of code for execution is quite complex. You can read about how it works in this [question](https://stackoverflow.com/questions/4094699/how-does-the-windows-command-interpreter-cmd-exe-parse-scripts). The equals symbol is affected by **phase 2**. – Squashman May 21 '18 at 15:52