Solved

Multiline File to CSV

Posted on 2013-06-19
4
420 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 54

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

Does Powershell have you tied up in knots?

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Powershell \ Office 365 script 4 39
Export DG's members info in Office 365. 2 25
[IIS FTP 7.5] Change FTP home dir for 300 accounts 7 28
-OutVariable to Global 1 20
A brief introduction to what I consider to be the best editor for PowerShell.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦

756 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