0

I have got multiple measurement files with one column of numeric data each. (Update: The script should work for variable numbers of measurement files.)

data1.dat

1.0
2.0
3.0

data2.dat

10.0
20.0
30.0

...

dataN.dat

1
1
1

How can I merge these data files into a comma separated values file using Powershell?

"data1","data2.dat",...,"dataN.dat"
1.0,10.0,...,1
2.0,20.0,...,1
3.0,30.0,...,1

Related

2 Answers 2

2

Here's one way:

$files = Get-ChildItem D:\temp *.dat
$header = $files|foreach {$_.basename}
$content= $files | foreach { ,(gc $_.fullname) }

$lines = for($i=0; $i -lt $content[0].Count; $i++)
{
    $line = for($x=0; $x -lt $files.count; $x++)
    {
        $content[$x][$i]        
    }

    $line -join ','
}

$lines | ConvertFrom-Csv -Header $header | Export-Csv data.csv
Sign up to request clarification or add additional context in comments.

3 Comments

Can you please explain the comma in foreach { ,(gc $_.fullname) }?
sure, the comma operator is the array construction operator in PowerShell. It creates a one element array and puts the result in the first element. Without it, all lines of all files would assign to $content. Remove it and print $content, you'll see what I mean.
Thanks to your comment I found stackoverflow.com/questions/11138288/… which I want to share.
1

You could try something like this:

$file1 = "C:\firstTxtFile.txt"
$file2 = "C:\SecondTxtFile.txt"
$outputFile = "C:\Output.txt"

$file1String = Get-content $file1 
$file2String = Get-content $file2

'"data1.dat","data2.dat"' > $outputFile

$counter = 0
while(($file1String[$counter] -ne $null) -and ($file2String[$counter] -ne $null)){
    $Line = $file1String[$counter] + "," + $file2String[$counter]
    $line >> $outputFile
    $counter++
}

This will take two text file and combine them into one output file.

Note: The output file will be overwritten each time this is run.

1 Comment

Thank you. I have updated my question to clarify that the script needs to process a varying number of files, not a fixed number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.