Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Multiline File to CSV

Posted on 2013-06-19
4
Medium Priority
?
440 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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 57

Assisted Solution

by:Bill Prew
Bill Prew earned 1000 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

Are You Ready for GDPR?

With the GDPR deadline set for May 25, 2018, many organizations are ill-prepared due to uncertainty about the criteria for compliance. According to a recent WatchGuard survey, a staggering 37% of respondents don't even know if their organization needs to comply with GDPR. Do you?

Question has a verified solution.

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

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

705 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