How to combine 2 .csv files using script

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
100questionsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michael B. SmithManaging ConsultantCommented:
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?
0
Jackson FavreTechnical ConsultantCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mustafa L. McLinnSystems Engineer/Systems AdministratorCommented:
How are you trying to combine them, are they still to be labeled as csv1_columnA,csv1_columnB ?
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

100questionsAuthor 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.
0
100questionsAuthor 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.
0
Jackson FavreTechnical ConsultantCommented:
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
0
100questionsAuthor 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?
0
100questionsAuthor 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.
0
Jackson FavreTechnical ConsultantCommented:
Odd, it works fine for me.

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

Cheers

Jackson
0
100questionsAuthor Commented:
Thanks Jackson.
No I could not, so I had to use a different script.
Thanks again.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.