Search code examples
powershellwindows-server-2012windows-serverpowershell-ise

Create multiple subfolders with different parent folders CSV - Powershell


I'm trying to create multiple sub folders for different parent directories. I have a CSV file with almost 700 folders. I have 4 columens in my CSV

Column A serial code (codigo)

Column B course (materia)

Column C faculty the course belongs to (facultad)

Column D career that have that course (carrera)

I have the following code for that

$materias = Import-Csv C:\Materias.csv

foreach{$EstMaterias in $materias)
{
    $path = "C:\" 
    $codigo = $EstMaterias.codigo
    $materia = $EstMaterias.materia
    $facultad = $EstMaterias.facultad
    $carrera = $EstMaterias.carrera

    New-Item -Path("$path\$facultad\$carrera\$mateira") -Type directory
}

I'm not sure how to filter thee subfolders so that the careers are created inside their correct faculty and courses inside their correct career. With the code I run right now all courses are created inside the faculties and inside all of the careers.


Solution

  • You don't use $codigo = $EstMaterias.codigo, and you have typing error here "$path\$facultad\$carrera\$mateira". You typed in $mateira instead of $materia.

    $materias = Import-Csv C:\Materias.csv
    
    $path = "C:"
    
    foreach ($EstMaterias in $materias) {
        $codigo = $EstMaterias.codigo # What is this? You don't use it here.
        $materia = $EstMaterias.materia
        $facultad = $EstMaterias.facultad
        $carrera = $EstMaterias.carrera
    
        New-Item -Path "$path\$facultad\$carrera\$materia" -Type directory
    }