Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Merge Multiple CSV's

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 02-17-2007   #1 (permalink)
Newbie


Join Date: Feb 2007
 
Rep Power: 13
kirby14 will become famous soon enough
  kirby14 is offline

Merge Multiple CSV's

I know how to do it in VBScript, but I'm wondering if it can be done better in PS.

I want to search through a directory for all files named "blah_blah_<date>*"

Then I want to take all of thse files and place the 2nd column into a new CSV with 3 columns.

IE.

blah_blah_20050217_test.csv (lots of these)
"foo","name of someone","bar"


New_CSV.csv
"name of someone","name of someone else","name of someone else"
"name of someone else","name of someone else","name of someone else"


If anyone can get me going in the right direction or if I haven't explained myself well, please tell me.

Can I do an LS using a wildcard and then process each of the files it returns 1 by 1?

My System SpecsSystem Spec
Old 02-17-2007   #2 (permalink)
Newbie


Join Date: Feb 2007
 
Rep Power: 13
kirby14 will become famous soon enough
  kirby14 is offline

So far I have something along these lines:

Code:
$a = get-content "c:\PS\letter_env_20070217*"
foreach ($i in $a)
{Add-Content C:\PS\newoldtest.csv ($i.Split(",")[1] + ",")}
Main problem is that it sends a CrLf after each one, is there any way to suppress that?
My System SpecsSystem Spec
Old 02-17-2007   #3 (permalink)
Techstarts
Guest


 

Re: Merge Multiple CSV's

I had similiar problem, I was able to merge two CSV file atleast results.
But formattting would require more work.
You can check my blog at http://techstarts.blogspot.com/search/label/HotFix

Hope you find useful.

Thanks,
Techstarts


"kirby14" <kirby14.2m69pf@no-mx.forums.net> wrote in message
news:kirby14.2m69pf@no-mx.forums.net...
>
> So far I have something along these lines:
>
>
> Code:
> --------------------
> $a = get-content "c:\PS\letter_env_20070217*"
> foreach ($i in $a)
> {Add-Content C:\PS\newoldtest.csv ($i.Split(",")[1] + ",")}
> --------------------
>
>
> --
> kirby14



My System SpecsSystem Spec
Old 02-17-2007   #4 (permalink)
Newbie


Join Date: Feb 2007
 
Rep Power: 13
kirby14 will become famous soon enough
  kirby14 is offline

In case it helps any, here is the main piece of VBScript code I'm using for this:

Code:
Set objReadFile = objFSO.OpenTextFile (strReadDirectory & "\" & ObjFSO.GetFileName(File), ForReading) 

    Do Until objReadFile.AtEndOfStream
        strNextLine = objReadFile.Readline
        arLine = Split(strNextLine , chr(34))

        If (i < 2) Then
            objWriteFile.Write(", " & Trim(arLine(3)))
            i = i + 1
        Else
            objWriteFile.WriteLine(", " & Trim(arLine(3)))
            i = 0
        End If
    Loop
My System SpecsSystem Spec
Old 02-18-2007   #5 (permalink)
Newbie


Join Date: Feb 2007
 
Rep Power: 13
kirby14 will become famous soon enough
  kirby14 is offline

Code:
If (Test-Path($strFileNameSearch))
    {
    $a = get-content $strFileNameSearch

    $y = 0
    for ($x = 0; $x -lt $a.length; $x++){
        if ($y -lt 3){
            $names += "," + $a[$x].split("`"")[3]
            $y +=1}
        else{
            $names += "`n"
            $y = 0}
    }
    add-content $strWritePath $names
}
This code works for me

(note - this is just out of testing stages, I realize variable names are awful and such)

Last edited by kirby14; 02-18-2007 at 01:02 AM.
My System SpecsSystem Spec
Old 02-18-2007   #6 (permalink)
Newbie


Join Date: Feb 2007
 
Rep Power: 13
kirby14 will become famous soon enough
  kirby14 is offline

Code:
If (Test-Path($strFileNameSearch))
    {
    $a = get-content $strFileNameSearch

    $y = 0
    for ($x = 0; $x -lt $a.length; $x++){
        if ($y -lt 2){
            $names += "," + $a[$x].split("`"")[3].Trim()
            $y +=1}
        else{
            $names += += "," + $a[$x].split("`"")[3].Trim() + "`n"
            $y = 0}
    }
    add-content $strWritePath $names}
Small Fix -

Last edited by kirby14; 02-18-2007 at 05:25 PM.
My System SpecsSystem Spec
Old 02-18-2007   #7 (permalink)
/\\/\\o\\/\\/ [MVP]
Guest


 

Re: Merge Multiple CSV's

You might also be interested in this series on my blog about using ADO and
dataTables for this :

http://mow001.blogspot.com/2006/03/w...-part-one.html
http://mow001.blogspot.com/2006/03/w...-part-two.html
http://mow001.blogspot.com/2006/04/m...-more-csv.html

Greetings /\/\o\/\/
http://thePowerShellGuy.com

"kirby14" <kirby14.2m6uqn@no-mx.forums.net> wrote in message
news:kirby14.2m6uqn@no-mx.forums.net...
>
> Code:
> --------------------
> $a = get-content "c:\PS\letter_env_20070217*"
>
> $y = 0
> for ($x = 0; $x -lt $a.length; $x++){
> if ($y -lt 3){
> $names += "," + $a[$x].split("`"")[3]
> $y +=1}
> else{
> $names += "`n"
> $y = 0}
> }
> add-content "c:\PS\test123.csv" $names
> }
> --------------------
> This code works for me
>
> (note - this is just out of testing stages, I realize variable names
> are awful and such)
>
>
> --
> kirby14


My System SpecsSystem Spec
Old 02-19-2007   #8 (permalink)
Newbie


Join Date: Feb 2007
 
Rep Power: 13
kirby14 will become famous soon enough
  kirby14 is offline

Quote:
/\\/\\o\\/\\/ [MVP]
View Post
Cool, very interesting read!

My final code:
Code:
If (Test-Path($strWritePath))
 {Output ("File " + $strWritePath + " already exists."); }
Else{
 $content = Get-Content $strFileNameSearch
 If($content.length -lt 1){Output ("No Files Found")}
 Else{
  $col = 0
  for ($i = 0; $i -lt $content.length; $i++)
  {
   If ($col -lt 2){$names += "," + $content[$i].split("`"")[3].Trim(); $col++}
   Else{$names += "," + $content[$i].split("`"")[3].Trim() + "`n"; $col = 0}
  }
 Add-Content $strWritePath ",,Place Text for Header"
 Add-Content $strWritePath $names
 
 }
}
My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Mail merge weenis Live Mail 1 05-30-2008 03:00 PM
Merge one bitmap onto another in vb Jon Jacobs .NET General 0 05-27-2008 11:41 AM
merge directories marty Vista General 2 10-06-2007 11:28 AM
merge directories marty Vista General 0 10-05-2007 01:09 PM
Merge Images Raghavendra Avalon 3 10-27-2006 01:10 AM


Update your Vista Drivers Update Your Drivers Now!!

Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008