Solved

Parse ini file, write specific values to .csv

Posted on 2007-12-05
12
684 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
[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
  • 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 375 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 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