Hi Everyone,
I'm working through a test to see how CSV parsing in relation to folder and subfolder creation works. The script I have is as follows:
$File = "C:\Projects\Folders\folders.csv" $Data = Import-Csv $File $Base = "C:\Projects\Folders\Test\" foreach($line in $Data) { $topFolder = $Base + $line.Base if (!(Test-Path $topFolder)) { New-Item $topFolder -type directory } if (!(Test-Path "$topFolder\$line.Sub2")) { New-Item "$topFolder\$line.Sub2" -type directory } if (!(Test-Path "$topFolder\$line.Sub2\$line.Sub3")) { New-Item "$topFolder\$line.Sub2\$line.Sub3" -type directory } } Write-Host "Folder created"
And the CSV looks like this:
Base,Sub1,Sub2 Folder 1, Subfolder 1,Subfolder 2
Now, I'm getting the CSV read just fine, however I'm getting stuck on building out the subsequent subfolders and end up with a "Folder 1" directory in my $Base which then gets populated with directories that look as such:
C:\Projects\Folders\Test\Folder 1\@{Base=Folder 1; Sub1=Subfolder 1; Sub2=Subfolder 2}.Sub1\@{Base=Folder 1; Sub1=Subfolder 1; Sub2=Subfolder 2}.Sub2
@{Base=Folder 1; Sub1=Subfolder 1; Sub2=Subfolder 2}.Sub1 being the name of the first folder in the topmost folder (Folder 1)
and @{Base=Folder 1; Sub1=Subfolder 1; Sub2=Subfolder 2}.Sub2 being the nested folder within that subfolder.
What am I doing wrong here? I need the script to parse the CSV, create the "Base" folder, then populate the subfolder structure from Sub1 under that and then in each of those Sub1 folders I need to populate the directory structure with the folder names from Sub2.
It seems pretty straightforward but I'm obviously missing something.