Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I need to add combine multiple csv files but adding a header called Source which will be populated with the filename.I have a power shell script that works but Ive tired adding to my C# program. Ive used System.Management.Automation and followed some tutorials but still not working. I was wondering if someone could write it up for me if i provided my powershell skript and add notes to help me in the future.

Thank you

 cd c:\folder
    
    $files = Get-ChildItem ".\" | Where-Object { $_.Extension -eq ".csv" }
    
    for ($i=0; $i -lt $files.Count; $i++) {
        $outfile = $files[$i].FullName + "out" 
        $csv = Import-Csv $files[$i].FullName 
        $newcsv = @()
        foreach ( $row in $csv ) {
            $row | Add-Member -MemberType NoteProperty -Name 'Source' -Value   $files[$i].BaseName
            $newcsv += $row
        }
    
         $newcsv | Export-Csv $files[$i].Name -NoTypeInformation
    
    }

$files = Get-ChildItem C:\folder\*.csv 
$files | ForEach-Object {Import-Csv $_} | 
    Export-Csv -NoTypeInformation C:\folder\combined.csv


What I have tried:

I have a power shell script that works but Ive tired adding to my C# program. Ive used System.Management.Automation and followed some tutorials but still not working.
Posted
Updated 21-Sep-16 1:17am
v2
Comments
[no name] 21-Sep-16 7:04am    
It would appear that you have mistaken us for rent-a-coder.com or some other scam website. We don't write code to order. If you need to hire someone you need to go to a website that supports that.

1 solution

It doesn't quite work like that.
We do not do your work for you.
If you want someone to write your code, you have to pay - I suggest you go to Freelancer.com and ask there.

But be aware: you get what you pay for. Pay peanuts, get monkeys.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900