Create Folder based on CSV file with PowerShell

Tagged: 

Viewing 3 reply threads
  • Author
    Posts
    • #1571645
      IT Consultant
      Participant
      Member Points: 401
      Rank: 2

      I have a CSV file and I would like to create folders based on 2 columns from that CSV file.

      $Folders = Import-Csv -Path ".\data.csv" -Delimiter ";" | Select-Object info1, info2

      I’m able to get info1 and info2 but not able to test if the folder already exists or not.

      If not then the script should create it like this *info1 info2*

    • #1571651
      Leos Marek
      Moderator
      Member Points: 25,048
      Author of Year 2020Author of the Year 2021
      Rank: 4

      Try Test-Path?

      • #1571652
        IT Consultant
        Participant
        Member Points: 401
        Rank: 2

        with test-path is not working 🙁

        $Names = Import-Csv -Delimiter "," -Path "$PSScriptRoot\Info.csv" | Select-Object customer, info
        $Folders = Set-Location -Path "$PSScriptRoot\Collection"
        foreach($customer in $Names){
        if (-not (Test-Path $Folders)) {
        New-Item -Path $Folders -Name ($($customer.customer)+" "+$($customer.info)) -ItemType Directory
        Write-Host -ForegroundColor Green "`tfolder $($client.client)" "$($client.cluster) created"
        }
        else {
        Write-Host -ForegroundColor White "`tfolder $($client.client)" "$($client.cluster) exist"
        }
        }
    • #1571654
      David Figueroa
      Participant
      Member Points: 5,224
      Rank: 3

      Like I mentioned on the other post, you should not get into the habit of using + to join strings.. it’s inefficient, and in the wrong circumstance can cause massive performance problems.  The format operator is much better.

      It would help a lot to provide the CSV layout, but this is mostly a guess.

      $Names = Import-Csv -Path “$PSScriptRoot\Info.csv” | Select-Object customer, info # comma is the default delimiter, it does not need to be specified
      Set-Location -Path "$PSScriptRoot\Collection"
      foreach($customer in $Names) {
      $CustomerPath = '{0}{1}' -f $Customer.customer, $Customer.Info
      if (Test-Path -Path .\$CustomerPath) {
      $Message = '{0}{1} {2} exists' -f "`t", $Customer.Client, $Customer.Info
      Write-Message -Message $Message -ForegroundColor Green
      }
      else {
      $null = mkdir $CustomerPath
      $Message = '{0}{1} {2} created' -f "`t", $Customer.Client, $Customer.Info
      Write-Host -Message $Message -ForegroundColor Green
      }
      }
      
      • #1571724
        IT Consultant
        Participant
        Member Points: 401
        Rank: 2

        First of all, I would like to thank you very much, because not only is the problem solved, but I learned from these few lines, Thank you.

        I know those two values are tied to $Customer.Customer, $Customer.Info, and besides this allowed me to go further and it allowed me to identify errors in my CSV by adding more values.

        The parameter “- f” I did not understand what it corresponds to:).

        I would like to take advantage of your level and experience and I wanted to have your opinion to add folders “- CSV- EXCEL-…” for each client what is the best way to proceed?

    • #1571834
      David Figueroa
      Participant
      Member Points: 5,224
      Rank: 3

      -f is the format operator, and it has numerous sub-functions.  But, the primary one we’re using here is to substitute various placeholders ( {0}, {1}, {2})  etc. with supplied values.

      “{0} is a {1}” -f ‘This’, ‘test’ will print out This is a test.

      As far as creating folders from your supplied CSV, it might be something like:

      foreach ($customer in $csv) {
          new-Item -Path C:\Customers -Name ( '{0}-CSV-Excel' -f $Customer.Customer) -ItemType Directory
      }

      Coralon

      avatar
      • #1571836
        IT Consultant
        Participant
        Member Points: 401
        Rank: 2

        Thank you 🙂

        I didn’t express myself correctly, I’m sorry.
        the CSV folder is created for each customer as it will contain additional data.
        My basic CSV file includes key information on all customers.
        for instance, each client file must have subdirectories named, [CSV], [Excel], [HTML]…etc.

        for sure if the subdirectorie if it’s already created it should not be created again 🙂

Viewing 3 reply threads
  • You must be logged in to reply to this topic.
© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account