How to combine 2 .csv files using script

Member_2_5306354
Member_2_5306354 used Ask the Experts™
on
I would kind to use a script that will look at a specific folder and if there are any .csv files it should join them.
All the csv files have the exact number of positions separated by a comma
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Michael B. SmithManaging Consultant

Commented:
So, if we have file1.csv and file2.csv in folder1; what should the final filename be? The final folder name be? What if there are more than two?

Do both (or either or all) files have header lines? If so, which header line should be retained, and which should be discarded?
Technical Consultant
Commented:
Good Morning,

This should get you started, I've tested this and it worked fine for my applications.

$getFirstLine = $true

get-childItem "C:\Temp\*.csv" | foreach {
    $filePath = $_

    $lines =  $lines = Get-Content $filePath  
    $linesToWrite = switch($getFirstLine) {
           $true  {$lines}
           $false {$lines | Select -Skip 1}

    }

    $getFirstLine = $false
    Add-Content "Output.csv" $linesToWrite
    }

Open in new window

Enjoy.

Jackson
Mustafa L. McLinnSystems Engineer/Systems Administrator

Commented:
How are you trying to combine them, are they still to be labeled as csv1_columnA,csv1_columnB ?
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Author

Commented:
Thanks to all.
There could be several perhaps 4 or more .csv files wit all different names.
There .csv files that are separated by commas once you use edit to open them, it is very clear that all of them have values that are separated by commas.
So all the script needs to do is to take the data in each .csv file it finds, create a new file, perhaps called new.csv and paste all the values from inside each .csv into the new file.

Author

Commented:
Hi Jackson, I tried your script however it does not seem to run.
Is it batch, or powershell, or vbs?
I can't seem to get it to work.
Thanks again.
Jackson FavreTechnical Consultant

Commented:
It's a powershell script.

You can copy paste it into a txt file then rename the txt extension to ps1.

The script removes the header of each subsequent CSV so if you don't need that we can adjust it.

Cheers

Jackson

Author

Commented:
Thanks Jackson I'll try again now.. I had done that but it didnt' work for some reason... will try again.
So is it looking in C:\Temp for the files or where the script runs?

Author

Commented:
Thanks Jackson, this worked awesome.

Update..
It's no longer working.. Sometimes it will join the files and sometimes it will not.. it's not consistent.
Jackson FavreTechnical Consultant

Commented:
Odd, it works fine for me.

Have you been able to determine whether there are circumstances surrounding the failures that are identifiable?

Cheers

Jackson

Author

Commented:
Thanks Jackson.
No I could not, so I had to use a different script.
Thanks again.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial