TL;DR: How can I go from "CSV Example" to create an array like "End Result"?
Background
I'm building a lab file system for testing purposes and I want to create a folder structure that looks somewhat like a real file system. I have several CSV files that contains folder information.
CSV Example:
Department Level1 Level2 Level3
Human Resources Personnel Templates APAC
Job Applications Customer Relations EMEA
Salaries and Expenses Directors NA
Vacation Tracking Human Resources SA
Disputes Legal Services
Marketing
Production
Finance
IT Services
I want to take each combination above and create a file system that has all the following folders.
End Result:
Human Resources\Personnel\Templates\APAC
Human Resources\Personnel\Templates\EMEA
...
Human Resources\Disputes\IT Services\NA
Human Resources\Disputes\IT Services\SA
Once I have the above array of all full paths, it's as simple as just doing:
foreach($folder in $MyFolderArray){
New-Item "\\Server\Share$\$folder" -ItemType Directory -Force
}
Problem
I want to be able to do this for any CSV file, regardless of how many columns I have, what the header names are, or how many values each column has. Currently, I'm hardcoding 4 foreach-loops but that solution requires all CSV files to have the same number of columns and header names. I'm looking for something that can take any CSV file of any column count and length.
Getting all headers from any CSV can be done like:
$CSVContent = Import-CSV "C:\PathToMyCSVFile"
$CSVHeaders = $CSVContent[0].PSObject.Properties.Name
This can be used to split the $CSVContent into one array per column with:
for($i=0;$i -lt $CSVHeaders.Count;$i++){
New-Variable -Name "Header$i" -Value $($CSVContent.$($CSVHeaders[$i]) | Where-Object{$_ -ne ""})
}
This creates arrays from $Header0 to $Header# where # is number of CSV columns minus 1, each array having all values from that column. Going from these arrays to create the final array with all full paths is where I'm stuck.
Question
How can I solve the logic of building a foreach(foreach(... loop that enumerates all value combinations without hardcoding this? I'm guessing this requires recursively calling the loop itself but I'm not sure how to do that.