CSV Row Problem

Tagged: ,

This topic contains 4 replies, has 4 voices, and was last updated by Profile photo of BrandonB BrandonB 9 months, 1 week ago.

  • Author
  • #54073
    Profile photo of JP

    Hey guys! I am currently trying to pull a list of names from a CSV file and creating directories for each name.
    The original directory I want to copy is the variable ($Folder). (There are other folders within it hence the -recurse)

    The CSV has two columns firstname, lastname and various dummy names in the following rows.

    I am getting the first row/name coming back perfect, but it seems I can't figure out how to do a loop of each row after that one.
    I have been looking all over the net for a solution. Thanks for your time, any help would be appreciated.

    • This topic was modified 9 months, 2 weeks ago by Profile photo of JP JP.
  • #54076
    Profile photo of Jonathan Warnken
    Jonathan Warnken

    Because your getname function returns after the first iteration through data imported it will only return the first row.
    something like this may be more useful to you

    $ImportCsv = Import-Csv -Path $Csv
    foreach($row in $ImportCsv ) {
        $firstname = $row.firstname 
        $lastname =  $row.lastname
        $name = "$firstname$lastname"
        Copy-item $Folder\* -destination "C:\Users\jp\Desktop\folderscript\testfolder\$($name)" -Recurse
  • #54077
    Profile photo of JP

    You are a deadset legend! Thank you so much Jonathan!

  • #54095
    Profile photo of Rob Simmers
    Rob Simmers

    You can simplify this a bit more. If you look at "Get-Help Copy-Item -Full", you'll see that you can pass the Path and Destination by ByPropertyName:

            Required?                    true
            Position?                    0
            Accept pipeline input?       true (ByValue, ByPropertyName)
            Parameter set name           Path
            Aliases                      None
            Dynamic?                     false

    If we utilize calculated expressions, you can name the property to Path and Destination respectively. Then you can pipe the data directly to Copy-Item:

    $csv = Import-Csv -Path C:\Users\Rob\Desktop\Archive\test.csv | 
    Select FirstName,
           @{Name="Destination";Expression={"C:\Users\Rob\Desktop\{0}{1}" -f $_.FirstName, $_.LastName}}
    $csv | Copy-Item -WhatIf 

    If the column doesn't match a parameter (e.g. FirstName, LastName), it is ignored. If your property name matches a parameter name (or Alias), it will pass that to the command. This is what the $CSV looks like:

    FirstName LastName Path                           Destination                     
    --------- -------- ----                           -----------                     
    Rob       Simmers  C:\Users\Rob\Desktop\Archive\* C:\Users\Rob\Desktop\RobSimmers 
    John      Smith    C:\Users\Rob\Desktop\Archive\* C:\Users\Rob\Desktop\JohnSmith  
    Sam      Johnson  C:\Users\Rob\Desktop\Archive\* C:\Users\Rob\Desktop\SamJohnson

    The fun part about this approach is you don't need a foreach loop, because it will be done with the Process inside the copy item function.

  • #54105
    Profile photo of BrandonB

    Please disregard wrong forum venue

    • This reply was modified 9 months, 1 week ago by Profile photo of BrandonB BrandonB. Reason: Delete this post please

You must be logged in to reply to this topic.