Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Parse ini file, write specific values to .csv

Posted on 2007-12-05
12
Medium Priority
?
690 Views
Last Modified: 2010-04-21
Hello experts,

I have an ini file where the desired values are located on specific lines denoted by quotation marks.
So far I have the line-values echoing back as needed, but not the values only. I need to trim them, but they are not all the same trim parameters. Left trimming varies by 5-9 chars and the right trim is always 1.
It may be easier to just read line by line and only get the values between the quotation marks, but I don't know how to do it. Instead of echoing back the output, I need to save it as a csv file of just the values.

Any help would be greatly appreciated.

Here is my working script:
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\currdat.txt", ForReading)

For i = 1 to 24
    objTextFile.ReadLine
Next
strLine = objTextFile.ReadLine
Wscript.Echo strLine
For i = 26 to 28
    objTextFile.ReadLine
Next
strLine = objTextFile.ReadLine
Wscript.Echo strLine
For i = 30 to 31
    objTextFile.ReadLine
Next
strLine = objTextFile.ReadLine
Wscript.Echo strLine
For i = 33 to 34
    objTextFile.ReadLine
Next
strLine = objTextFile.ReadLine
Wscript.Echo strLine
For i = 36 to 38
    objTextFile.ReadLine
Next
strLine = objTextFile.ReadLine
Wscript.Echo strLine
For i = 40 to 42
    objTextFile.ReadLine
Next
strLine = objTextFile.ReadLine
Wscript.Echo strLine
For i = 44 to 47
    objTextFile.ReadLine
Next
strLine = objTextFile.ReadLine
Wscript.Echo strLine
For i = 49 to 53
    objTextFile.ReadLine
Next
strLine = objTextFile.ReadLine
Wscript.Echo strLine
For i = 55 to 57
    objTextFile.ReadLine
Next
strLine = objTextFile.ReadLine
Wscript.Echo strLine
For i = 59 to 61
    objTextFile.ReadLine
Next
strLine = objTextFile.ReadLine
Wscript.Echo strLine
For i = 63 to 65
    objTextFile.ReadLine
Next
strLine = objTextFile.ReadLine
Wscript.Echo strLine
For i = 67 to 69
    objTextFile.ReadLine
Next
strLine = objTextFile.ReadLine
Wscript.Echo strLine
For i = 71 to 73
    objTextFile.ReadLine
Next
strLine = objTextFile.ReadLine
Wscript.Echo strLine
objTextFile.Close
0
Comment
Question by:Phosphor
  • 6
  • 6
12 Comments
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20413705
Try this script........

regards
Chandru
Option Explicit
Dim oFSO, sFile, oFile, sText
Set oFSO = CreateObject("Scripting.FileSystemObject")
sFile = "your text file"
If oFSO.FileExists(sFile) Then
Set oFile = oFSO.OpenTextFile(sFile, 1)
 Do While Not oFile.AtEndOfStream
  sText = oFile.ReadLine
   If Trim(sText) <> "" Then
    WScript.Echo sText
   End If
 Loop
oFile.Close
Else
WScript.Echo "The file was not there."

Open in new window

0
 
LVL 1

Author Comment

by:Phosphor
ID: 20413984
I got an error "Expected 'End'", added it, then "Expected 'If'". The loop worked but not what I am looking for. I only want the values that between the quotation marks. Which are on specific lines. e. g.
line 60        locat="NW"
line 64        temp="99.0"
line 68        WWV3="187"

Output to text, not echo.
Sorry, there's a lot I am trying to learn in a short amount of time...
Option Explicit
Dim oFSO, sFile, oFile, sText
Set oFSO = CreateObject("Scripting.FileSystemObject")
sFile = "H:\Scripting\HW\hw.txt"
If oFSO.FileExists(sFile) Then
Set oFile = oFSO.OpenTextFile(sFile, 1)
 Do While Not oFile.AtEndOfStream
  sText = oFile.ReadLine
   If Trim(sText) <> "" Then
    WScript.Echo sText
   End If
 Loop
oFile.Close
Else
WScript.Echo "The file was not there."
End If
0
 
LVL 12

Accepted Solution

by:
chandru_sol earned 750 total points
ID: 20414062
Try this..........

