cfoster5
cfoster5

Reputation: 1826

Use Import-Csv cmdlet with all files in directory

Using PowerShell, I have created a script that adds headers to a CSV, select unique value within specified columns, and exports it as a new CSV.

That script is:

import-csv -Header (1..39) -Path 'J:\consult evv\splitfile_385.csv' | select '30', '31', '39' -Unique | Export-Csv -Path "C:\Users\CFOSTER\Desktop\stuff\modsplitfile_385.csv" -NoTypeInformation

This script only works for one item: splitfile_385.csv. I'd like to use this for all files in a directory. I can loop through items in the specified path but am having trouble combining the loop with the script above.

Currently, I have:

$files = Get-ChildItem "J:\consult evv"
for ($i=0; $i -lt $files.Count; $i++) {

    $outfile = "C:\Users\CFOSTER\Desktop\Stuff\new" + $files[$i]
    Get-Content $files[$i].FullName | Where-Object { !($_ -match 'step4' -or $_ -match 'step9') } | import-csv -Header (1..39) -Path $files[$i].FullName | select '30', '31', '39' -Unique | Set-Content $outfile
}

Using this gives me the error: import-csv : You must specify either the -Path or -LiteralPath parameters, but not both.

How can I combine my script with a loop for all files in a directory?

Upvotes: 0

Views: 807

Answers (2)

Esperento57
Esperento57

Reputation: 17492

try somethng like this:

Get-ChildItem "J:\consult evv" -file -Filter "*.csv" | %{

    $outfile = "C:\Users\CFOSTER\Desktop\Stuff\new" + $_.Name

    Get-Content $_.FullName | select -skip 1 |  Where { !($_ -match 'step4' -or $_ -match 'step9') } | 
        ConvertFrom-Csv -Header (1..39) | select '30', '31', '39' -Unique | Out-File $outfile -Append 
}

Upvotes: 0

Bill_Stewart
Bill_Stewart

Reputation: 24585

You can use ForEach-Object and import one at a time.

Get-ChildItem "C:\CSV Files\*.csv" | ForEach-Object {
  Import-Csv $_ | Where-Object { ... }
}

If you want to output to a new CSV file, make sure the output filename doesn't match the pattern used with Get-ChildItem or you'll run into trouble.

Upvotes: 2

Related Questions