I want to split a text file in how many names are in the text file (sorry for the bad explanation)

See attached file for the complete example

Datum                      Klant naam                                       ArtCode
-----                           ----------                                              -------
1-4-2015 0:00:00     Cafe Brakke                                        20206  
1-4-2015 0:00:00     Cafe Brakke                                        20110  
1-4-2015 0:00:00     Cafe Lime                                          50110  
1-4-2015 0:00:00     Cafe Lime                                          04355  
1-4-2015 0:00:00     Café Pollux                                        16368  
1-4-2015 0:00:00     Café Pollux                                        16356  

In this example i need 3 x a  text file, the first one called Cafe Brakke.txt and in the text file
Datum                      Klant naam                                        ArtCode
-----                           ----------                                               -------
1-4-2015 0:00:00     Cafe Brakke                                        20206  
1-4-2015 0:00:00     Cafe Brakke                                        20110  

2nd file  Cafe Lime.txt and in the file
Datum                      Klant naam                                       ArtCode
-----                           ----------                                              -------
1-4-2015 0:00:00     Cafe Lime                                          50110  
1-4-2015 0:00:00     Cafe Lime                                          04355  

3th file  Café Pollux.txt and in the file
Datum                      Klant naam                                       ArtCode
-----                           ----------                                              -------
1-4-2015 0:00:00     Café Pollux                                        16368  
1-4-2015 0:00:00     Café Pollux                                        16356  

If somebody also can help me to remove the 0:00:00 behind the date it would be perfect (not necessary:)
Thanks for helping

Danny
LVL 6
Danny KonAsked:
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.

Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
Here it is: ( Output in .csv form for you)

save the example file in C:\Temp\DataFileSplit.txt    (Modify it as required)

Output File Location : "c:\temp\"   (Modify  $OutputLocation   as required)

$content = Get-Content C:\Temp\DataFileSplit.txt

$content = $content -replace "   "," "
$content = $content -replace " 0:00:00",""

$len = $content.Length
$content = $content[2..$len]

$items = @()
$Outputlocation = "c:\temp\"


foreach ($line in $content)
{
$line = $line -replace "   "," "
$line = $line -replace "  "," "

$datum = $line.Split(" ")[0]
$klantNaam = $line.Split(" ")[1] + " "  + $line.Split(" ")[2]
$artcode = $line.Split(" ")[-2]

$item = New-Object System.Object
$item | Add-Member -Type Noteproperty -Name Datum -Value $datum.Trim()
$item | Add-Member -Type Noteproperty -Name KlantNaam -Value $klantNaam
$item | Add-Member -Type Noteproperty -Name Artcode -Value $artcode

$items += $item

}
#$items | Sort-Object Artcode -Descending

foreach ($item in $items)
{
$filename = $Outputlocation + "$($item.KlantNaam).csv"
$item | Export-Csv $filename -NoTypeInformation -Append -Encoding BigEndianUnicode
}

Open in new window

0
Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
Oh you missed to attach the file. I just knew it
0
Bill PrewIT / Software Engineering ConsultantCommented:
Need a sample of the input file, is it space delimited, or fixed column format (looks a bit like a report file), or are there tabs in it, etc?

~bp
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Danny KonAuthor Commented:
Sorry here is the attached file.

Thanks Danny
test.txt
0
Danny KonAuthor Commented:
Raheman

Almost there, 3 problems, 1 file is unreadable and does not have a name, a second file has the name ---------- .csv
and inside the file i see
þÿ"Datum","KlantNaam","Artcode"
-----,"---------- ",""
 
All the files showing quotation marks around the names
þÿ"Datum","KlantNaam","Artcode"
1-4-2015,"Black Tiger","01201"

And the text in the file always start with letters þÿ

Thanks Danny
0
Bill PrewIT / Software Engineering ConsultantCommented:
Here is a VBscript solution, tested here, works as requested.  Save as a VBS file, and adjust inout file name in the script.  Then run from a command line in the folder where the files reside like:

cscript clients.vbs

' Text file I/O constants
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const TristateTrue = -1
Const TristateFalse = 0
Const TristateUseDefault = -2

' Create the file system object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Define input file name
strFileIn = "test.txt"

' Make sure input file exists
If Not objFSO.FileExists(strFileIn) Then
   Wscript.Echo "ERROR: Input file """ & strFileIn & """ does not exist."
   Wscript.Quit
End If

' Read entire input file into an array, break lines into an array
Set objFileIn = objFSO.OpenTextFile(strFileIn, ForReading, False, TriStateUseDefault)
arrLines = Split(objFileIn.ReadAll, VbCrLf)
objFileIn.Close
Set objFileIn = Nothing

' CLear holder of header lines (needed for each new client file)
strHeader = ""

' Process all lines from input file
For i = LBound(arrLines) To UBound(arrLines)

   ' Get this line from tjhe array of all lines
   strLine = arrLines(i)

   ' Skip blank lines in input file
   If strLine <> "" Then

      ' If header lines, save for later use
      If i < 3 Then
         strHeader = strHeader & strLine & vbCrLf
      Else
         ' Process data line, extracting client name
         strLine = Replace(strLine, "0:00:00", "       ")
         strClient = Trim(Mid(strLine, 21, 50))

         ' Write to individual file for just this Client
         WriteClientFile strClient, strLine
      End If

   End If

Next

' Wrap up
Wscript.Quit


' Subroutine to add a row to the individual file for the Client
Sub WriteClientFile(strClient, strLine)
   ' Build name for this Cline file
   strClientFile = strClient & ".txt"

   ' If the file doesn't exist yet we need the header line
   If objFSO.FileExists(strClientFile) Then 
      blnNeedHeader = False
   Else 
      blnNeedHeader = True
   End If

   ' Open this file for apending
   Set objClientFile = objFSO.OpenTextFile(strClientFile, ForAppending, True)

   ' Write header line of needed
   If blnNeedHeader Then
      objClientFile.Write strHeader
   End If

   ' Write data line
   objClientFile.WriteLine strLine
End Sub

Open in new window

~bp
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
Danny KonAuthor Commented:
Bill Prew,

It works like a charm but I can't get it to work in my powershell script "cscript.exe test.vbs"
But that has nothing to do with my question
Perfect solution

Thanks Danny
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.