0

I have a list of projects and a list of project types. I need to iterate over the list of project types and every time the selected project type equals a certain value I need to get the Ith entry from the project names. For example:

Project Names: Project1,Project2,Project3

Project Types: web,ssis,ssis

I need all the "ssis" projects so the resulting list should be "Project2,Project3".

I've used for /f a lot but I don't know how to get the "i" in the "for" iteration so I can select the related entry from the second list. I've searched on this and gone down into the weeds on "for" but can't seem to come up with an answer.

If there's a method that works better than "for" I'm game. If there's a way to use "for" on the first list and pull the related entries out of the second list that would be great too.

Any help is greatly appreciated.

Richard Schaefer
  • 525
  • 3
  • 13
  • 45
  • Are the project name and types in different files? – aphoria Apr 24 '14 at 13:36
  • The project names and project types are set by parameters passed into the bat file. I set them as constants for the purpose of simplifying the question. I get the %projnames% and %projtypes% set correctly, it's the process of iterating over %projtypes% and selecting from %projnames% that has me stumped. – Richard Schaefer Apr 24 '14 at 13:48
  • Here's what I have. Need to put it in another comment to try and keep the formatting. The error I'm getting is in the nested FOR command using "tokens=!ptr!". I'm getting error '!ptr!" was unexpected at this time.' – Richard Schaefer Apr 24 '14 at 14:33
  • set projlist=WebProject,SSISProject,ReportProject set projtypes=web,ssis,ssrs set /a ptr=1 setlocal ENABLEDELAYEDEXPANSION for %%a in (%projtypes%) do ( echo %%a echo ptr=!ptr! for /f "tokens=!ptr!" %%b IN ("%projlist%") DO (echo %%b) set /a ptr=ptr+1 ) endlocal – Richard Schaefer Apr 24 '14 at 14:41
  • Sorry for the wall of text above. I can't seem to get formatted code in a comment despite following the editing guidelines.. – Richard Schaefer Apr 24 '14 at 14:41
  • 1
    You can't put formatte code in a comment. Try editing it into your question using `edit` below the tags. – Magoo Apr 24 '14 at 14:58

2 Answers2

0

Based off a now-deleted comment:

@ECHO OFF

SET projlist=Project1,Project2,Project3
SET projtypes=web,ssis,ssrs
SET /a ptr=1

SETLOCAL ENABLEDELAYEDEXPANSION

FOR %%a IN (%projtypes%) DO (
  ECHO %%a
  SET index=1
  FOR %%b IN (%projlist%) DO (
    IF !index!==!ptr! ECHO %%b
    SET /A index+=1
  )
  SET /a ptr=ptr+1
  ECHO ----------------
)
ENDLOCAL

Running this will give this result:

web
Project1
----------------
ssis
Project2
----------------
ssis
Project3
----------------

Of course, you'll need to modify it to fit your exact needs.

aphoria
  • 19,796
  • 7
  • 64
  • 73
  • Thanks. I just realized I should have posted my code as an "Answer" not as a comment on my original question. I could have formatted the code in an "answer". Anywho, that looks like it solves my problem. Now on to the next part. Thanks. – Richard Schaefer Apr 24 '14 at 15:05
  • For future reference, you should be able to edit your question and add your code there. – aphoria Apr 24 '14 at 15:07
0

If I understand it correctly, you want to group all project names that belongs to the same project type and show the result as a list of groups. I taken the answer of this post, that do a match of the elements of two lists, and modified it in order to get the groups you want:

@echo off
setlocal EnableDelayedExpansion

set ProjectNames=Project1,Project2,Project3
set ProjectTypes=web,ssis,ssis

rem Group project names by type
set i=0
for %%A in (%ProjectTypes%) do (
   set /A i+=1, j=0
   for %%B in (%ProjectNames%) do (
      set /A j+=1
      if !i! equ !j! set NamesByType[%%A]=!NamesByType[%%A]!,%%B
   )
)

rem Show result
for /F "tokens=2,3 delims=[]=" %%a in ('set NamesByType[') do (
   set list=%%b
   echo Type %%a: !list:~1!
)

Output:

Type ssis: Project2,Project3
Type web: Project1

You may also consult a particular element of NamesByType array, for example:

echo %NamesByType[ssis]%
Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Thanks @Aacini but that's not really what I'm looking for. Your code would work for me as a more generic algorithm but I'm (at this point) just looking for which projects have a type=SSIS. I was able to modify the original answer to subset the comparison to only return entries where ProjectType=ssis. Thanks. – Richard Schaefer Apr 25 '14 at 10:19