regards
Chandru
Option Explicit
Dim oFSO, sFile, oFile, sText
Set oFSO = CreateObject("Scripting.FileSystemObject")
sFile = "c:\scripts\testcomma.txt"
If oFSO.FileExists(sFile) Then
Set oFile = oFSO.OpenTextFile(sFile, 1)
 Do While Not oFile.AtEndOfStream
  sText = oFile.ReadLine
if instr(stext,"=") then
    WScript.Echo sText
   End If
 Loop
oFile.Close
Else
WScript.Echo "The file was not there."
End If

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Phosphor
ID: 20414156
This one returns fewer lines (any with [bracketed]  text are omitted because they don't have "=" signs), it seems to return any lines with an "=" sign, which is interesting and possibly useful down the road. Is it possible to to use the " " marks as tags? The values I need are only within the quotation marks.
0
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20414188
This should work for you


regards
Chandru
Option Explicit
Dim oFSO, sFile, oFile, sText
Set oFSO = CreateObject("Scripting.FileSystemObject")
sFile = "c:\scripts\testcomma.txt"
If oFSO.FileExists(sFile) Then
Set oFile = oFSO.OpenTextFile(sFile, 1)
 Do While Not oFile.AtEndOfStream
  sText = oFile.ReadLine
if instr(stext,"""") then
    WScript.Echo sText
   End If
 Loop
oFile.Close
Else
WScript.Echo "The file was not there."
End If

Open in new window

0
 
LVL 1

Author Comment

by:Phosphor
ID: 20414254
This one returns only lines with quotation marks. (which is good because values I need all have quotation marks, though still need to just get the values within the quotation marks. Is it possible to create an array out of those lines and then trim them in another process perhaps?
0
 
LVL 1

Author Comment

by:Phosphor
ID: 20421679
Using Chr(34) with a really long script implementing intermediate text files got me where I needed to go.  The second iteration you posted works to strip out all the stuff I absolutely don't need (blank lines, ini tags etc.) Thanks for helping out
Const ForReading = 1
Const ForWriting = 2
 
'Dim oFSO, sFile, oFile, sText, sNText
Set oFSO = CreateObject("Scripting.FileSystemObject")
sFile = "H:\Scripting\HW\hw.txt"
If oFSO.FileExists(sFile) Then
Set oFile = oFSO.OpenTextFile(sFile, 1)
 Do Until oFile.AtEndOfStream
  sText = oFile.ReadLine
if instr(stext,"=") then
    'WScript.Echo sText
sNText = sNText & sText & vbCrLf
   End If
 Loop
oFile.Close
Else
WScript.Echo "The file was not there."
End If
 
 
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.OpenTextFile _
    ("h:\Scripting\hw\CSWrite.txt", ForWriting)
oFile.Write(sNText)
oFile.Close
 
Const ForAppending = 8
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("h:\Scripting\hw\CSWrite.txt", ForAppending)
 
objFile.Write "Timestamp=" & Chr(34) & Now & Chr(34)
 
objFile.Close

Open in new window

0
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20421716
Hope it solved your purpose

regards
Chandru
0
 
LVL 1

Author Closing Comment

by:Phosphor
ID: 31413214
Accepting answer in protest!

I don't believe the replies were exactly what I was looking for. I wanted to make this script  as concise as possible, though I ended up stringing a bunch of scripts together to get my desired results.  Using a variety of splits/replacement routines being performed on intermediate files I was able to get my project going, but I did it by myself. I would not have awarded any points for this query and I'd be hard-pressed to award any points to my my other scripting question that is still open. I think you need to be able to adjust points, or outright deny them for half-assed, incomplete answers.
I am beginning to think that experts-exchange is going downhill as far as quality feedback is concerned.
0
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20421765
If that was the case i would have helped you to get what you wanted. I thought that solved your purpose

regards
Chandru
0
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20421792
May be if you think i can still help please let me know

I will be glad to help :-)
0
 
LVL 1

Author Comment

by:Phosphor
ID: 20422060
Nothing personal, I appreciate the help though you never answered me back.  I'll chalk it up to a learning experience.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
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…
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 anti-spam), the admin…
Loops Section Overview
Suggested Courses

885 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