Hey, can I do this in dos?
Used to do similar on Unix but can't see how to make it work for Windows.
-----
Set Pets=(Cats Dogs Horses)
Set Cats=(FiFi FruFru Fluffy)
Set Dogs=(Lassie MrMuggles RinTinTin)
Set Horsese=(MrEd Trigger)
FOR %%A in %Pets% DO (
FOR %%B in %%A DO (
Echo “%%A %%B”
)
)
-----
Output should be…
Cats FiFi
Cats FruFru
Cats Fluffy
Dogs Lassie
Dogs MrMuggles
Dogs RinTinTin
Horses MrEd
Horses Trigger
This obviously does not work, is there any way to achieve this?
Simon.
Page 1 of 1
Nested Loops of Array Elements
#2
Posted 08 February 2012 - 03:20 AM
#3
Posted 08 February 2012 - 03:54 AM
I do not have a DOS system to test this on, only a Windows 7 command prompt (cmd.exe).
This will do for the specific conditions you have listed.
Changes will need to be made if any of the pet names will have spaces.
This will do for the specific conditions you have listed.
Changes will need to be made if any of the pet names will have spaces.
@echo off
Set Pets=Cats Dogs Horses
Set Cats=FiFi FruFru Fluffy
Set Dogs=Lassie MrMuggies RinTinTin
Set Horses=MrEd Trigger
For %%a In (%Pets%) Do (
For /f "Tokens=1,* delims== " %%b In ('set %%a') Do (
For %%n In (%%c) Do (
Echo %%a %%n
)
)
)
#4
Posted 08 February 2012 - 04:33 AM
If I may, if DELAYEDEXPANSION is set, you don't even need the FOR /F:
jaclaz
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
Set Pets=Cats Dogs Horses
Set Cats=FiFi FruFru Fluffy
Set Dogs=Lassie MrMuggles RinTinTin
Set Horses=MrEd Trigger
For %%A In (%Pets%) Do (
For %%B In (!%%A!) Do (
For %%C In (%%B) Do (
Echo %%A %%C
)
)
)
jaclaz
#5
Posted 08 February 2012 - 11:48 AM
Here is a way of doing what the poster wants using VBS script.
Produces this output
Dim Pets :Pets = Array("Cats","Dogs","Horses")
Dim a,c,i,z
'-> Loop Threw Pets Array
For Each a In Pets
c = c + 1
call SortArray(a, c)
Next
'-> Function To Display Pets Array And Names
Function SortArray(Item, N)
Select Case N
Case 1 :i = Array("FiFi", "FruFru", "Fluffy")
Case 2 :i = Array("Lassie", "MrMuggles", "RinTinTin")
Case 3 :i = Array("MrEd", "Trigger", "Silver")
End Select
For Each z In i
WScript.Echo Item & vbTab & z
Next
End Function
Produces this output
Cats FiFi Cats FruFru Cats Fluffy Dogs Lassie Dogs MrMuggles Dogs RinTinTin Horses MrEd Horses Trigger Horses Silver
#6
Posted 09 February 2012 - 09:06 PM
Thanks all, I'll run that on all systems (NT, XP, W7) today and see how it goes.
Not going to have issues with spaces, naming conventions we use prohibit it.
cheers
Not going to have issues with spaces, naming conventions we use prohibit it.
cheers
- ← How to finis batch script?
- Programming (C++, Delphi, VB/VBS, CMD/batch, etc.)
- VB.NET vs C# vs C++ vs Delphi vs Borland C++ vs Java →
Share this topic:
Page 1 of 1



Help
Back to top










