0

I have main set of values and a subset to it

Main Set: Group1,Group2,Group3

Subset : Group1_Sub1,Group1_Sub2,Group2_Sub1,Group3_Sub1,Group3_Sub2

Group1 ->Group1_Sub1 and Group1_Sub2

Group2 ->Group2_Sub1

Group3 ->Group3_Sub1,Group3_Sub2

For each main list, I would like to loop only through its corresponding sub group list and display output.

Currently I am using below code

for %%s in (

Group1,Group2,Group3

    ) do (

        echo set Main Group %%s >> Log.txt

        for %%i in (
                    Group1_Sub1,Group1_Sub2,Group2_Sub1,Group3_Sub1,Group3_Sub2
                    ) do (
                        echo Main Group is %%s and its sub group is %%i >>Log.txt
                         )
            )

Above code will give me the out put as :

set Main Group Group1 
Main Grpup is Group1 and its sub group is Group1_Sub1 
Main Grpup is Group1 and its sub group is Group1_Sub2 
Main Grpup is Group1 and its sub group is Group2_Sub1 
Main Grpup is Group1 and its sub group is Group3_Sub1 
Main Grpup is Group1 and its sub group is Group3_Sub2 
set Main Group Group2 
Main Grpup is Group2 and its sub group is Group1_Sub1 
Main Grpup is Group2 and its sub group is Group1_Sub2 
Main Grpup is Group2 and its sub group is Group2_Sub1 
Main Grpup is Group2 and its sub group is Group3_Sub1 
Main Grpup is Group2 and its sub group is Group3_Sub2 
set Main Group Group3 
Main Grpup is Group3 and its sub group is Group1_Sub1 
Main Grpup is Group3 and its sub group is Group1_Sub2 
Main Grpup is Group3 and its sub group is Group2_Sub1 
Main Grpup is Group3 and its sub group is Group3_Sub1 
Main Grpup is Group3 and its sub group is Group3_Sub2 

I would like to restrict them to go through only its corresponding list like below

set Main Group Group1 
Main Grpup is Group1 and its sub group is Group1_Sub1 
Main Grpup is Group1 and its sub group is Group1_Sub2 
set Main Group Group2 
Main Grpup is Group2 and its sub group is Group2_Sub1 
set Main Group Group3  
Main Grpup is Group3 and its sub group is Group3_Sub1 
Main Grpup is Group3 and its sub group is Group3_Sub2 

How can I achieve this ?

2
  • IIUR parse the output of a set with varying length variable names with a for /f, set will output all vars with a given prefix and their content. Commented May 15, 2019 at 16:17
  • are you aware that your code doesn't access any variables but just generates the output by concatenating strings? Commented May 15, 2019 at 18:08

2 Answers 2

1

within the inner (%%i) loop:

    ECHO %%i|FINDSTR /b /i /L /c:"%%s_">nul
    IF NOT ERRORLEVEL 1 echo Main Group is %%s and its sub group is %%i >>Log.txt

which echoes (eg) Group1_Sub2 into findstr which looks for a string /b beginning with the current value of %%s + an underscore. /i means case-insensitive, /L means literal comparison and /c: indicates the string to detect.

If findstr finds the string it's looking for, then errorlevel will be set to 0 or 1 otherwise. The >nul suppresses output. errorlevel may then be tested using the conventional syntax.

Sign up to request clarification or add additional context in comments.

Comments

0

As you speak about variables: the following code looks for the main groups and their sub groups by itself:

@echo off
setlocal enabledelayedexpansion

REM following line just for generating test variables:
for %%a in (1 2 3) do for %%b in (1 2 3 4) do set "Group%%a_Sub%%b=!random!"

REM search MainGroups:
for /f "delims=_" %%a in ('set group') do set "Main_%%a=Group"
REM process each MainGroup
for /f "tokens=2 delims=_=" %%a in ('set Main_') do (
  echo set Main Group %%a
  for /f "delims==" %%b in ('set %%a_') do (
    echo Main Group is %%a and its sub group is %%b and its content is !%%b!
  )
)

Pro: no need to hardcode each and every variable name
Con (?): does not list empty (non-existing) variables (depending on your needs, this could even be another Pro)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.