Solved

Multiline File to CSV

Posted on 2013-06-19
4
413 Views
Last Modified: 2013-07-23
Experts,

I have a large number of multiline files, that look like the below example.  Could these files be reformatted as a CSV automatically?  Could you do a replace in Powershell to remove the new line breaks and replace them with Commas?

BEGMF;
MFVERSION 2;
MFDESC;
 BeginImportJob
  AutoApproved = True
  Class = Document
  DeleteProcessedFiles = True
  RequireReview = False
 BeginObject
  FileName = \\server\temp$\Tiffs\823609E02.tif
   BeginAttributes
    Document_Number = 823609
    Document_Name = 823609E02.tif
    Document_Revision = L
    Document_Type = Tiff
    Document_Description = DOCUMENT DESCRIPTION
    SheetNum = 02
    ReleaseDate = 05/08/2009
    DrawnBy = FIUKSP    
   EndAttributes
  EndObject
  EndImportJob
  ENDMF;

Open in new window

0
Comment
Question by:lm1189
  • 2
4 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 39259546
Can you give a sample output which you are expecting?
0
 

Author Comment

by:lm1189
ID: 39259668
A CSV, with all the lines as columns. IE:

BEGMF;,MFVERSION 2;,MFDESC;, BeginImportJob,  AutoApproved = True,Class = Document  ,DeleteProcessedFiles = True

Open in new window

0
 
LVL 40

Accepted Solution

by:
Subsun earned 250 total points
ID: 39259727
Try with following code and see if it works for you..
Set-Content C:\Temp\result.csv ((GC C:\temp\input.txt) -join ",")

Open in new window

Replace C:\temp\input.txt with input file path
C:\Temp\result.csv is the output file..
0
 
LVL 53

Assisted Solution

by:Bill Prew
Bill Prew earned 250 total points
ID: 39260393
Here's a small VBS that should do the job.

Run as

cscript EE28161581.vbs EE28161581.in ee28161581.out

or

cscript EE28161581.vbs EE28161581.in

in the first case it reads from the first file and writes to the second.  In the second case it reads from the first file and over writes it with the changes.

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Get input file name from command line parm, if 2 parms entered
' use second as new output file, else rewrite to input file
If (WScript.Arguments.Count > 0) Then
   strInFile = WScript.Arguments(0)
Else
   WScript.Echo "No filename specified."
   WScript.Quit
End If
If (WScript.Arguments.Count > 1) Then
   strOutFile = WScript.Arguments(1)
Else
   strOutFile = strInFile
End If

' Read file into an array
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strInFile, ForReading, False, TriStateUseDefault)
strData = objFile.ReadAll
objFile.Close

strData = Replace(strData, vbCrLf, ",")
strData = Replace(strData, "ENDMF;,", "ENDMF;" & vbCrLf)

' Rewrite file with any changes made
Set objFile = objFSO.OpenTextFile(strOutFile, ForWriting, True)
objFile.Write strData
objFile.Close

Open in new window

~bp
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now